void extend_gcd(ll a,ll &x,ll b,ll &y){
if(b==){
x=,y=;
return;
}
ll x1,y1;
extend_gcd(b,x1,a%b,y1);
x=y1;
y=x1-(a/b)*y1;
}
ll inv(ll a,ll m){
ll t1,t2;
extend_gcd(a,t1,m,t2);
return ( t1%m + m ) % m;
}
ll qpow(ll x,ll y,ll m){
if(!y) return ;
ll ans=qpow(x,y>>,m);
ans= ans *ans %m;
if(y&) ans=ans *x %m;
return ans;
}
ll nump(ll x,ll p){
ll ans=;
while(x) ans += x/p, x /= p;
return ans;
}
ll fac(ll n,ll p,ll pk){
if(n==) return ;
ll ans=;
for(ll i=;i<=pk;i++){
if(i%p==) continue;
ans= ans *i %pk;
}
ans = qpow(ans,n/pk,pk);
ll to=n%pk;
for(ll i=;i<=to;i++){
if(i%p==) continue;
ans = ans *i %pk;
}
return fac(n/p,p,pk) *ans %pk;
}
ll cal(ll n,ll m,ll p,ll pi,ll pk){
ll a=fac(n,pi,pk),b=fac(m,pi,pk),c=fac(n-m,pi,pk);
ll d=nump(n,pi)-nump(m,pi)-nump(n-m,pi);
ll ans= a %pk * inv(b,pk) %pk * inv(c,pk) %pk * qpow(pi,d,pk) %pk;
return ans * (p/pk) %p * inv(p/pk,pk) %p;
}
ll nCmmodp(ll n,ll m,ll p){
if(n<m) return ;
ll ans=;
ll x=p;
for(ll i=;i*i<=x&&x>;i++){
ll k=,pk=;
while(x%i==){
x /= i;
k++;
pk *= i;
}
if(k>){
ans =( ans + cal(n,m,p,i,pk) )%p;
}
}
if(x>) ans=( ans + cal(n,m,p,x,x) )%p;
return ans;
}

模板 lucas的更多相关文章

  1. 组合数模板 - Lucas

    2017-08-10 19:35:32 整理者:pprp 用于计算C(m,n) % p 代码如下: //lucas #include <iostream> using namespace ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. xdoj-1057(Lucas定理的证明及其模板)

    Lucas定理的证明: 转自百度百科(感觉写的还不错) 首先你需要这个算式:    ,其中f > 0&& f < p,然后 (1 + x) nΞ(1 + x) sp+q Ξ ...

  4. 模板—数学—Lucas

    模板—数学—Lucas Code: #include <cstdio> #include <algorithm> using namespace std; #define N ...

  5. Lucas定理模板【bzoj2982】【combination】

    (上不了p站我要死了,侵权度娘背锅) Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ ...

  6. 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数

    typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...

  7. 【组合数+Lucas定理模板】HDU 3037 Saving

    acm.hdu.edu.cn/showproblem.php?pid=3037 [题意] m个松果,n棵树 求把最多m个松果分配到最多n棵树的方案数 方案数有可能很大,模素数p 1 <= n, ...

  8. BZOJ 4403 2982 Lucas定理模板

    思路: Lucas定理的模板题.. 4403 //By SiriusRen #include <cstdio> using namespace std; ; #define int lon ...

  9. BZOJ2982: combination Lucas模板

    2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 734  Solved: 437[Submit][Status][Di ...

随机推荐

  1. Struts个人总结

    编写Struts2第一个程序 Struts2是目前最流行的MVC框架,吸收了传统Struts和WebWork两者的精华,基于Struts2来进行开发可以大大减少开发时间,提高开发效率,并降低后期维护时 ...

  2. POJ2892Tunnel Warfare (线段树)

    http://poj.org/problem?id=2892 记录每个区间端点的左连续及右连续 都是单点更新 用不着向下更新 还简单点 找错找了N久 最后发现将s[w<<1|1]写成s[w ...

  3. codevs3731 寻找道路

    方向dfs判定是否可行,spfa跑最短路. noip水题,wa好几次. #include<cstdio> #include<algorithm> #include<cst ...

  4. MySQL table_id原理及风险分析

    1. 什么是table_id MySQL binlog文件按格式分为文件头部和事件信息.文件头部占4字节,内容固定为:"\xfe\x62\x69\x6e",接下来就是各个event ...

  5. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.8

    For any matrix $A$ the series $$\bex \exp A=I+A+\frac{A^2}{2!}+\cdots+\frac{A^n}{n!}+\cdots \eex$$ c ...

  6. 序列化框架性能对比(kryo、hessian、java、protostuff)

    简介:   优点 缺点 Kryo 速度快,序列化后体积小 跨语言支持较复杂 Hessian 默认支持跨语言 较慢 Protostuff 速度快,基于protobuf 需静态编译 Protostuff- ...

  7. PLSQL Develop 配置

    plsq 连接oracle数据库的配置步骤: 1.下载oracle客户端: 下载地址:http://www.oracle.com/technetwork/topics/winx64soft-08954 ...

  8. uva11732 strcmp() Anyone?

    题意:给出多个字符串,两两配对,求总配对次数. 思路:如果两个字符串一样,ans=strlen(字符串)*2+2,如果不同,ans=公共前缀长度*2+1:用左儿子右兄弟建字典树.插入一个字符计算一次. ...

  9. 利用迅雷提供的接口从磁力链得到bt种子文件

    本地下载工具的磁力链下载速度不给力,而百度云盘有提供离线下载服务,相当于就是直接到服务器取个链接而已.但这需要bt文件,而我只有链力链.网上搜了一下,可以从磁力链构造一个bt文件的下载地址,用pyth ...

  10. CDH 5.5.1 Yum源服务器搭建

    CDH 5.5.1 Yum源服务器搭建 下载Cloudera Repo #下载资源仓库repo wget https://archive.cloudera.com/cdh5/redhat/6/x86_ ...