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. nyist 596 谁是最好的Coder

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

  2. 响应式框架pure--来自雅虎

    梦想还是要有  http://www.purecss.org/ 

  3. Vim篇

    Vim编辑器中的一些常用命令: 1:shift+* , 选取光标所在处的整个字符,并查找.(十分方便),快捷键gd 2:set nu , 显示各行行号,使得基于行的命令更方便. 3:shift+% , ...

  4. PHP导出CSV文件

    经常会碰到需要从数据库中导出数据到Excel文件,用一些开源的类库,比如PHPExcel,确实比较容易实现,但对大量数据的支持很不好,很容易到达PHP内存使用上限.这里的方法是利用fputcsv写CS ...

  5. 4项技巧使你不再为PHP中文编码苦恼

    PHP程序设计中中文编码问题曾经困扰很多人,导致这个问题的原因其实很简单,每个国家(或区域)都规定了计算机信息交换用的字符编码集,如美国的扩展 ASCII 码,中国的 GB2312-80,日本的 JI ...

  6. IO细述

    Java IO1:IO和File IO 大多数的应用程序都要与外部设备进行数据交换,最常见的外部设备包含磁盘和网络.IO就是指应用程序对这些设备的数据输入与输出,Java语言定义了许多类专门负责各种方 ...

  7. 什么是BI(Business Intelligence)【转】

    谈谈对BI的理解,从BI的定义.基本技术.专业名词.实例应用及扩展等方面进行重新描述,巩固对BI的理解. 一.BI的定义 BI是Business Intelligence的英文缩写,中文解释为商务智能 ...

  8. Linux USB摄像头驱动【转】

    本文转载自:http://www.itdadao.com/articles/c15a509940p0.html 在 cortex-a8 中,可接入摄像头的接口通常可以分为两种, CAMERA 接口和 ...

  9. 对EJB返回的AaaryList显示到table的处理方法

      1. ArrayList --> Object[]        ArrayList x = new ArrayList();        int i = x.size();        ...

  10. 使用SQLServer Profiler侦测死锁(转)

    准备工作: 为了侦测死锁,我们需要先模拟死锁.本例将使用两个不同的会话创建两个事务. 步骤: 1. 打开SQLServer Profiler 2. 选择[新建跟踪],连到实例. 3. 然后选择[空白] ...