标签:
题意:
求(sigma(i,0,k)C(n,i))%2333。n,k≤1018
题解:
根据Lucas定理(我不会),C(n,k)%2333=C(n/2333,k/2333)*C(n%2333,k%2333),故可以进行一些化简(把模省去了)
(sigma(i,0,k)C(n,i))=sigma(i,0,k)C(n/2333,i/2333)*C(n%2333,i%2333)
=sigma(i,0,k/2333-1)C(n/2333,i)*(sigma(j,0,2332)C(n%2333,j))+C(n/2333,k/2333)*sigma(i,0,k%2333)C(n%2333,i)
一开始先递推出i,j≤2333的C[i][j]和sm[i][j](表示sigma(k,0,j)C[i][k]),然后上式橙色部分可以看做红色部分的简化部分,递归求解直到n,k的范围≤2333就return sm[n][k],同时上式中蓝色部分也可以用Lucas定理递归求解到n,k范围≤2333return C[n][k]。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define inc(i,j,k) for(int i=j;i<=k;i++) 5 #define ll long long 6 #define maxn 2334 7 #define mod 2333 8 using namespace std; 9 10 inline ll read(){ 11 char ch=getchar(); ll f=1,x=0; 12 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1; ch=getchar();} 13 while(ch>=‘0‘&&ch<=‘9‘)x=x*10+ch-‘0‘,ch=getchar(); 14 return f*x; 15 } 16 ll n,k,c[maxn][maxn],sm[maxn][maxn]; int t; 17 void calc(){ 18 c[0][0]=sm[0][0]=1; inc(i,1,mod)sm[0][i]=1; 19 inc(i,1,mod){ 20 c[i][0]=sm[i][0]=1; 21 inc(j,1,i)c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod; 22 inc(j,1,mod)sm[i][j]=(sm[i][j-1]+c[i][j])%mod; 23 } 24 } 25 ll solvec(ll n,ll k){ 26 if(n<=mod&&k<=mod)return c[n][k];else return solvec(n/mod,k/mod)*c[n%mod][k%mod]%mod; 27 } 28 ll solvesm(ll n,ll k){ 29 if(k<mod)return solvec(n/mod,k/mod)*sm[n%mod][k%mod]%mod; 30 return (solvesm(n/mod,k/mod-1)*sm[n%mod][mod-1]%mod+solvec(n/mod,k/mod)*sm[n%mod][k%mod]%mod)%mod; 31 } 32 int main(){ 33 t=read(); calc(); 34 inc(i,1,t){n=read(); k=read(); printf("%lld\n",solvesm(n,k)%mod);} 35 return 0; 36 }
20160724
标签:
原文地址:http://www.cnblogs.com/YuanZiming/p/5701689.html