bzoj 4591: [Shoi2015]超能粒子炮·改 [lucas定理]
4591: [Shoi2015]超能粒子炮·改
题意:多组询问,求
\[S(n, k) = \sum_{i=0}^n \binom{n}{i} \mod 2333,\ k \le n \le 10^{18}
\]
lucas定理,展开一层然后整除分块一下,不完整的块单独拿出来,就是
\[S(n,k) = S(\frac{n}{p}, \frac{k}{p}-1)S(n \bmod p, p-1) + \binom{\frac{n}{p}}{ \frac{k}{p}} S(n \bmod p, k \bmod p)
\]
预处理\(n,k \le 2333\)的
单次询问复杂度log^2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 2500, P = 2333;
inline ll read() {
char c=getchar(); ll x=0,f=1;
while(c<'0'||c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
ll n, k;
int c[N][N], s[N][N];
inline ll lucas(ll n, ll m) {
if(n < m) return 0;
ll ans = 1;
for(; m; n /= P, m /= P) ans = ans * c[n % P][m % P] %P;
return ans;
}
ll S(ll n, ll k) {
if(n <= P && k <= P) return s[n][k];
ll ans = (S(n / P, k / P - 1) * S(n % P, P - 1) + lucas(n / P, k / P) * S(n % P, k % P)) %P;
return ans;
}
int main() {
freopen("in", "r", stdin);
int T = read();
c[0][0] = 1;
for(int i=1; i<=P; i++) {
c[i][0] = 1;
for(int j=1; j<=i; j++) c[i][j] = (c[i-1][j] + c[i-1][j-1]) %P;
}
for(int i=0; i<=P; i++) {
s[i][0] = c[i][0];
for(int j=1; j<=P; j++) s[i][j] = (s[i][j-1] + c[i][j]) %P;
}
while(T--) {
n = read(); k = read();
printf("%lld\n", S(n, k));
}
}
Copyright:http://www.cnblogs.com/candy99/