hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP?

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.
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.
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.
#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定理+预处理+帕斯卡公式优化)的更多相关文章
- 组合数取模Lucas定理及快速幂取模
组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1) , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...
- 组合数取模&&Lucas定理题集
题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020 输出组合数C(n, m) mod p (1 ...
- [转]组合数取模 Lucas定理
对于C(n, m) mod p.这里的n,m,p(p为素数)都很大的情况.就不能再用C(n, m) = C(n - 1,m) + C(n - 1, m - 1)的公式递推了. 这里用到Lusac定理 ...
- hdu 3037 费马小定理+逆元除法取模+Lucas定理
组合数学推推推最后,推得要求C(n+m,m)%p 其中n,m小于10^9,p小于1^5 用Lucas定理求(Lucas定理求nm较大时的组合数) 因为p数据较小可以直接阶乘打表求逆元 求逆元时,由费马 ...
- [hdu5226]组合数求和取模(Lucas定理)
题意:给一个矩阵a,a[i][j] = C[i][j](i>=j) or 0(i < j),求(x1,y1),(x2,y2)这个子矩阵里面的所有数的和. 思路:首先问题可以转化为求(0,0 ...
- HDU 5698 大组合数取模(逆元)
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- BZOJ-1951 古代猪文 (组合数取模Lucas+中国剩余定理+拓展欧几里得+快速幂)
数论神题了吧算是 1951: [Sdoi2010]古代猪文 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1573 Solved: 650 [Submit ...
- lucas定理解决大组合数取模
LL MyPow(LL a, LL b) { LL ret = ; while (b) { ) ret = ret * a % MOD; a = a * a % MOD; b >>= ; ...
- 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 ...
随机推荐
- java_jdbc_基本连接池
MyDataSource实现封装连接池,MyConnection实现connection类,通过代理模式相互调用 package cn.itcast; import java.sql.*; publi ...
- 介绍一些实用的IOS手势识别库 (COCOS2D)
http://www.supersuraccoon-cocos2d.com/zh/2012/11/14/introduction-to-some-great-ios-gesture-recogniti ...
- 2、netlink简介
Netlink 是一种特殊的 socket,它是 Linux 所特有的,类似于 BSD 中的AF_ROUTE 但又远比它的功能强大,目前在最新的 Linux 内核(2.6.14)中使用netlink ...
- iOS开发之OCR光学识别储蓄卡以及信用卡
最近由于公司需要一个扫描银行卡获取卡号的功能,网上找了很多相关的资料,完全扫描银行卡获取卡号信息的都是价格贵的不得了的,而且仅仅只是授权而已,在此咱退而求次,找到一个可以扫描信用卡的第三方框架,给大家 ...
- 常用CentOS 6/7 扩展源
1.系统自带 baseextrasupdates 2.epel yum install epel-release https://fedoraproject.org/wiki/EPEL 3.el(用于 ...
- Python基础:1.数据类型(空、布尔类型、整型、长整型、浮点型、字符串)
提示:python版本2.7,windows系统 Python提供的基本数据类型:空.布尔类型.整型.长整型.浮点型.字符串.列表.元组.字典.日期 1.空(None) None,是一个特殊的值,不能 ...
- css布局模型之绝对定位与相对定位
1. 绝对定位,absolute 如果想为元素设置层模型中的绝对定位,需要设置position:absolute(表示绝对定位),这条语句的作用将元素从文档流中拖出来,然后使用left.right.t ...
- windows下go开发环境部署 (sublime+gosublime+geocode)
1.下载git.go和sublime免安装版 go下载地址:http://pan.baidu.com/s/1hq1mrDM#path=%252Fgo%252F1.5.2sublime下载地址:http ...
- C#集合之Hashtable
Hashtable是一个键值对集合,其泛型版本是Dictionary<K, V>,下面说下常用的一些方法; 1.Add(),向Hashtable添加元素,需要注意的是因为它是键值对集合,所 ...
- DOM4J方式解析XML文件
dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory dom4j是一个简单的开源库,用于处理XML. X ...