hdu 3037 费马小定理+逆元求组合数+Lucas定理
来源:互联网 发布:nginx配置lua模块 编辑:程序博客网 时间:2024/05/10 00:01
组合数学推推推最后,推得要求C(n+m,m)%p
其中n,m小于10^9,p小于1^5
用Lucas定理求(Lucas定理求nm较大时的组合数)
因为p数据较小可以直接阶乘打表求逆元
求逆元时,由费马小定理知道p为素数时,a^p-1=1modp可以写成a*a^p-2=1modp
所以a的逆元就是a^p-2,
可以求组合数C(n,m)%p中除法取模,将其转化为乘法取模
即 /(m!*(n-m)!)=*(m!*(n-m)!)^p-2
#include <iostream>#define ll long longconst int N=1e5+5;using namespace std;ll fac[N],p;void init(){ fac[0]=1; for(int i=1;i<=p;i++) fac[i]=fac[i-1]*i%p;}ll qpow(ll a,ll b){ ll ans=1; a%=p; while(b) { if(b&1) { ans=ans*a%p; } b>>=1; a=a*a%p; } return ans;}ll C(ll a,ll b){ if(a<b) return 0; return fac[a]*qpow(fac[b]*fac[a-b],p-2)%p;}ll Lucas(ll a,ll b){ if(b==0) return 1; return (C(a%p,b%p)*Lucas(a/p,b/p))%p;}int main(){ int T; ll n,m; cin>>T; while(T--) { cin>>n>>m>>p; init(); cout<<Lucas(n+m,m)<<endl; } return 0;}
0 0
- hdu 3037 费马小定理+逆元求组合数+Lucas定理
- 组合数/Lucas定理
- [HDU 3944] DP?组合数 Lucas定理
- HDU 3037 Saving Beans 大组合数 lucas定理
- hdu-3037-组合数取模-Lucas定理
- hdu 3037 插板法组合 + lucas定理
- 组合数中的Lucas定理
- hdu3037 组合数 lucas定理
- Lucas定理求组合数
- HDU 3037 lucas + 费马小定理
- hdu 3037 lucas定理
- HDU 3944 DP?(lucas定理 组合数)
- HDU 5226 Tom and matrix 组合数求和+Lucas定理.
- 费马小定理+Lucas定理 大组合数取模
- hdu 3037 lucas定理-组合数学 hdu 4399
- 关于组合数与Lucas定理
- 组合数【Lucas定理,逆元】
- hdu 3037 Saving Beans 【大组合数取模-Lucas定理+逆元取模】
- Python读写文件
- 【Hibernate】一对多映射
- 序列化和反序列化
- 棒!使用.NET Core构建3D游戏引擎
- 【p082】排座椅
- hdu 3037 费马小定理+逆元求组合数+Lucas定理
- 机器学习实战:决策树勘误
- BurpSuite系列(十二)----User options模块(用户选择)
- 仿堆糖Demo
- 夕拾算法进阶篇:16)最长回文子串(动态规划DP)
- 笔试题7
- HTML自学历程二:关于页面文字及背景的基本设置
- Python基础教程(一)
- ERROR beeline.ClassNameCompleter: Fail to parse the class na