標籤:
bzoj4591[Shoi2015]超能粒子炮·改
題意:
求(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
bzoj4591[Shoi2015]超能粒子炮·改