標籤:
4591: [Shoi2015]超能粒子炮·改Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 178 Solved: 70
[Submit][Status][Discuss]Description曾經發明了腦洞治療儀&超能粒子炮的發明家SHTSC又公開了他的新發明:超能粒子炮·改--一種可以發射威力更加強大的粒子流的神秘裝置。超能粒子炮·改相比超能粒子炮,在威力上有了本質的提升。它有三個參數n,k。它會向編號為0到k的位置發射威力為C(n,k) mod 2333的粒子流。現在SHTSC給出了他的超能粒子炮·改的參數,讓你求其發射的粒子流的威力之和模2333。
Input第一行一個整數t。表示資料群組數。之後t行,每行二個整數n,k。含義如題面描述。k<=n<=10^18,t<=10^5
Outputt行每行一個整數,表示其粒子流的威力之和模2333的值。
Sample Input1
5 5
Sample Output32HINT Source
By 佚名上傳
題解:
Lucas定理:C(n,k)%p=(C(n/p,k/p)*C(n%p,k%p))%p (p為質數)
C(n,k)%2333=C(n/2333,k/2333)*C(n%2333,k%2333) 分兩種部分考慮:設k=k1*2333+k2 (0≤k1,k2)
1.對於k1部分C(n,0)……C(n,2332)
=C(n/2333,0)*C(n%2333,0)+C(n/2333,0)*C(n%2333,1)+……+C(n/2333,0)*C(n%2333,2332) = C(n/2333,0)*(∑C(n%2333,i)(0≤i≤2332)) ==> 2333個C(n,2333)……C(n,4665)
=C(n/2333,1)*C(n%2333,0)+C(n/2333,1)*C(n%2333,1)+……+C(n/2333,1)*C(n%2333,2332) = C(n/2333,1)*(∑C(n%2333,i)(0≤i≤2332)) ==> 2333個C(n,4666)……C(n,6998)=C(n/2333,2)*C(n%2333,0)+C(n/2333,2)*C(n%2333,1)+……+C(n/2333,2)*C(n%2333,2332) = C(n/2333,2)*(∑C(n%2333,i)(0≤i≤2332)) ==> 2333個C(n,6999)……C(n,9331)=C(n/2333,3)*C(n%2333,0)+C(n/2333,3)*C(n%2333,1)+……+C(n/2333,3)*C(n%2333,2332) = C(n/2332,3)*(∑C(n%2333,i)(0≤i≤2332)) ==> 2333個…………所以k1部分的總和sum=(∑C(n%2333,i)(0≤i≤2332))*(∑C(n/2333,j)(0≤j≤k1-1))
2.對於k2部分C(n,k1*2333)……C(n,k)=C(n/2333,k1)*C(n%2333,0)+C(n/2333,k1)*C(n%2333,1)+……+C(n/2333,k1)*C(n%2333,k%2333) ==> k%2333+1個=C(n/2333,k1)*(∑C(n%2333,i)(0≤i≤k%2333)) 由以上可得
ans=
(∑C(n%2333,i)(0≤i≤2332))*(∑C(n/2333,j)(0≤j≤k1-1))+C(n/2333,k1)*(∑C(n%2333,i)(0≤i≤k%2333)) 預先處理
S(n,k)=∑C(n,i)(0≤i≤k),化簡
ans=
S(n%2333,2332)*(∑C(n/2333,j)(0≤j≤k1-1))+C(n/2333,k1)*S(n%2333,k%2333)因為n%2333一定小於2333,所以可以用二維數組S(n,k)表示。但 ∑C(n/2333,j)(0≤j≤k1-1) 中n/2333可能很大,無法用二維數組儲存,所以不把 ∑C(n/2333,j)(0≤j≤k1-1) 化簡為 S(n/2333,k1-1)。但是可以發現 ∑C(n/2333,j)(0≤j≤k1-1) 與 要求的最終答案的公式的格式 ∑C(n,i)(0≤i≤k) 一樣,所以可以遞迴求解。另外ans中的C(n/2333,k1)可以用Lucas定理求解。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define MOD 2333 5 LL jc[MOD+10],C[MOD+10][MOD+10],S[MOD+10][MOD+10]; 6 LL read() 7 { 8 LL s=0,fh=1;char ch=getchar(); 9 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)fh=-1;ch=getchar();}10 while(ch>=‘0‘&&ch<=‘9‘){s=s*10+(ch-‘0‘);ch=getchar();}11 return s*fh;12 }13 LL mod(LL k,LL k1){return k-(k/k1)*k1;}14 void cljc()15 {16 jc[0]=1LL;17 for(int i=1;i<=MOD;i++)jc[i]=mod(jc[i-1]*i,MOD);18 }19 void clC()20 {21 int i,j;22 C[0][0]=1LL;23 for(i=1;i<=MOD;i++)24 {25 C[i][0]=C[i][i]=1LL;26 for(j=1;j<i;j++)C[i][j]=mod(C[i-1][j]+C[i-1][j-1],MOD);27 }28 for(i=0;i<=MOD;i++)29 {30 S[i][0]=1LL;31 for(j=1;j<=MOD;j++)S[i][j]=mod(S[i][j-1]+C[i][j],MOD);32 }33 }34 LL ksm(LL bb,LL pp,LL kk)35 {36 LL s=1LL;37 while(pp>0)38 {39 if(pp%2!=0)s=mod(s*bb,kk);40 pp/=2;41 bb=mod(bb*bb,kk);42 }43 return s;44 }45 LL Comb(LL n,LL m,LL p)46 {47 if(m>n)return 0LL;48 if(m>n-m)m=n-m;49 return mod(jc[n]*ksm(mod(jc[m]*jc[n-m],p),p-2,p),p);50 }51 LL Lucas(LL n,LL m,LL p)52 {53 if(m==0LL)return 1LL;54 return mod(/*Comb(mod(n,p),mod(m,p),p)*/C[n%p][m%p]*Lucas(n/p,m/p,p),p);55 }56 LL getans(LL n,LL m,LL p)57 {58 if(m<0LL)return 0LL;59 return mod(mod(S[mod(n,2333)][2332]*getans(n/2333,m/2333-1,p),p)+mod(Lucas(n/2333,m/2333,p)*S[mod(n,2333)][mod(m,2333)],p),p);60 }61 int main()62 {63 LL T,n,k;64 cljc();65 clC();66 T=read();67 while(T--)68 {69 n=read();k=read();70 printf("%lld\n",getans(n,k,MOD));71 }72 fclose(stdin);73 fclose(stdout);74 return 0;75 }
Bzoj 4591: [Shoi2015]超能粒子炮·改 數論,Lucas定理,排列組合