DP?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 128000/128000 K (Java/Others)Total Submission(s): 1804 Accepted Submission(s): 595
Problem Description
Input
Input to the problem will consists of series of up to 100000 data sets. For each data there is a line contains three integers n, k(0<=k<=n<10^9) p(p<10^4 and p is a prime) . Input is terminated by end-of-file.
Output
For every test case, you should output "Case #C: " first, where C indicates the case number and starts at 1.Then output the minimum sum mod p.
Sample Input
1 1 2
4 2 7
Sample Output
Case #1: 0
Case #2: 5
Author
phyxnj@UESTC
Source
1 #include2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 typedef __int64 LL; 8 vector 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 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 }