扩展Lucas定理 扩展Lucas板子
题意概述:多组询问,给出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板子的更多相关文章
- codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula 扩展Lucas定理 扩展CRT
默默敲了一个下午,终于过了, 题目传送门 扩展Lucas是什么,就是对于模数p,p不是质数,但是不大,如果是1e9这种大数,可能没办法, 对于1000000之内的数是可以轻松解决的. 题解传送门 代码 ...
- BZOJ1951 [Sdoi2010]古代猪文 【费马小定理 + Lucas定理 + 中国剩余定理 + 逆元递推 + 扩展欧几里得】
题目 "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久很久以前,在山的那 ...
- [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)
题目链接:http://acm.swust.edu.cn/problem/0247/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- bzoj1272 Gate Of Babylon(计数方法+Lucas定理+乘法逆元)
Description Input Output Sample Input 2 1 10 13 3 Sample Output 12 Source 看到t很小,想到用容斥原理,推一下发现n种数中选m个 ...
- 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) ...
- Lucas定理详解
这篇博客是从另一位园友那里存的,但是当时忘了写原文的地址,如果有找到原文地址的请评论联系! Lucas定理解决的问题是组合数取模.数学上来说,就是求 \(\binom n m\mod p\).(p为素 ...
- 洛谷P3773 [CTSC2017]吉夫特(Lucas定理,dp)
题意 满足$b_1 < b_2 < \dots < b_k$且$a_{b_1} \geqslant a_{b_2} \geqslant \dots \geqslant a_{b_k} ...
- Lucas定理和扩展Lucas定理
1.Lucas定理 首先给出式子:\(C_n^m\%p = C_{\lfloor\frac{n}{p}\rfloor}^{\lfloor\frac{m}{p}\rfloor} * C_{n\%p}^{ ...
- 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 ...
随机推荐
- C++快速开发样本工程的建立--建立工程
因为QT建立工程清晰整洁,便于作为样板工程原型.采用QT 5.8.0 64位版本建立工程. 1.建立工程 打开VS2015 新建->新建项目->QT GUI Application -&g ...
- C++练习 | 模板与泛式编程练习
#include <iostream> #include <cmath> #include <cstring> #include <string> #i ...
- MongoDB模糊查询 工具
{"Exception":{$regex:"定时发送邮件"}} //模糊查询条件 {"DateTime":-1} // ...
- laravel5.5源码阅读草稿——路由
laravel 里的路由是由RouteServiceProvider提供的,其中的boot方法为启动项,调用了父类的boot方法. RouteServiceProvider中的boot方法设置了自己与 ...
- 关于flume的filechannel的 full 问题
事务启动以后,批量向事务Transaction的一个putList的尾部写入,putlist是一个LinkedBlockingDeque . 事务提交的时候, 把putlist中的event批量移除, ...
- 嵌入式C语言自我修养 10:内联函数探究
10.1 属性声明:noinline & always_inline 这一节,接着讲 __atttribute__ 属性声明,__atttribute__ 可以说是 GNU C 最大的特色.我 ...
- XAMPP之Mysql启动失败
启动XAMPP中的Mysql出现如下: 可能的原因是本地有多个MySQL,所以要在注册表编辑器中将imagePath改成XAMPP中的mysql的地址.(打开注册表编辑器:win+R,输入regedi ...
- intel-FPGA的片内存储器问题
FPGA的片内有很多的存储器资源,可以配置成单端口的ROM.RAM和双端口的ROM.RAM,以及移位寄存器和FIFO等.在学习过程中,笔者遇到过几个小问题,总结如下: 片内是不是有ROM或者RAM? ...
- 20155315 2017-05-10 《Java程序设计》课堂代码检查
一.教材代码检查-p98 代码要求 修改教材P98 Score2.java, 让执行结果数组填充是自己的学号 代码链接 运行结果截图 二.在IDEA中以TDD的方式对String类和Arrays类进行 ...
- 20155320 实验四 Android程序设计
20155320 实验四 Android程序设计 实验内容 (一)Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for ...