题意概述:多组询问,给出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 安装 配置 及启动

    linux下安装redis及其中遇到的问题的解决方法1.将下载好的压缩包放到/usr/local目录下# tar xzf redis-3.0.2.tar.gz# cd redis-3.0.2# mak ...

  2. Vue 自动获取最新的Vue文件

    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>

  3. 小白的Unity5之路(二)镜头平滑跟随角色

    这次要完成Camera跟随Player移动, 首先考虑Camera的跟随目标target和平滑移动速度smothing再考虑Camera与Player的偏移量(就是Camera与Player有一个永恒 ...

  4. Linux用户和权限管理

    用户:资源获取标识符,资源分配,安全权限模型的核心要素之一 密码:来实现用户认证 创建用户:useradd Username 生成的属性信息 /etc/passwd 用户名:密码:占位符:UID:GU ...

  5. macOS安装MongoDB

    先在macOS里安装一个很好的插件 1.百度搜索homebrew 或 直接进入https://brew.sh 2.复制网页下面的命令符 /usr/bin/ruby -e "$(curl -f ...

  6. usb驱动之打印usb设备信息(一)

    1. 定义usb支持的设备类型: static const struct usb_device_id mouse_table[] = { { USB_INTERFACE_INFO(USB_INTERF ...

  7. ACM1021:Fibonacci Again

    Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) ...

  8. PTA(BasicLevel)-1006换个格式输出整数

      一  换格式输出整数 用字母 B 来表示“百”.字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n(<10),换个格式来输出任一个不超过 3 位的正整数.例如 234 应该被 ...

  9. SSM 框架基于ORACLE集成TKMYBATIS 和GENERATOR自动生成代码(Github源码)

    基于前一个博客搭建的SSM框架 https://www.cnblogs.com/jiangyuqin/p/9870641.html 源码:https://github.com/JHeaven/ssm- ...

  10. Dart 语言了解

    Dart 语言了解 概念 当您了解Dart语言时,请记住以下事实和概念: 您可以放在变量中的所有内容都是一个对象,每个对象都是一个类的实例.偶数,函数和 null对象.所有对象都从Object类继承. ...