解题报告:HDU_3944 DP? 数论
2016-07-18 21:02
477 查看
DP?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 128000/128000 K (Java/Others)Total Submission(s): 2833 Accepted Submission(s): 880
[align=left]Problem Description[/align]
![](http://acm.hdu.edu.cn/data/images/C363-1005-1.jpg)
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1 1 2
4 2 7
[align=left]Sample Output[/align]
Case #1: 0
Case #2: 5
[align=left]Author[/align]
phyxnj@UESTC
[align=left]Source[/align]
2011 Multi-University Training Contest 11 - Host by
UESTC
[align=left]Recommend[/align]
xubiao
Statistic | Submit | Discuss
|
Note
题意:在杨辉三角上,给出n,k,p。问以0,0为起点到n,k处的最小和模p的值。
分析:
step 1.
先由组合的对称性可知k与n-k的结果一样,所以这里我们只考虑k>n/2的情况:当k>n/2时,最优的线路是先斜走到k,k ,然后竖直向下走到n,k,根据组合公式
①
②
得出当k>n/2时,最优线路的总和值为(C(n+1,k+1)+k)%p,然后就转换成组合数求模问题。
step 2.
如果p是固定值,那么相信大家都会做了:打p内的阶乘表及阶乘逆元表,然后用lucas很快就能查询出结果。
虽然p不是固定的,但是打素数表可以发现1e4的范围内只有1250个素数,那么我们可以打出这些素数全部表,然后根据p的值用对应的表就行了(题目给的内存特别大)。
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; namespace prime_table{ const int MAX_N = 1e4; const int MAX_M = sqrt(MAX_N+0.5); int all=0; int pr[MAX_N/10+300]; bool isp[MAX_N+10]; inline void init(){ memset(isp,0,sizeof(isp)); for(int i=2;i<=MAX_N;i++){ if(!isp[i]){pr[all++]=i; if(i<=MAX_M) for(int j=i*i;j<=MAX_N;j+=i){ isp[j]=true; } } } // printf("all=%d\n",all); } } using namespace prime_table; namespace Combinatorial{ const int N = 10005;/** n 的最大 值 **/ int fac[N+5][1300],ifac[N+5][1300]; /**快速幂**/ int q_pow(int a,int b,int mod){ int n=1; while(b){ if(b&1){ n=(int)(1LL*n*a%mod); } a=(int)(1LL*a*a%mod); b>>=1; } return n; } /**预处理逆元**/ inline void init_QP(){ for(int i=0;i<all;i++){ fac[0][i]=ifac[0][i]=1; for(int j=1;j<=pr[i];j++){ fac[j][i]=(int)(1LL*fac[j-1][i]*j%pr[i]); ifac[j][i]=q_pow(fac[j][i],pr[i]-2,pr[i]); } } } int C(int a,int b,int id){ if(b>a || b<0)return 0; return (int)(1LL*fac[a][id]*ifac[b][id]%pr[id]*ifac[a-b][id]%pr[id]); } int lucas(int a,int b,int id){ if(!a||!b){ return 1; } return (int)(1LL*lucas(a/pr[id],b/pr[id],id)*C(a%pr[id],b%pr[id],id)%pr[id]); } } using namespace Combinatorial; int main(){ init();init_QP(); int n,k,p,t=0; while(scanf("%d%d%d",&n,&k,&p)==3){ int *id = lower_bound(pr,pr+all,p); k = max(k,n-k); printf("Case #%d: %d\n",++t,(lucas(n+1,k+1,id-pr)+k)%p); }return 0; }
相关文章推荐
- 获取二进制中1的个数
- php最基本的增删改查
- 《C#高级编程》读书笔记(十一):错误和异常
- 在windos 环境下安装
- 在centos6.8上安装使用VNC server
- ZOJ3209 Treasure Map(Dancing Links)
- jquery基本的用法
- DNS服务器可能不可用
- background-position属性用法
- Java实现原子操作的原理
- 从上往下打印二叉树
- 矩阵快速幂(NYOJ 148 fibonacci数列(二), POJ 3233 Matrix Power Series, hdu 2157 How many ways??)
- hdoj--1865 1sting
- day 1
- CodeForces 589J Cleaner Robot BFS
- 面试题:阐述Handler的实现原理
- ARC(二) ARC规则
- EventBus
- 呼叫中心架构设计
- java高级特性之集合概述 其二