Saving Beans http://acm.hdu.edu.cn/showproblem.php?pid=3037

 #include<cstdio>
typedef __int64 LL;
const int M=;
class LUCAS { //lucas求组合数C(n,k)%p
LL F[M];
LL inv(LL a,LL mod) {
if(a==) return ;
return inv(mod%a,mod)*(mod-mod/a)%mod;
}
void init(LL p) {
F[]=;
for(int i=; i<=p; i++) {
F[i]=F[i-]*i%p;
}
}
LL Lucas(LL n,LL k,LL p) {
LL ans=;
while(n&&k) {
LL a=n%p;
LL b=k%p;
if(a<b) return ;
ans=ans*F[a]%p*inv(F[b]*F[a-b]%p,p)%p;
n/=p;
k/=p;
}
return ans;
}
public:
LL solve(LL n,LL k,LL p){
init(p);
return Lucas(n,k,p);
}
}gx;
int main() {
int t,n,m,p;
while(~scanf("%d",&t)) {
while(t--) {
scanf("%d%d%d",&n,&m,&p);
printf("%d\n",(int)gx.solve(n+m,n,p));
}
}
return ;
}

DP? http://acm.hdu.edu.cn/showproblem.php?pid=3944

 #include<cstdio>
#include<cstring>
#include<vector>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef __int64 LL;
const int M=;
class LUCASM { //比较适合n,k<=10^9,p<=10^4的情况
int flag[M*],prime[],pcnt;
vector<int> rev[M],fac[M];
int quickpow(int a,int b,int c) { //快速幂求(a^b)%c
int ret=%c;
for(; b; a=a*a%c,b>>=) {
if(b&) {
ret=ret*a%c;
}
}
return ret;
}
public:
void init() {//先初始化一次即可
pcnt=;
mt(flag,-);
for(int i=; i<=; i++) {
if(flag[i]) {
prime[pcnt++]=i;
rev[i].clear();
fac[i].clear();
}
for(int j=; j<pcnt&&prime[j]<=/i; j++) {
flag[i*prime[j]]=;
if(!(i%prime[j])) break;
}
}
for(int i=; i<pcnt; i++) {
int tnum=;
rev[prime[i]].push_back();
fac[prime[i]].push_back();
for (int j=; j<prime[i]; j++) {
tnum=(tnum*j)%prime[i];
int now=quickpow(tnum,prime[i]-,prime[i]);
fac[prime[i]].push_back(tnum);
rev[prime[i]].push_back(now);
}
}
}
int lucas(int n,int k,int p) {
int ret=;
while (n && k) {
int num1=n%p;
int num2=k%p;
n/=p;
k/=p;
if (num1<num2) return ;
int num=(fac[p][num1]*rev[p][num2])%p;//计算c(num1,num2)%p
num=(num*rev[p][num1-num2])%p;
ret=(ret*num)%p;
}
return ret;
}
} gx;
int main() {
int n,k,p,cas=;
gx.init();
while(~scanf("%d%d%d",&n,&k,&p)) {
printf("Case #%d: ",cas++);
if(k>n/) k=n-k;
int o=gx.lucas(n+,k,p);
printf("%d\n",(n-k+o)%p);
}
return ;
}

end

lucas求组合数C(n,k)%p的更多相关文章

  1. 1067 - Combinations---LightOj(Lucas求组合数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1067 模板求C(n,m)%p, Lucas模板; #include <iostr ...

  2. 求组合数 C++程序

    一 递归求组合数 设函数为void    comb(int m,int k)为找出从自然数1.2.... .m中任取k个数的所有组合. 分析:当组合的第一个数字选定时,其后的数字是从余下的m-1个数中 ...

  3. HDU 5698——瞬间移动——————【逆元求组合数】

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

  4. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  5. URAL 1994 The Emperor's plan 求组合数 大数用log+exp处理

    URAL 1994 The Emperor's plan 求组合数 大数用log #include<functional> #include<algorithm> #inclu ...

  6. HDU 5852 Intersection is not allowed!(LGV定理行列式求组合数)题解

    题意:有K个棋子在一个大小为N×N的棋盘.一开始,它们都在棋盘的顶端,它们起始的位置是 (1,a1),(1,a2),...,(1,ak) ,它们的目的地是 (n,b1),(n,b2),...,(n,b ...

  7. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】

    任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...

  8. 51nod1119(除法取模/费马小定理求组合数)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 题意:中文题诶- 思路:这题数据比较大直接暴力肯定是不 ...

  9. [2011山东ACM省赛] Binomial Coeffcients(求组合数)

    Binomial Coeffcients nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...

随机推荐

  1. 第六十九篇、OC_录制语音和播放语音功能的实现

    录制: 1.设置全局属性 NSURL *recordedFile;//存放路径 AVAudioPlayer *player;//播放 AVAudioRecorder *recorder;//录制 NS ...

  2. UI3_视图切换

    // // ViewController.m // UI3_视图切换 // // Created by zhangxueming on 15/7/3. // Copyright (c) 2015年 z ...

  3. PowerDesigner 工具面板 association,inheritance,association link 不可用 解决方法

    PowerDesigner 画E-R 图,关联,继承及关联实体连接不可用. 如图: 解决方法: tools->modeloptions->Notation 改为"E/R+Meri ...

  4. bzoj 1006: [HNOI2008]神奇的国度

    这是个标准的弦图,但如果不知道弦图就惨了=_= 趁着这个机会了解了一下弦图,主要就是完美消除序列,求出了这个就可以根据序列进行贪心染色. 貌似这个序列很神,但是具体应用不了解…… 这道题为什么可以这么 ...

  5. NodeJs菜鸟初始

    我们先来了解下什么是nodejs 一.nodejs具有事件驱动.异步编程的特点. 事件驱动这个词并不陌生,在某些传统语言的网络编程中,我们会用到回调函数,比如当socket资源达到某种状态时,注册的回 ...

  6. git管理和自动部署项目

    当一个项目需要纳入到版本控制的时候,选择的工具还是比较多的,最常见的就是工具有CVS,SVN,GIT等.在平时的开发中视情况而定,从来就没有最好的版本控制工具,只有最适合的工具.在这里我习惯用git来 ...

  7. kdbchk: the amount of space used is not equal to block size

    一.对数据文件检查 注意:应该在关闭数据库模式下进行bbed的操作 [oracle@ora10 controlfile]$ dbv file=/u01/app/oracle/oradata/ORCL/ ...

  8. Lambda(2)

    Lambda表达式是匿名方法的超集,处理匿名方法有的功能外,还有其他的功能: 1.能够推测出参数的类型,无需显示声明 2.支持语句块和表达式作为方法体 Lambda表达式的书写方式: Lambda表达 ...

  9. HTTP传递数据的几种方法

    Http请求的时候,需要传递参数给后端,一般都是key-value的形式,传递的方法有很多种 例如需要传递的数据是 dict(key1=value1,key2=value2) 1. URL参数 把参数 ...

  10. 1107. Social Clusters (30)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...