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
 
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std;
typedef __int64 LL;
vector<LL> dp[];
bool s[];
void init()
{
LL i,p,j;
memset(s,false,sizeof(s));
for(i=;i<=;i++){
if(s[i]==false)
for(j=i*;j<=;j=j+i)
s[j]=true;
}
s[]=true;
for(i=;i<;i++) dp[i].clear();
for(p=;p<;p++)
{
if(s[p]==true)continue;
dp[p].push_back();
for(i=;i<=p;i++)
{
dp[p].push_back((dp[p][i-]*i)%p);
}
}
}
LL pow_mod(LL a,LL n,LL p)
{
LL ans=;
while(n){
if(n&) ans=(ans*a)%p;
n=n>>;
a=(a*a)%p;
}
return ans;
}
LL C(LL a,LL b,LL p)
{
if(a<b)return ;
if(a==b) return ;
if(b>a-b) b=a-b;
LL sum1,sum2;
sum1=dp[p][a];
sum2=(dp[p][b]*dp[p][a-b])%p;
LL ans=(sum1*pow_mod(sum2,p-,p))%p;
return ans;
}
LL Lucas(LL n,LL m,LL p)
{
LL ans=;
while(n&&m&&p){
ans=(ans*C(n%p,m%p,p))%p;
n=n/p;
m=m/p;
}
return ans;
}
int main()
{
init();
LL n,k,p;
int t=;
while(scanf("%I64d%I64d%I64d",&n,&k,&p)>){
printf("Case #%d: ",++t);
if(k>n-k) k=n-k;
LL ans=Lucas(n+,k,p);
printf("%I64d\n",(ans+(n-k))%p);
}
return ;
}

hdu 3944 dp?的更多相关文章

  1. hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)

    DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...

  2. HDU 3944 DP? [Lucas定理 诡异的预处理]

    DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)Total Subm ...

  3. HDU 3944 DP? (Lucas定理)

    题意:在杨辉三角中让你从最上面到 第 n 行,第 m 列所经过的元素之和最小,只能斜向下或者直向下走. 析:很容易知道,如果 m 在n的左半部分,那么就先从 (n, m)向左,再直着向上,如果是在右半 ...

  4. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  5. HDU 5928 DP 凸包graham

    给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...

  6. HDU 1069 dp最长递增子序列

    B - Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  7. HDU 1160 DP最长子序列

    G - FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  8. hdu 4826(dp + 记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...

  9. HDU 2861 (DP+打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2861 题目大意:n个位置,m个人,分成k段,统计分法.S(n)=∑nk=0CknFibonacci(k ...

随机推荐

  1. hunnu 修路

    ing········ 这题我一眼就想到二分修路的长度 可是还有一个问题,有个费用,如果没有的话就所有的边都连起来判断能否二分到最小可行的 可是,有费用... 怎么做呢... ... 有了费用后,就不 ...

  2. ASP.NET的一般处理程序对图片文件的基本操作

    以一个小项目为例: 验证码: public class VerifyCodeHelper { public VerifyCodeHelper() { this.ran = new Random(); ...

  3. poj 1179 Polygon

    http://poj.org/problem?id=1179 Polygon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  4. Aptana Studio3开发Python和Ruby(最佳工具)

    即从: http://d1iwq2e2xrohf.cloudfront.net/tools/studio/standalone/3.3.1.201212171919/win/Aptana_Studio ...

  5. C++之路起航——标准模板库(list)

    list(链表):http://baike.baidu.com/link?url=gkVdBlHEzy6ssrgT5Iy2wze4jl37ka1G45TRpUHrQSYFZQg2HimtUCePV0t ...

  6. linux环境变量与本地变量

    两者不同的是. 环境变量可以在shell的子进程中使用, 而本地变量不同. 每当连接上服务器时,服务器就会通过帐号密码运行一个SHELL,我们所做的工作都在这个SHELL上,特殊方法除外(如,守护进程 ...

  7. 更改EBS APPS 密码流程

    更改EBS APPS 密码流程 (更改完后重启APP 和DB)2008-1-4 在EBS11.5.10.2环境应用成功!  参考metalink Note:160337.1 How To Manual ...

  8. FTP小教程

    1.下载文件:http://pan.baidu.com/s/1gd3Uo63 2. 右键点击“传送”,就会把本地的文件传送到服务器现在打开的目录

  9. 文字处理控件TX Text Control X10独家揭秘(一):数据源自动处理

    TX Text Control即将发布的X10版本,将升级重点还是放到了其比较优势的流式布局报表设计和生成上.慧都获得了来自其开发商Text Control GmbH公司的一手资料,迫不及待的为大家带 ...

  10. Creater中选择一行的方法

    1.  在表布局中增加一单选钮列,给单选钮的属性name任意设定一个值.2.  选择单选钮对应列,将其selectID设为单选钮的ID;将onclick设为setTimeout('initAllRow ...