light oj 1067 Combinations (组合数的lucas定理)
来源:互联网 发布:手机p图换头软件 编辑:程序博客网 时间:2024/06/11 12:31
/*
用到了lucas定理:A、B是非负整数,p是质数。AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]。
则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0]) modp同
即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p)
对于单独的C(ni, mi) mod p,已知C(n, m) mod p = n!/(m!(n - m)!) mod p。显然除法取模,这里要用到m!(n-m)!的逆元。
根据费马小定理:
已知(a, p) = 1,则 a^(p-1) ≡ 1 (mod p), 所以 a*a^(p-2) ≡ 1 (mod p)。
也就是 (m!(n-m)!)的逆元为 (m!(n-m)!)^(p-2) ;
*/
用到了lucas定理:A、B是非负整数,p是质数。AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]。
则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0]) modp同
即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p)
对于单独的C(ni, mi) mod p,已知C(n, m) mod p = n!/(m!(n - m)!) mod p。显然除法取模,这里要用到m!(n-m)!的逆元。
根据费马小定理:
已知(a, p) = 1,则 a^(p-1) ≡ 1 (mod p), 所以 a*a^(p-2) ≡ 1 (mod p)。
也就是 (m!(n-m)!)的逆元为 (m!(n-m)!)^(p-2) ;
*/
#include<stdio.h>#include<string.h>#include<iostream>#include<vector>using namespace std;#define ll long long#define maxn 1000005#define MOD 1000003ll fac[maxn];ll pow_mod(ll a,ll b,ll mod){ //快速幂运算 ll ret=1; while(b){ if(b&1) ret=(ret*a)%mod; a=(a*a)%mod; b>>=1; } return ret;}ll get_fac(ll p){ //fac数组存的是前i个数的乘积 fac[0]=1; for(int i=1;i<=p;i++) fac[i]=fac[i-1]*i%p;}ll lucas(ll n,ll m,ll p){ //lucas定理:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p) ll ret=1; while(n&&m){ ll a=n%p,b=m%p; if(a<b) return 0; ret=(ret*fac[a]*pow_mod(fac[b]*fac[a-b]%p,p-2,p))%p; //(m!(n-m)!)的逆元为 (m!(n-m)!)^(p-2) ; n/=p; m/=p; } return ret;}int main(){ int i,j,t; ll n,m,p; p=MOD; get_fac(p); cin>>t; for(i=1;i<=t;i++){ scanf("%lld%lld",&n,&m); printf("Case %d: %lld\n",i,lucas(n,m,p)); } return 0;}
0 0
- light oj 1067 Combinations (组合数的lucas定理)
- JOJ1595:Combinations(求解大组合数,Lucas定理)
- 组合数/Lucas定理
- 【Lifht-oj】-1067-Combinations(Lucas定理&逆元)
- Light OJ 1067 Combinations (费小马定理求逆元)
- Light OJ 1067 Combinations
- LIght oj 1067 Combinations
- light oj 1067Combinations
- LightOJ 1067 - Combinations【Lucas定理】
- 组合数中的Lucas定理
- hdu3037 组合数 lucas定理
- Lucas定理求组合数
- Lucas定理(大组合数的取模)
- 【Lucas定理】洛谷1869[愚蠢的组合数]题解
- 洛谷 1869 愚蠢的组合数 Lucas定理 解题报告
- LightOJ 1067 - Combinations (Lucas定理)
- 关于组合数与Lucas定理
- 组合数【Lucas定理,逆元】
- poj 3970 Party 最小公倍数
- css浮动
- Linux下的重要目录
- Git tips
- 4月3日毕设日记
- light oj 1067 Combinations (组合数的lucas定理)
- 字符转换
- 【hadoop】 3007-hadoop的提交job的源码分析
- QMdiArea 右键菜单
- @GeneratedValue
- alfresco中添加自定义metadata属性(基本属性、切面)
- spring学习3-IoC概述
- ORACLE审计小结
- linux 强大的文本处理命令awk详解(转)