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. JavaBean 动作元素事例

    JavaBean.jsp JavaBeanSuccess.jsp Type类 效果

  2. nyist 596 谁是最好的Coder

    http://acm.nyist.net/JudgeOnline/problem.php?pid=596 谁是最好的Coder 时间限制:1000 ms  |  内存限制:65535 KB 难度:0 ...

  3. Python学习总结18:函数 参数篇

    1. 判断函数是否可调用 >>> import math >>> x = 1 >>> y = math.sqrt >>> cal ...

  4. hdu5322 Hope

    设dp[n]为n个数字排列时候的答案,那么可以得到dp方程 dp[n]=Σdp[n-i]*c(n-1,i-1)*(i-1)!*i^2(1<=i<=n) 然后上式可以化成卷积形式,分治FFT ...

  5. CCF真题之相反数

    201403-1 问题描述 有 N 个非零且各不相同的整数.请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数). 输入格式 第一行包含一个正整数 N.(1 ≤ N ≤ 500).   ...

  6. Android中操作数据的集中方式---文件,SQLite,ContentProvider

    http://blog.csdn.net/he90227/article/details/33734239 转

  7. Fresco源码解析 - 创建一个ImagePipeline(一)

    在Fresco源码解析 - 初始化过程分析章节中, 我们分析了Fresco的初始化过程,两个initialize方法中都用到了 ImagePipelineFactory类. ImagePipeline ...

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON HighpassImage

    zw版[转发·台湾nvp系列Delphi例程]HALCON HighpassImage unit Unit1;interfaceuses Windows, Messages, SysUtils, Va ...

  9. yii2表关联实例

    yii2表关联 1.两张表关联,以“商品表关联品牌表”为例 控制器中: $goods_model=new Goods(); $goods_info=$goods_model::find()->j ...

  10. HorizontalScrollView的配置

    package com.exmple.jinritoutiao; import java.util.ArrayList;import java.util.Calendar;import java.ut ...