DP?

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
 
思路:Lucas定理正好适用于p较小的组合数取模问题;由于很多组查询,所以只能预处理出每个素数对应的每个每个值的阶乘值;
Lucas定理:C(n,m)=C([n/p],[m/p]) * C(a0,b0)  (mod p);
这是我们能容易地求解出终点的值,但是如果我们是一层一层地加到边界,再找还有多少个1?这样直接T了,现在就需要帕斯卡公式还计算这个sigma和了;
帕斯卡公式:
1.  C(n+1,m) = C(n,m) + C(n-1,m-1)+...+C(n-m,0);  当m <= n/2时,斜向上走到左边界,之后还有n - m个1
2.  C(n+1,m+1) = C(n-1,m) + C(n-2,m)+...+C(n-m,m); 一直竖直向上走到右边界,之后还有m个1
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1|1
typedef pair<int,int> PII;
#define A first
#define B second
#define MK make_pair
typedef __int64 ll;
template<typename T>
void read1(T &m)
{
T x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
const int N = ;
int prime[N],check[N];
void getprime()
{
for(int i = ;i < N;i++)if(!check[i]){
prime[i] = ++prime[];
for(int j = i*i;j < N;j += i)
check[j] = ;
}
}
int f[][N];
void init()
{
getprime();
for(int i = ;i <= N;i++){
if(prime[i] == ) continue;
int id = prime[i];
f[id][] = ;
for(int j = ;j < N;j++)
f[id][j] = f[id][j-]*j%i;
}
}
int pow_mod(int a,int n,int p)
{
int ans = ;
while(n){
if(n & ) ans = ans*a%p;
a = a*a%p;
n >>= ;
}
return ans;
}
int C(int n,int m,int p)
{
if(n < m) return ;
if(n == m) return ;
int id = prime[p];
int a = f[id][n],b = f[id][m]*f[id][n - m]%p;
return a*pow_mod(b,p-,p)%p;
}
int Lucas(int n,int m,int p)
{
if(m == ) return ;
if(m == ) return n%p;
return C(n%p,m%p,p)*Lucas(n/p,m/p,p)%p;
}
int main()
{
init();
int n,m,p,kase = ;
while(scanf("%d%d%d",&n,&m,&p) == ){
if(m <= n/) m = n - m;
int ans = Lucas(n + ,m + ,p);
printf("Case #%d: %d\n",kase++,(ans + m)%p);
}
return ;
}

hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)的更多相关文章

  1. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

  2. 组合数取模&&Lucas定理题集

    题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020  输出组合数C(n, m) mod p (1 ...

  3. [转]组合数取模 Lucas定理

    对于C(n, m) mod p.这里的n,m,p(p为素数)都很大的情况.就不能再用C(n, m) = C(n - 1,m) + C(n - 1, m - 1)的公式递推了. 这里用到Lusac定理 ...

  4. hdu 3037 费马小定理+逆元除法取模+Lucas定理

    组合数学推推推最后,推得要求C(n+m,m)%p 其中n,m小于10^9,p小于1^5 用Lucas定理求(Lucas定理求nm较大时的组合数) 因为p数据较小可以直接阶乘打表求逆元 求逆元时,由费马 ...

  5. [hdu5226]组合数求和取模(Lucas定理)

    题意:给一个矩阵a,a[i][j] = C[i][j](i>=j) or 0(i < j),求(x1,y1),(x2,y2)这个子矩阵里面的所有数的和. 思路:首先问题可以转化为求(0,0 ...

  6. HDU 5698 大组合数取模(逆元)

    瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  7. BZOJ-1951 古代猪文 (组合数取模Lucas+中国剩余定理+拓展欧几里得+快速幂)

    数论神题了吧算是 1951: [Sdoi2010]古代猪文 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1573 Solved: 650 [Submit ...

  8. lucas定理解决大组合数取模

    LL MyPow(LL a, LL b) { LL ret = ; while (b) { ) ret = ret * a % MOD; a = a * a % MOD; b >>= ; ...

  9. 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)

    J. Ceizenpok’s formula time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

随机推荐

  1. android的界面编程

    主要是用View以及ViewGroup,同时ViewGroup又是View的子类,充当容器. 主要有两种方法控制view的行为: 1.在XML布局文件中通过XML属性进行控制 2.在Java代码中通过 ...

  2. com.domain.bean

    package com.domain.bean; import java.util.LinkedHashMap; import java.util.Map; public class TmpBean ...

  3. java_JdbcUtilis_单实例

    //eg1,没有使用单实例,eg2有 package cn.itcast; import java.sql.Connection; import java.sql.DriverManager; imp ...

  4. Best Practice of cross-platform games

    __super keyword this keyworld is offered only by Microsoft VC. So you had to call the very name of p ...

  5. Authentication

    Authentication Introduction Configuration Storing Passwords Authenticating Users Basic Usage Introdu ...

  6. Windows系统下用命令行编译C/C++程序过程总结

    转自:http://www.cnblogs.com/caikehe/archive/2013/01/12/2858017.html (1)先用记事本编写如下所示的代码,并另存为hello.cpp,假设 ...

  7. 用jquery-easyui中的combotree实现树形结构的选择

    用jquery-easyui中的combotree实现树形结构的选择 需求:实现一个树形节点的选择,要求默认父节点都折叠,父节点前的checkbox不显示,子节点显示checkbox,且父节点不可选择 ...

  8. 新建的linux虚拟机找不到eth0解决办法

    新建的linux虚拟机通过 vi /etc/sysconfig/network-scripts/ifcfg-eth0进行配置: 再用ifconfig查看ip竟然没有eth0,只有lo和virbr0 其 ...

  9. PHP 实现对象的持久层,数据库使用MySQL

    http://www.xuebuyuan.com/1236808.html 心血来潮,做了一下PHP的对象到数据库的简单持久层. 不常用PHP,对PHP也不熟,关于PHP反射的大部分内容都是现学的. ...

  10. Mysql_存储功能

    先上一段代码: -->DELIMETER;     ----加上这一句:DELIMETER的作用是设定客户机的分隔符,表示用//包含的是一段程序,一起执行,而不是见到“:”就执行 结束的时候写上 ...