题意概述:多组询问,给出N,K,M,要求回答C(N,K)%M,1<=N<=10^18,1<=K<=N,2<=M<=10^6

分析:

  模数不为质数只能用扩展Lucas,裸题没什么好说的。

  emmmmmm......知识点我就不讲了吧......(主要是我现在都还没有参透博客园怎么放公式)直接丢代码!加上了一些棒棒的优化~

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
using namespace std;
typedef long long LL;
const int maxn=; LL N,K; int M;
int pri[maxn],tot; bool ntp[maxn]; void get_pri(){
ntp[]=ntp[]=;
for(int i=;i<=;i++){
if(!ntp[i]) pri[++tot]=i;
for(int j=;j<=tot&&1ll*pri[j]*i<=;j++){
ntp[pri[j]*i]=;
if(i%pri[j]==) break;
}
}
}
void exgcd(int a,int b,LL &d,LL &x,LL &y){
if(!b) d=a,x=,y=;
else exgcd(b,a%b,d,y,x),y-=a/b*x;
}
int inv(int a,int p){
LL d,x,y; exgcd(a,p,d,x,y);
return (x%p+p)%p;
}
int qkpow(int x,LL y,int p){
int re=,t=x;
for(int i=;(1ll<<i)<=y;i++){
if((1ll<<i)&y) re=1ll*re*t%p;
t=1ll*t*t%p;
}
return re;
}
int J(LL n,int p,int pt,int mul){
if(n<=) return ;
int re=;
if(n>=pt) re=qkpow(mul,n/pt,pt);
for(int i=;i<=n%pt;i++)
if(i%p) re=1ll*re*i%pt;
return 1ll*re*J(n/p,p,pt,mul)%pt;
}
int C(LL n,LL m,int p,int pt){
LL k=,t;
for(t=n;t;t/=p) k+=t/p;
for(t=m;t;t/=p) k-=t/p;
for(t=n-m;t;t/=p) k-=t/p;
int pw=qkpow(p,k,pt); if(!pw) return ;
int mul=;
for(int i=;i<pt;i++)
if(i%p) mul=1ll*mul*i%pt;
int a=J(n,p,pt,mul),b=inv(J(m,p,pt,mul),pt),c=inv(J(n-m,p,pt,mul),pt);
return 1ll*pw*a%pt*b%pt*c%pt;
}
int exLucas(LL n,LL m,int p){
int re=,t=p;
for(int i=;i<=tot&&pri[i]<=t;i++){
if(t%pri[i]) continue;
int pt=,c;
while(t%pri[i]==) pt*=pri[i],t/=pri[i];
c=C(n,m,pri[i],pt);
re=(re+1ll*p/pt*c%p*inv(p/pt,pt)%p)%p;
}
return re;
}
int main()
{
get_pri();
while(cin>>N>>K>>M) cout<<exLucas(N,K,M)<<'\n';
return ;
}

扩展Lucas定理 扩展Lucas板子的更多相关文章

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

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

  2. BZOJ1951 [Sdoi2010]古代猪文 【费马小定理 + Lucas定理 + 中国剩余定理 + 逆元递推 + 扩展欧几里得】

    题目 "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久很久以前,在山的那 ...

  3. [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)

    题目链接:http://acm.swust.edu.cn/problem/0247/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  4. bzoj1272 Gate Of Babylon(计数方法+Lucas定理+乘法逆元)

    Description Input Output Sample Input 2 1 10 13 3 Sample Output 12 Source 看到t很小,想到用容斥原理,推一下发现n种数中选m个 ...

  5. HDU 4349——Xiao Ming's Hope——————【Lucas定理】

    Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. Lucas定理详解

    这篇博客是从另一位园友那里存的,但是当时忘了写原文的地址,如果有找到原文地址的请评论联系! Lucas定理解决的问题是组合数取模.数学上来说,就是求 \(\binom n m\mod p\).(p为素 ...

  7. 洛谷P3773 [CTSC2017]吉夫特(Lucas定理,dp)

    题意 满足$b_1 < b_2 < \dots < b_k$且$a_{b_1} \geqslant a_{b_2} \geqslant \dots \geqslant a_{b_k} ...

  8. Lucas定理和扩展Lucas定理

    1.Lucas定理 首先给出式子:\(C_n^m\%p = C_{\lfloor\frac{n}{p}\rfloor}^{\lfloor\frac{m}{p}\rfloor} * C_{n\%p}^{ ...

  9. 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 ...

随机推荐

  1. Redis之Redis消息订阅发布简介

    概念: Redis消息订阅发布是进程间的一种消息通信模式,发送者pub发送消息,订阅者sub接收消息. 使用须知: 需要先订阅后发布,才能接收到消息.在订阅时,相当于创建了可供发布的频道. 案例: ( ...

  2. 常用PHP方法

    个人常用的一些方法记录/** * 返回错误 * * @param int $err_no * @param string $err_msg * @param array $data * @return ...

  3. SQL学习笔记:高级教程

    SQL语法 LIMIT select col from table limit number select * from table limit number LIKE select * from t ...

  4. SpringBoot 启动报The Java Virtual Machine has not been configured to use the desired default character encoding (UTF-8)

    解决方法: 启动的时候在VM中添加 Dfile.encoding=UTF-8 就好了!

  5. 20155209实验一《Java开发环境的熟悉》实验报告

    20155209实验一<Java开发环境的熟悉>实验报告 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验步骤一 ...

  6. 20155227 2016-2017-2 《Java程序设计》第十周学习总结

    20155227 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 Java的网络编程 网络编程 就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所 ...

  7. 20155307 2016-2017第三次《Java程序设计》课堂实践项目

    码云链接 我使用电脑端登录的云班课,截图已经放上去了,却忘了点提交.

  8. day5 RHCE

    19 .配置 iSCSI 服务端  (***先做这个题目**,挂载重启,机器会挂掉) 配置server0提供一个iSCSI服务磁盘名为iqn.2014-11.com.example:server0,并 ...

  9. 【LG3242】 [HNOI2015]接水果

    题面 洛谷 题解 20pts 对于\(n,P,Q\leq 3000\),暴力判断每条路径的包含关系然后排序\(kth\)即可,复杂度\(O(PQ\log P)\) 另30pts 原树为一条链. 发现对 ...

  10. 【LG4317】花神的数论题

    [LG4317]花神的数论题 题面 洛谷 题解 设\(f_{i,up,tmp,d}\)表示当前在第\(i\)位,是否卡上界,有\(tmp\)个一,目标是几个一的方案数 最后将所有\(d\)固定,套数位 ...