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. App字体大小不随系统改变而改变
  2. struct 数组
  3. Python实训day10am【Python中的地址引用、os模块】
  4. python基础-元组
  5. Qt无法用UTF-8编辑问题
  6. asp.net web常用控件FileUpload(文件上传控件)
  7. pe下找不到ssd硬盘_【进入pe系统后认不到硬盘解决方法】进入pe系统看不到硬盘_pe系统不认硬盘...
  8. 简述 maven 命令 package、install、deploy 的区别
  9. RISC-V正在采取行动,避免MIPS类的碎片化
  10. 论文学习19-Structured prediction models for RNN based sequence labeling in clinical text(LSTM_CRF,2016)
  11. java skip_Java CharArrayReader skip()方法
  12. SAP License:SAP用户权限
  13. 解决miner.start() 返回null
  14. OSPF系列小实验之6:网络类型对邻居关系及路由学习的影响
  15. Atitit db model 数据库快速建模法 开发效率 目录 1. 结构(数据)设计 行为(处理)设计: 1 2. 业务建模阶段 1 2.1. Ui建模法,根据表单字段建立表字段 2 2.2.
  16. 训练集与测试集数据分布不一致
  17. 无线通信行业常用名词
  18. 中国各省份矢量地图-可编辑
  19. 推送环境搭建基于开源MPush
  20. 计算机驱动程序属于系统还是软件,什么是VGA驱动程序?

热门文章

  1. AspnetBoilerplate (ABP) Organization Units 组织结构管理
  2. 为什么PHP在很多公司遭到弃用?
  3. android之SDK
  4. Xcon2014 Geekpwn2014
  5. Doodle era
  6. 文本溢出及其在angular中失效的bug
  7. 电商秒杀系统设计分析
  8. Element UI表格行拖拽功能
  9. No6-6.从零搭建spring-cloud-alibaba微服务框架,添加用户鉴权逻辑,动态数据权限(使用AOP实现)等(六,no6-6)
  10. python神经网络编程 pdf下载_Python神经网络编程 PDF 高质量完整版