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. CTreeCtrl 控件总结

      一 基础操作  1 插入节点 1)插入根节点 //插入根节点 HTREEITEM hRoot; CString str=L"ROOT" hRoot=nTreeCtrl.Inse ...

  2. com.velocity.servlet

    package com.velocity.servlet; import java.io.IOException; import java.util.ArrayList; import java.ut ...

  3. [转]Oracle Stored Procedures Hello World Examples

    本文转自:http://www.mkyong.com/oracle/oracle-stored-procedures-hello-world-examples/ List of quick examp ...

  4. mongodb使用mongoose分组查询

    一个分组查询的例子: model.aggregate([{$match: ops}, {$unwind: '$details'}, {$sort: {create_at: -1}}, { $group ...

  5. php gd 生成日历图

    <?php //如果您提交了时间则显示您提交年月的日历,否则显示当前月份日历 if (isset($_GET['month']) && isset($_GET['year'])) ...

  6. JAXB - Annotations, Top-level Elements: XmlRootElement

    A class that describes an XML element that is to be a top-level element, i.e., one that can function ...

  7. 基于spark实现表的join操作

    1. 自连接 假设存在如下文件: [root@bluejoe0 ~]# cat categories.csv 1,生活用品,0 2,数码用品,1 3,手机,2 4,华为Mate7,3 每一行的格式为: ...

  8. 【C#4.0图解教程】笔记(第19章~第25章)

    第19章 泛型 1.泛型概念 泛型提供了一种更准确地使用有一种以上的类型的代码的方式. 泛型允许我们声明类型参数化的代码,我们可以用不同的类型进行实例化. 泛型不是类型,而是类型的模板.   2.声明 ...

  9. asp搜索两个以上的词的原理

    通常会在许多网站上进行搜索一些内容,要输入两个或两个以上的词,它的原理是这样的: 假设在搜索框search中输入:“asp php” 先得到输入框中的内容:search=request("s ...

  10. 关于TouchEvent中出现异常:MessageQueue-JNI问题

    Tag:MessageQueue-JNI Exception dispatching input event. Exception in MessageQueue callback: handleRe ...