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

题意:给个n,k,问从第一行第一列走到第n行,第k列的最小路径点权和是多少? 只能向下走或者斜右下走。输出对p取模。

分析:

我们思考逆问题,从n,k走向1,1只能向上或者左上走。

容易看出,根据已知的那个点(n,m) 如果 n > 2*m 那么从已知点出发,可以一直往斜的方向走,直到边界,那么 权值和就为 C(n,m)+C(n-1,m-1)....... 由帕斯卡公式可得该式等于 C(n+1,m)+(n-m)  如果n <= 2*m,那么就是一直往上走,权值和就为C(n,m)+C(n-1,m)+C(n-2,m)..... 等于C(n+1,m+1)+m

得到公式之后因为n,m很大,所以需要用Lucas定理化简,而且样例有1e5组,因此要先将素数的一些组合数打好表。

代码:

#include<stdio.h>
#include<string.h>
typedef long long ll;
int book[10000] ={1,1,0};
int prim[10000],pnum = 0;
int preC[1300][10001],preA[1300][10001];
int s[2001];
void gcd(int a,int b,int &d,int &x,int &y){if(!b){d = a;x = 1;y = 0;}else{gcd(b,a%b,d,y,x);y -= x*(a/b);}
}
inline int inv(int a,int p){int d,x,y;gcd(a,p,d,x,y);return (d==1)?(x+p)%p:-1;
}
void init()
{for(int i = 2 ; i < 10000 ; i ++){if(book[i])continue;book[i] = pnum;prim[pnum++] = i;for(int j = 2 ; j * i < 10000 ; j ++)book[i*j]=1;}for(int i = 0 ; i < pnum ; i ++){preC[i][1] = 1;preA[i][1] = 1;for(int j = 2 ; j < 10001 ; j ++)preC[i][j] = preC[i][j-1] * inv(j,prim[i]) % prim[i];for(int j = 2 ; j < 10001 ; j ++)preA[i][j] = preA[i][j-1] * j % prim[i];}
}
ll C(ll n,ll m,ll mod)
{if(m>n) return 0;if(m==n||m==0)return 1;if(n==m+1||m==1)return n%mod;return preA[book[mod]][n]*preC[book[mod]][m]%mod*preC[book[mod]][n-m]%mod;
}
ll lucas(ll n,ll m,ll mod)
{if(m == 0) return 1;return C(n%mod,m%mod,mod)*lucas(n/mod,m/mod,mod)%mod;
}int main()
{init();int n,m,q,_case=0;while(~scanf("%d%d%d",&n,&m,&q)){printf("Case #%d: %lld\n",++_case,2*m<n?(lucas(n+1,m,q)+n-m)%q:(lucas(n+1,m+1,q)+m)%q);}return 0;
}

帕斯卡公式+Lucas定理______DP?( hdu 3944 )相关推荐

  1. hdu 3944 DP? (Lucas 定理)

    仔细观察杨辉三角后可以发现从最高点到第n行第k个数的最短路为c(n+1,k); 根据Lucas定理可以求出,一般来说要求答案模去一个质数p且p的范围不大于10^5则可用Lucas. Lucas(n,m ...

  2. HDU 5226 Tom and matrix(组合数学+Lucas定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5226 题意:给一个矩阵a,a[i][j] = C(i,j)(i>=j) or 0(i < ...

  3. 组合数(Lucas定理) + 快速幂 --- HDU 5226 Tom and matrix

    Tom and matrix Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5226 Mean: 题意很简单,略. analy ...

  4. hdu 3037 Lucas定理

    题目可以转换成 x1+x2+--+xn=m 有多少组解,m在题中可以取0-m. x1+x2+...+xn = m的解的个数,利用插板法可以得到方案数为: (m+1)*(m+2)...(m+n-1) = ...

  5. Lucas定理及组合数取模

    首先给出这个Lucas定理: A.B是非负整数,p是质数.AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]. 则组合数C(A,B)与C(a[n],b[n] ...

  6. Lucas定理与大组合数的取模的求法总结

    Lucas定理与大组合数的取模的求法总结 分类: ACMer 数学 2012-03-11 09:38  1219人阅读  评论(0)  收藏  举报 c 首先给出这个Lucas定理: A.B是非负整数 ...

  7. 洛谷P3773 [CTSC2017]吉夫特(Lucas定理,dp)

    题意 满足$b_1 < b_2 < \dots < b_k$且$a_{b_1} \geqslant a_{b_2} \geqslant \dots \geqslant a_{b_k} ...

  8. [HDU3037]Saving Beans,插板法+lucas定理

    [基本解题思路] 将n个相同的元素排成一行,n个元素之间出现了(n-1)个空档,现在我们用(m-1)个"档板"插入(n-1)个空档中,就把n个元素隔成有序的m份,每个组依次按组序号 ...

  9. BZOJ4737 组合数问题 【Lucas定理 + 数位dp】

    题目 组合数C(n,m)表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3)三个物品中选择两个物品可以有( 1,2),(1,3),(2,3)这三种选择方法.根据组合数的定义,我们可以给 ...

最新文章

  1. linux内核实验平台搭建,搭建自己的Linux实验系统(一)
  2. 【我的项目经验】——Visual Studio 插件
  3. String到底是值类型还是引用类型(C#)
  4. 安装nagios_grapher,有图有真相。
  5. linux录制声卡声音_不是你唱歌难听,选对麦克风和声卡了吗?
  6. 【转载】谁动了摩卡的奶酪?
  7. Wireshake抓包数据怎么看(一)
  8. SpringMVC的RESTful(二)定制格式
  9. python安装菜鸟教程_Python菜鸟教程 | 多平台安装
  10. python分析每月销售数据_如何用Python分析销售数据
  11. 上海计算机学业水平考试,上海信息科技学业水平考试复习资料整理——计算机系统.pdf...
  12. 华为开源数据库openGauss
  13. Loadrunner11
  14. switch语句的执行顺序
  15. 收入--支出=储蓄?
  16. C++中cout的使用
  17. css3恐龙蛋孵化动画代码
  18. Oracle公有云上的ADG配置(单实例)
  19. python制作图片数据集 h5py_基于h5py的使用及数据封装代码
  20. dataframe中将第一列放到最后一列

热门文章

  1. 蓝桥杯2020年填空题既约分数
  2. 小型电子商务网站设计原则
  3. 【Python学习笔记(一)—— 初识Python】
  4. 夜游项目如何挖掘景区独具特色文化
  5. PCB做的 东南大学 校徽
  6. 意外的惊喜:f.lux, 一款真的很强大的护眼软件
  7. 生物信息学python脚本_Python生物信息学数据管理
  8. 中国政府融资平台态势分析及发展前景规划评估研究报告2022-2028年版
  9. 计算机系的信息与计算科学考研方向,信息与计算科学专业考研方向和考试科目有哪些...
  10. Spring开篇介绍-如果没有Spring如何对外暴露一个接口