扩展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 ...
随机推荐
- JSON定义及应用
1 什么是JSON? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) 是轻量级的文本数据交换格式,JSON 比 XML 更小.更快,更易解析 ...
- scrapy管道MySQL简记
import pymysqlfrom scrapy.exceptions import DropItemimport time class ErshouchePipeline(object): def ...
- 怎样在Win7系统中搭建Web服务器
一.搭建web服务 1.打开控制面板,选择并进入“程序”,双击“打开或关闭Windows服务”,在弹出的窗口中选择“Internet信息服务”下面所有的选项,点击确定后,开始更新服务. 2.更新完成后 ...
- react项目 使用echarts
1.安装 npm install --save echarts-for-react //如果需要使用echarts的一些特殊方法需要安装 npm install --save echarts 2.使用 ...
- MySQL安装及配置
MySQL安装 我是在Windows上安装的MySQL.首先登录MySQL的官网下载安装包. 最后献上5.5.28这个服务器的下载连接地址: http://cdn.mysql.com/Download ...
- Delphi跨平台下的GetTickCount,GetCurrentThreadID
在Windows下只要uses Windows,就有这两个API可调用GetTickCount,GetCurrentThreadID 如果我们需要跨平台使用这两个函数,就不能仅仅Uses Window ...
- python学习笔记:第8天 文件操作
目录 1. 文件操作介绍 2. 文件操作的几种方式 3. 文件的操作的方法 1. 文件操作介绍 说到操作文件我们肯定会想到流,文件的操作都是通过流来操作的.在python中文件的操作非常简单,并不像J ...
- 中国大学MOOC-JAVA学习(浙大翁恺)—— 温度转换
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto- ...
- 快排(python)
用python写了个快排,第一次发现python居然可以这么简洁. def quicksort(arr): if len(arr) <= 1: return arr pivot = arr[le ...
- HDFS(0.20.2)运营中急救方案
这段时间折腾的都是hadoop和lucene,总结了hadoop在运营过程中出现问题时的解决方案,请大家指教! HDFS(0.20.2)运营中急救方案 1 Namenode 挂掉( ...