标签:des style blog class code java
Time Limit: 10000/3000 MS
(Java/Others) Memory Limit: 128000/128000 K
(Java/Others)
Total Submission(s): 1804 Accepted
Submission(s): 595
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 #include<vector> 6 using namespace std; 7 typedef __int64 LL; 8 vector<LL> dp[10000]; 9 bool s[10001]; 10 void init() 11 { 12 LL i,p,j; 13 memset(s,false,sizeof(s)); 14 for(i=2;i<=10000;i++){ 15 if(s[i]==false) 16 for(j=i*2;j<=10000;j=j+i) 17 s[j]=true; 18 } 19 s[1]=true; 20 for(i=0;i<10000;i++) dp[i].clear(); 21 for(p=1;p<10000;p++) 22 { 23 if(s[p]==true)continue; 24 dp[p].push_back(1); 25 for(i=1;i<=p;i++) 26 { 27 dp[p].push_back((dp[p][i-1]*i)%p); 28 } 29 } 30 } 31 LL pow_mod(LL a,LL n,LL p) 32 { 33 LL ans=1; 34 while(n){ 35 if(n&1) ans=(ans*a)%p; 36 n=n>>1; 37 a=(a*a)%p; 38 } 39 return ans; 40 } 41 LL C(LL a,LL b,LL p) 42 { 43 if(a<b)return 0; 44 if(a==b) return 1; 45 if(b>a-b) b=a-b; 46 LL sum1,sum2; 47 sum1=dp[p][a]; 48 sum2=(dp[p][b]*dp[p][a-b])%p; 49 LL ans=(sum1*pow_mod(sum2,p-2,p))%p; 50 return ans; 51 } 52 LL Lucas(LL n,LL m,LL p) 53 { 54 LL ans=1; 55 while(n&&m&&p){ 56 ans=(ans*C(n%p,m%p,p))%p; 57 n=n/p; 58 m=m/p; 59 } 60 return ans; 61 } 62 int main() 63 { 64 init(); 65 LL n,k,p; 66 int t=0; 67 while(scanf("%I64d%I64d%I64d",&n,&k,&p)>0){ 68 printf("Case #%d: ",++t); 69 if(k>n-k) k=n-k; 70 LL ans=Lucas(n+1,k,p); 71 printf("%I64d\n",(ans+(n-k))%p); 72 } 73 return 0; 74 }
标签:des style blog class code java
原文地址:http://www.cnblogs.com/tom987690183/p/3705595.html