首页 > 其他 > 详细

hdu 3944 dp?

时间:2014-05-04 11:19:37      阅读:514      评论:0      收藏:0      [点我收藏+]

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
bubuko.com,布布扣

Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0,1,2,…and the column from left to right 0,1,2,….If using C(n,k) represents the number of row n, column k. The Yang Hui Triangle has a regular pattern as follows.
C(n,0)=C(n,n)=1 (n ≥ 0) 
C(n,k)=C(n-1,k-1)+C(n-1,k) (0<k<n)
Write a program that calculates the minimum sum of numbers passed on a route that starts at the top and ends at row n, column k. Each step can go either straight down or diagonally down to the right like figure 2.
As the answer may be very large, you only need to output the answer mod p which is a prime.
 

 

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
 
bubuko.com,布布扣
 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 }
bubuko.com,布布扣

 

hdu 3944 dp?,布布扣,bubuko.com

hdu 3944 dp?

原文:http://www.cnblogs.com/tom987690183/p/3705595.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!