hdu 3944 dp?
DP?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 128000/128000 K (Java/Others)
Total Submission(s): 1804 Accepted Submission(s): 595

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.
#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?的更多相关文章
- hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...
- HDU 3944 DP? [Lucas定理 诡异的预处理]
DP? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 128000/128000 K (Java/Others)Total Subm ...
- HDU 3944 DP? (Lucas定理)
题意:在杨辉三角中让你从最上面到 第 n 行,第 m 列所经过的元素之和最小,只能斜向下或者直向下走. 析:很容易知道,如果 m 在n的左半部分,那么就先从 (n, m)向左,再直着向上,如果是在右半 ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 5928 DP 凸包graham
给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...
- HDU 1069 dp最长递增子序列
B - Monkey and Banana Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDU 1160 DP最长子序列
G - FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- hdu 4826(dp + 记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...
- HDU 2861 (DP+打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2861 题目大意:n个位置,m个人,分成k段,统计分法.S(n)=∑nk=0CknFibonacci(k ...
随机推荐
- 转:python webdriver API 之 获取对象的属性
获取测试对象的属性能够帮我们更好的进行对象的定位.比如页面上有很多标签为 input 元素,而我们需要定位其中 1 个有具有 data-node 属性不一样的元素.由于 webdriver 是不支持直 ...
- Java SE series:1. environment configure and Hello world! [We use compiler and packager to create an application!]
1. cli (command line interface) and gui (graphic user interface) use javahome path, search classpath ...
- 数据库SQL 查询
查询 1.简单查询 select * from info(表名) --查所有数据 select code(列名),name(列名) from 表名 --查指定列的数据 selec ...
- linux第5天 socket api
IPv4套接口地址结构通常也称为“网际套接字地址结构”,它以“sockaddr_in”命名,定义在头文件<netinet/in.h>中 通用地址结构用来指定与套接字关联的地址.以socka ...
- java中创建线程的几种方法及区别
1,实现Runnable接口创建线程 特点: A:将代码和数据分开,形成清晰的模型 B:线程体run()方法所在的类可以从其它类中继承一些有用的属性和方法 C:有利于保持程序风格的一致性 2,继承Th ...
- 关于设置oracle中系统编号SYSID自动编号的问题;
http://liye9801.blog.163.com/blog/static/601970320086210039591/ 如何在oracle里设置自动编号列 2008-07-21 12:00:3 ...
- 【linux】xrander/cvt自定义分辨率
今天在虚拟机上装了一个LUbuntux64(12.10)玩,安装的时候,由于主板默认是没有开虚拟化支持,报错,改后相当的顺利.但是进入系统后,屏幕显示分辨率为800X600的,全屏的话,在大显示器上显 ...
- google pinyin elmentary os
sudo apt-get install software-properties-common for ppa. I have been using Sun Pinyin for quite a lo ...
- Elasticsearch多索引
在Elasticsearch中,一般的查询都支持多索引.只有文档API或者别名API等不支持多索引操作,因此本篇就翻译一下多索引相关的内容. 首先,先插入几条数据: $ curl -XPOST lo ...
- linux中的优先搜索树的实现--prio_tree【转】
转自:http://blog.csdn.net/bailyzheng/article/details/8041943 linux中的优先搜索树的实现--prio_tree prio_tree在linu ...