Lucas定理模版、
2016-09-08 20:05
211 查看
资料:传送门
相关题目:传送门
求C(n,m)模p的结果
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll n,m,p;
ll quick_mod(ll a, ll b){
ll ans = 1;
a=a%p;
while(b){
if(b&1) ans = ans*a%p;
b>>=1;
a=a*a%p;
}
return ans;
}
ll C(ll n, ll m){
if(m>n) return 0;
if(n==m) return 1;
if(m>n-m) m = n-m;
ll ans=1, cns=1;
for(int i=1; i<=m; ++i){
ans=(ans*(n+i-m))%p;
cns=(cns*i)%p;
}
ll res = (ans*quick_mod(cns, p-2))%p;
return res;
}
ll Lucas(ll n, ll m){
if(m==0) return 1;
return C(n%p, m%p)%p * Lucas(n/p, m/p)%p;
}
int main(){
scanf("%lld%lld%lld",&n,&m,&p);
printf("%lld\n", Lucas(n, m));
return 0;
}
相关题目:传送门
求C(n,m)模p的结果
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll n,m,p;
ll quick_mod(ll a, ll b){
ll ans = 1;
a=a%p;
while(b){
if(b&1) ans = ans*a%p;
b>>=1;
a=a*a%p;
}
return ans;
}
ll C(ll n, ll m){
if(m>n) return 0;
if(n==m) return 1;
if(m>n-m) m = n-m;
ll ans=1, cns=1;
for(int i=1; i<=m; ++i){
ans=(ans*(n+i-m))%p;
cns=(cns*i)%p;
}
ll res = (ans*quick_mod(cns, p-2))%p;
return res;
}
ll Lucas(ll n, ll m){
if(m==0) return 1;
return C(n%p, m%p)%p * Lucas(n/p, m/p)%p;
}
int main(){
scanf("%lld%lld%lld",&n,&m,&p);
printf("%lld\n", Lucas(n, m));
return 0;
}
相关文章推荐
- DOM Element对象的offsetXXX方法
- 第一章-第四题(ACM 比赛的程序是软件么? “写程序” 和 ”做软件“ 有区别么?软件工程是不是教那些不怎么会写程序的人开发软件? 你怎么看?这个游戏团队, 有很好的软件,但是商业模式和其他软件之外的因素呢?有没有考虑到)--By梁旭晖
- Java中不编写返回引用对象的访问器方法
- NOIP2015 提高组 day1 信息传递
- 安装easy_install和pip
- Intent中的四个重要属性——Action、Data、Category、Extras
- JS与安卓的通信
- windows server 2008 域控安装
- *浙大甲级PAT 1109
- 分治思想《算法竞赛入门经典》
- 乱七八糟,不务正业
- 3.Android基础:常见控件----->ImageView
- popupwindow的基本应用,点击外部消失
- 基于PCNTL的PHP多进程并发编程
- MongoDB基本命令用
- 01背包问题
- LBF特征和增强的LBF特征。
- 一个简单的Android Library Project例子
- 【c++11】 练习 std::bind & lambda
- R语言的一些笔记