传送门

[题意]:

求C(n,k)%m,n<=108,k<=n,m<=106

[思路]:

扩展lucas定理+中国剩余定理

  

#include<cstdio>
using namespace std;
typedef long long ll;
ll n,m,MOD,ans;
ll fpow(ll a,ll p,ll mod){
ll res=;
for(;p;p>>=,a=a*a%mod) if(p&) res=res*a%mod;
return res;
}
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
if(!b){d=a;x=;y=;return ;}
exgcd(b,a%b,d,y,x);
y-=a/b*x;
}
ll inv(ll a,ll p){
ll d,x,y;
exgcd(a,p,d,x,y);
return d==?(x%p+p)%p:-;
}
ll mul(ll n,ll pi,ll pk){
if(!n) return ;
ll ans=;
if(n/pk){
for(ll i=;i<=pk;i++) if(i%pi) ans=ans*i%pk;
ans=fpow(ans,n/pk,pk);
}
for(ll i=;i<=n%pk;i++) if(i%pi) ans=ans*i%pk;
return ans*mul(n/pi,pi,pk)%pk;
}
ll C(ll n,ll m,ll pi,ll pk,ll mod){
if(n<m) return ;
ll a=mul(n,pi,pk),b=mul(m,pi,pk),c=mul(n-m,pi,pk);
ll ans,k();
for(ll i=n;i;i/=pi) k+=i/pi;
for(ll i=m;i;i/=pi) k-=i/pi;
for(ll i=n-m;i;i/=pi) k-=i/pi;
ans=a*inv(b,pk)%pk*inv(c,pk)%pk*fpow(pi,k,pk)%pk;
return ans*(mod/pk)%mod*inv(mod/pk,pk)%mod;
}
int main(){
scanf("%I64d%I64d%I64d",&n,&m,&MOD);
ll x=MOD;
for(ll pk,i=;i*i<=MOD;i++){
if(!(x%i)){
pk=;
while(!(x%i)) pk*=i,x/=i;
ans=(ans+C(n,m,i,pk,MOD))%MOD;
}
}
if(x>) ans=(ans+C(n,m,x,x,MOD))%MOD;
printf("%I64d\n",ans);
return ;
}

codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula【扩展lucas】的更多相关文章

  1. codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula 扩展Lucas定理 扩展CRT

    默默敲了一个下午,终于过了, 题目传送门 扩展Lucas是什么,就是对于模数p,p不是质数,但是不大,如果是1e9这种大数,可能没办法, 对于1000000之内的数是可以轻松解决的. 题解传送门 代码 ...

  2. CF 2015 ICL, Finals, Div. 1 J. Ceizenpok’s formula [Lucas定理]

    http://codeforces.com/gym/100633/problem/J Lucas定理P不是质数裸题 #include <iostream> #include <cst ...

  3. GYM100633J. Ceizenpok’s formula 扩展lucas模板

    J. Ceizenpok’s formula time limit per test 2.0 s memory limit per test 256 MB input standard input o ...

  4. Codeforces.100633J.Ceizenpok's formula(扩展Lucas)

    题目链接 ->扩展Lucas //求C_n^k%m #include <cstdio> typedef long long LL; LL FP(LL x,LL k,LL p) { L ...

  5. 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)

    J. Ceizenpok’s formula time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. 2015 ICL, Finals, Div. 2【ABFGJK】

    [题外话:我......不补了......] 2015 ICL, Finals, Div. 2:http://codeforces.com/gym/100637 G. #TheDress[水] (st ...

  7. Ceizenpok’s formula Gym - 100633J 扩展Lucas定理 + 中国剩余定理

    http://codeforces.com/gym/100633/problem/J 其实这个解法不难学的,不需要太多的数学.但是证明的话,我可能给不了严格的证明.可以看看这篇文章 http://ww ...

  8. Java高并发 -- J.U.C.组件扩展

    Java高并发 -- J.U.C.组件扩展 主要是学习慕课网实战视频<Java并发编程入门与高并发面试>的笔记 FutureTask Future模式,核心思想是异步调用.和同步调用的区别 ...

  9. [Codeforces 100633J]Ceizenpok’s formula

    Description 题库链接 求 \[C_n^m \mod p\] \(1\leq m\leq n\leq 10^{18},2\leq p\leq 1000000\) Solution 一般的 \ ...

随机推荐

  1. Spring学习笔记(四)-- Spring事务全面分析

    通过本系列的文章对Spring的介绍,我们对Spring的使用和两个核心功能IOC.AOP已经有了初步的了解,结合我个人工作的情况,因为项目是金融系 统.那对事务的控制是不可缺少的.而且是很严格的控制 ...

  2. Hive SQL 常见问题(转载)

    http://www.aboutyun.com/thread-14942-1-1.html 问题导读 1.Hive查询语句和SQL查询语句区别与联系. 2.distribute by.group by ...

  3. Navicat Premium 快捷键

    1.ctrl+q 打开查询窗口2.ctrl+/ 注释sql语句3.ctrl+shift +/ 解除注释4.ctrl+r 运行查询窗口的sql语句5.ctrl+shift+r 只运行选中的sql语句6. ...

  4. git问题:git提交的时候总是提示key加载失败,总是需要手工将key加到Pageant中

    问题描述: 重装过一次系统,在重装之前git+tortoisegit配合很好,提交的时候都能自动加载ppk,但是重装系统后,也重新生成pulic key上传到了服务器,但是每次提交的时候都提示key加 ...

  5. centos7 安装php 多线程pthreads

    第一步:yum源更新 yum update 第二步:下载php7源码 wget http://124.202.164.8/files/312100000A0BB72D/cn.php.net/distr ...

  6. PHP中获取当前页面的完整URL & php $_SERVER中的SERVER_NAME 和HTTP_HOST的区别

    #测试网址:     http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<b ...

  7. Mac Yosemite上安装macvim和YouCompleteMe

    今天在macvim上安装YouCompleteMe的时候,碰到一个运行vim崩溃的错误.查了半天终于解决! 先上一下安装macvim的过程 # install xcode and command li ...

  8. 多线程-ConcurrentHashMap(JDK1.8)

    前言 HashMap非线程安全的,HashTable是线程安全的,所有涉及到多线程操作的都加上了synchronized关键字来锁住整个table,这就意味着所有的线程都在竞争一把锁,在多线程的环境下 ...

  9. Atitit. 。Jna技术与 解决 java.lang.Error: Invalid memory access

    Atitit. .Jna技术与 解决 java.lang.Error: Invalid memory access 1. 原因与解决1 2. jNA (这个ms sun 的)1 3. Code1 4. ...

  10. 150. Best Time to Buy and Sell Stock II【medium】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...