博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3944 dp?
阅读量:6847 次
发布时间:2019-06-26

本文共 2545 字,大约阅读时间需要 8 分钟。

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
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
 
1 #include
2 #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 }

 

转载于:https://www.cnblogs.com/tom987690183/p/3705595.html

你可能感兴趣的文章
【强化学习】python 实现 saras lambda 例一
查看>>
基于GPU屏幕空间的精确光学折射效果
查看>>
数据交换=>Windows_Mobile+WCF+Exchange2007 - part1
查看>>
POJ-2502 Subway
查看>>
python调用Shell脚本:os.system(cmd)或os.popen(cmd)【转】
查看>>
wifi简介
查看>>
C++默认构造函数
查看>>
margin-top失效的解决方法
查看>>
FireBug与FirePHP
查看>>
使用socket方式连接Nginx优化php-fpm性能
查看>>
JS转义 escape()、encodeURI()、encodeURIComponent()区别详解
查看>>
cocos2dx 编写shader 遇到 溢出问题
查看>>
OC与JS互相调用
查看>>
IT持续集成之质量管理
查看>>
用jquery追加的元素不能触发treeview事件
查看>>
java代码走查审查规范
查看>>
各大Oj平台介绍 刷题平台
查看>>
MyEclipse------如何连接MySQL
查看>>
uva 11270 - Tiling Dominoes(插头dp)
查看>>
[翻译] - <Entity Framework> - 直接执行数据库命令
查看>>