lucas定理
来源:互联网 发布:sql统计列 编辑:程序博客网 时间:2024/05/16 15:20
题目链接
大佬博客还有扩展lucas
题意:给你n和m求C(n,m)。
C(n,m)=n!/(m!*(n-m)!);
当题目数据不是很大的时候可以打表,存一个阶乘表和逆元表,这样就可以直接计算了,但是当n和m很大的时候,就要用lucas定理了,前提是模数要是素数。
lucas定理:C(n,m)%p=C(n/p,m/p)*C(n%p,m%p)%p;
逆元:由费马小定理a ^ (p-1) = 1 (%p),两边同时除去a则a ^ (p-2) = a^-1 (%p),得a的逆元a^-1 = a ^ (p-2)。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define LL long longusing namespace std;const int maxn=1e6+10;const LL mod=1000003;LL fac[maxn];void init(){ fac[0]=1; for(int i=1;i<maxn;i++)//阶乘表 fac[i]=(i*fac[i-1])%mod;}LL quickpow(LL a,LL k)//求逆元{ LL res=1; while(k) { if(k&1) res=(res*a)%mod; a=(a*a)%mod; k>>=1; } return res;}LL C_n_m(LL n,LL m)//求C(n,m);{ LL ans=1; ans=fac[n]*quickpow(fac[m]*fac[n-m]%mod,mod-2)%mod; return ans;}LL lucas(LL n,LL m){ LL ans=1; if(n<m) return 0; while(n&&m&&ans) { ans=ans*C_n_m(n%mod,m%mod); n/=mod; m/=mod; } return ans;}int main(){ init(); int ncase,Z=0; scanf("%d",&ncase); while(ncase--) { LL n,m; scanf("%lld%lld",&n,&m); printf("Case %d: %lld\n",++Z,lucas(n,m)); }}
阅读全文
0 0
- Lucas 定理
- Lucas 定理
- lucas定理
- Lucas定理
- Lucas 定理
- Lucas 定理
- Lucas 定理
- Lucas 定理
- Lucas定理
- lucas定理
- lucas定理
- Lucas定理
- Lucas定理
- Lucas 定理。。。
- lucas定理
- Lucas定理
- lucas定理
- Lucas定理
- Android 应用冻结流程分析
- Android逆向之旅---动态方式破解apk进阶篇(IDA调试so源码)
- CDN加速
- SpringMVC标签的使用以及command的详解
- 编写可维护的javascript(十):抛出自定义错误
- lucas定理
- LIBSVM与LIBLINEAR
- ubuntu调整分辨率
- Android布局----详解
- 1.7-Java-冒泡排序
- SSO搭建(框架KISSO)
- Jmeter连接SqlServer数据库进行压力测试
- 归并排序
- python中的__new__概念(工厂