拓展Lucas

 #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<vector>
#define MAXN 100000+10
#define ll long long
#define pb push_back
#define ft first
#define sc second
#define mp make_pair
using namespace std;
void extgcd(ll a,ll b,ll &x,ll &y){
if(!b){x=,y=;}
else{
ll xx,yy;
extgcd(b,a%b,xx,yy);
x=yy;
y=xx-a/b*yy;
}
}
ll Inv(ll a,ll b){
ll x,y;
extgcd(a,b,x,y);
x=(x%b+b)%b;
if(!x)x+=b;
return x;
}
ll Pow(ll a,ll b,ll p){
ll ret=1LL;
while(b){
if(b&){(ret*=a)%=p;}
(a*=a)%=p;
b>>=;
}
return ret;
}
ll fac(ll n,ll pi,ll pk){
if(!n)return 1LL;
ll ret=1LL;
for(ll i=;i<pk;i++){
if(i%pi)(ret*=i)%=pk;
}
ret=Pow(ret,n/pk,pk);
for(ll i=;i<=(n%pk);i++){
if(i%pi)(ret*=i)%=pk;
}
return ret*fac(n/pi,pi,pk)%pk;
//每隔pi拿走一个,所以是n/pi,不是n/pk
}
ll C(ll n,ll m,ll pi,ll pk){
ll a=fac(n,pi,pk),b=fac(m,pi,pk),c=fac(n-m,pi,pk);
ll t=0LL;
for(ll i=n/pi;i;i/=pi)t+=i;
for(ll i=m/pi;i;i/=pi)t-=i;
for(ll i=(n-m)/pi;i;i/=pi)t-=i;
ll ret=a*Inv(b,pk)*Inv(c,pk)%pk;
(ret*=Pow(pi,t,pk))%=pk;
return ret;
}
ll n,m,p;
int main()
{
ll ans=0LL;
scanf("%lld%lld%lld",&n,&m,&p);
for(ll x=p,i=;i<=p;i++){
if(x%i==){
ll pk=;
while(x%i==){
pk*=i;
x/=i;
}
ll t=C(n,m,i,pk);
(t*=(p/pk))%=p;
(t*=Inv(p/pk,pk))%=p;
(ans+=t)%=p;
}
}
printf("%lld\n",ans);
return ;
}

codeforces Gym - 100633J Ceizenpok’s formula的更多相关文章

  1. [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 一般的 \ ...

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

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

  4. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  5. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  6. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  7. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  8. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  9. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

随机推荐

  1. 《团队-Oldnote-最终程序》

    托管平台地址:https://github.com/Vcandoit/Notepad 小组名称:TOP 小组成员合照:待添加 程序运行方法:手机app,安装到手机点击即可运行,打开页面会有图标提示. ...

  2. Alpha第五天

    Alpha第五天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  3. 高级软件工程2017第6次作业——团队项目:Alpha阶段综合报告

    1.版本测试报告 1.1在测试过程中总共发现了多少Bug?每个类别的Bug分别为多少个? Bug分类 Bug内容 Fixed 编辑博文时改变文字格式会刷新界面 Can't reproduced 无 N ...

  4. 乘法表(24.9.2017) (WARNING!!!!!!!!!!!)

    #include "stdio.h" main() { int i,j,result; printf("\n"); ;i<;i++) { ;j<;j ...

  5. 201621123057 《Java程序设计》第8周学习总结

    1. 本周学习总结 思维导图归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 ArrayList是允许重复的,但当用它来 ...

  6. 安装QT5.02

    1.下载QT5 SDK 下载地址:http://qt-project.org/downloads. 2.安装QT5 下载完后,假设放在Download/,切换到该目录,输入:./qt-linux-op ...

  7. fs 创建文件夹

    var http = require("http"); var fs = require("fs"); var server = http.createServ ...

  8. sql 多条记录插入

    --多条记录插入,用逗号分开值. INSERT dbo.studentinfor ( id, name, class, age, hpsw ) ', -- id - nvarchar(50) N'te ...

  9. 关于 Ubuntu Linux 16.04中文版的 root 权限及桌面登录问题

    新接触 Ubuntu 的朋友大多会因为安装中没有提示设置 root 密码而不太清楚是什么原因. 起初 Ubuntu 团队希望安装尽可能的简单. 不使用 root , 在安装期间的两个用户交互步骤可以省 ...

  10. 解决忽略VScode中Python插件pylint报错的问题

    pylint是VScode中python自带的插件,可以帮助代码规范,美观. 但是有些报错是你不想看到的,你可以选择性的忽略. 例如,在re.compile()中,可以添加参数re.S使. 匹配任意字 ...