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. Android开发之View动画效果插补器Interpolator

    插补器Interpolator 官网描述:An interpolator defines the rate of change of an animation. This allows the bas ...

  2. hadoop博客 oschina

    http://my.oschina.net/Xiao629/blog?catalog=449279

  3. 函数innobase_start_or_create_for_mysql

    buffer pool初始化 /******************************************************************** Starts InnoDB a ...

  4. 在SQL Server实现最短路径的搜索

    开始 这是去年的问题了,今天在整理邮件的时候才发现这个问题,感觉顶有意思的,特记录下来. 在表RelationGraph中,有三个字段(ID,Node,RelatedNode),其中Node和Rela ...

  5. 禅道,然之和蝉知入驻VM Depot

    Posted on 四月 21, 2015 by 陈阳 近日, 易软天创与微软开放技术合作,在虚拟镜像中国站点vmdepot.msopentech.cn上成功部署易软天创的集成管理环境,为希望快速基于 ...

  6. (4)java方法区

    java方法区[名词解析]        --->和java堆一样,方法区是一块所有线程共享的内存区域.        --->保存系统的类信息,比如,类的字段,方法,常量池等.      ...

  7. Apache-AB压力测试实例

    一 AB背景介绍 Apache附带的压力测试工具apache bench--简称ab,非常容易使用,并且完全可以摸你各种条件对Web服务器发起测试请求.ab可以直接在Web服务器本地发起测试请求,这对 ...

  8. Good Bye 2015 C - New Year and Domino

    题意:计算给定矩形面积(r1,c1),(r2,c2)内长度为2的有多少个?向右或向下计算. 思路:预处理字符.分别向右和向下处理.注意边界情况,可能算多了.用容斥原理计算长度为二的单位. #inclu ...

  9. 4.2 CUDA Reduction 一步一步优化

    Reduction并行分析: 每个线程是基于一个树状的访问模型,从上至下,上一层读取数据相加得到下一层的数据.不停的迭代,直到访问完所有的数据. 利用这么多的线程块(thread block)我们需要 ...

  10. leetcode—Same Tree

    1.题目描述 Given two binary trees, write a function to check if they are equal or not.   Two binary tree ...