Description
Input
Output
Sample Input
5 5
Sample Output
HINT
Source
给定$n, k$,求$\sum_{i = 0} ^{k}C_{n}^{i}$模2333的余数。
显然Lucas。
$\sum_{i = 0} ^{k}C_{n}^{i}\\ =\sum_{i = 0} ^ {k}C_{n \% p}^{i\% p}C_{\left \lfloor \frac{n}{p} \right \rfloor}^{\left \lfloor \frac{i}{p} \right \rfloor}\\ =\left (\sum_{i = 0}^{p}C_{n \% p}^{i\% p} \right )\left ( \sum_{i = 0}^{\left \lfloor \frac{k}{p} \right \rfloor - 1} C_{\left \lfloor \frac{n}{p} \right \rfloor} ^ {\left \lfloor \frac{i}{p} \right \rfloor} \right ) + C_{\left \lfloor \frac{n}{p} \right \rfloor}^{\left \lfloor \frac{k}{p} \right \rfloor}\left ( \sum_{i = 0}^{k \% p}C_{n \% p}^{i} \right )$
考虑中间的一步可以进行递归计算。又因为$p = 2333$,所以最后一个前缀和可以预处理。
会出现一个较大的组合数,可以直接用Lucas算。
其实它可先用Lucas预处理,这样可以少个log。
Code
1 /** 2 * bzoj 3 * Problem#4591 4 * Accepted 5 * Time: 4140ms 6 * Memory: 44008k 7 */ 8 #include <bits/stdc++.h> 9 #ifndef WIN32 10 #define Auto "%lld" 11 #else 12 #define Auto "%I64d" 13 #endif 14 using namespace std; 15 typedef bool boolean; 16 #define ll long long 17 18 const int p = 2333; 19 20 ll n, k; 21 22 int pow2[p + 5]; 23 int C[p + 5][p + 5]; 24 int sc[p + 5][p + 5]; 25 26 inline void prepare() { 27 pow2[0] = 1; 28 for (int i = 1; i < p; i++) 29 pow2[i] = (pow2[i - 1] << 1) % p; 30 31 C[0][0] = 1; 32 for (int i = 1; i < p; i++) { 33 C[i][0] = C[i][i] = 1; 34 for (int j = 1; j < i; j++) 35 C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % p; 36 } 37 38 for (int i = 0; i < p; i++) { 39 sc[i][0] = 1; 40 for (int j = 1; j < p; j++) 41 sc[i][j] = (sc[i][j - 1] + C[i][j]) % p; 42 } 43 } 44 45 inline void init() { 46 scanf(Auto""Auto, &n, &k); 47 } 48 49 int Lucas(ll n, ll k) { 50 if (!n && !k) return 1; 51 return Lucas(n / p, k / p) * C[n % p][k % p] % p; 52 } 53 54 int S(ll n, ll k) { 55 if (k < 0) return 0; 56 return (pow2[n % p] * S(n / p, k / p - 1) + Lucas(n / p, k / p) * sc[n % p][k % p]) % p; 57 } 58 59 inline void solve() { 60 printf("%d\n", S(n, k)); 61 } 62 63 int T; 64 int main() { 65 prepare(); 66 scanf("%d", &T); 67 while (T--) { 68 init(); 69 solve(); 70 } 71 return 0; 72 }