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. 文件和目录之umask函数

    本篇博文内容摘自<UNIX环境高级编程>(第二版),仅作个人学习记录所用.关于本书可参考:http://www.apuebook.com/. umask函数为进程设置文件模式创建屏蔽字,并 ...

  2. Controllers

    Controllers Controllers are the bread and butter of the framework they control when a model is used ...

  3. springMVC学习笔记三

    十三.springMVC和spring集成 配置文件,spring的配置路径applicationContext.xml 在默认的web-inf下面 strut的配置文件默认在src下面 用了什么框架 ...

  4. Day03 - Python 函数

    1. 函数简介 函数是组织好的,可重复使用的,用来实现单一或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.Python提供了许多内建函数,比如print():也可以自己创建函数,这 ...

  5. Hibernate初级

    文章来源: http://blog.csdn.net/jiuqiyuliang/article/details/39078749/ 持久化(Persistence),把数据(如内存中的对象)保存到可永 ...

  6. Objective-C,复合类,Composition

     复合类 5.复合类现实中,复杂的对象都是由较小和较为简单的对象构成:由简单对象创建复杂对象的过程称作合成.合成通常使用在有has-a关系的对象:通常的基本数据类型可以满足构造简单和小的对象.为了从小 ...

  7. 【Objective-C】2.自定义构造方法和description方法

    1.Student.h 1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject { 4 int _age ...

  8. 【C语言】01-函数

    一.函数的分类 前面已经说过,C语言中的函数就是面向对象中的"方法",C语言的函数可以大概分为3类: 1.主函数,也就是main函数.每个程序中只能有一个.也必须有一个主函数.无论 ...

  9. xampp 安装后无法启动apache 的解决方法

    1,安装xampp 后,apache 无法启动,当时的报错已经没有证据了,大概的翻译就是端口 被block(锁定)的意思 2,通过 查找端口被占用,找到被占用程序,进行杀掉进程,或者卸载软件,参考网址 ...

  10. spark下统计单词频次

    写了一个简单的语句,还没有优化: scala> sc. | textFile("/etc/profile"). | flatMap((s:String)=>s.spli ...