【模板】Lucas定理
代码如下
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
int p;
LL fac[maxn],inv[maxn];
LL fpow(LL a,LL b,LL mod){
LL ret=1%mod;
for(;b;b>>=1,a=a*a%mod)if(b&1)ret=ret*a%mod;
return ret;
}
LL C(int n,int m){
if(m>n)return 0;
return fac[n]*inv[n-m]%p*inv[m]%p;
}
LL Lucas(int n,int m){
if(m==0)return 1;
return Lucas(n/p,m/p)*C(n%p,m%p)%p;
}
int main(){
int T;scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d%d",&n,&m,&p);
fac[0]=1;
for(int i=1;i<=p;i++)fac[i]=fac[i-1]*i%p;
inv[p-1]=fpow(fac[p-1],p-2,p);
for(int i=p-1;i>=1;i--)inv[i-1]=inv[i]*i%p;
printf("%lld\n",Lucas(n+m,m));
}
return 0;
}
【模板】Lucas定理的更多相关文章
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- xdoj-1057(Lucas定理的证明及其模板)
Lucas定理的证明: 转自百度百科(感觉写的还不错) 首先你需要这个算式: ,其中f > 0&& f < p,然后 (1 + x) nΞ(1 + x) sp+q Ξ ...
- Lucas定理模板【bzoj2982】【combination】
(上不了p站我要死了,侵权度娘背锅) Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ ...
- 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数
typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...
- 【组合数+Lucas定理模板】HDU 3037 Saving
acm.hdu.edu.cn/showproblem.php?pid=3037 [题意] m个松果,n棵树 求把最多m个松果分配到最多n棵树的方案数 方案数有可能很大,模素数p 1 <= n, ...
- BZOJ 4403 2982 Lucas定理模板
思路: Lucas定理的模板题.. 4403 //By SiriusRen #include <cstdio> using namespace std; ; #define int lon ...
- lucas定理 模板
lucas定理 (nm) mod p=(⌊np⌋⌊mp⌋)(n mod&VeryTh ...
- 【luogu P3807】【模板】卢卡斯定理/Lucas 定理(含 Lucas 定理证明)
[模板]卢卡斯定理/Lucas 定理 题目链接:luogu P3807 题目大意 求 C(n,n+m)%p 的值. p 保证是质数. 思路 Lucas 定理内容 对于非负整数 \(n\),\(m\), ...
- [模板] 数学基础:快速幂/乘/逆元/exGCD/(ex)CRT/(ex)Lucas定理
方便复制 快速乘/幂 时间复杂度 \(O(\log n)\). ll nmod; //快速乘 ll qmul(ll a,ll b){ ll l=a*(b>>hb)%nmod*(1ll< ...
- 洛谷.3807.[模板]卢卡斯定理(Lucas)
题目链接 Lucas定理 日常水题...sublime和C++字体死活不同步怎么办... //想错int范围了...不要被longlong坑 //这个范围现算阶乘比预处理快得多 #include &l ...
随机推荐
- 【5】标题上的小logo
<link rel="shortcut icon" href="logo图片的路径"> shortcut --- 捷径,近路 icon --- ...
- nodejs之简单应用与运行
1.nodejs第一个应用,入口函数为http.createServer() var http=require('http');//1.引入 http 模块 //2.用 http 模块创建服务 htt ...
- win 10 系统激活
win10企业版永久激活方法?win10企业版是针对企业用户推出的版本,随着win10系统的不断完善,现在越来越多的人选择升级win10,升级完系统就需要激活它.那么今天就为大家分享一下怎么永久激活w ...
- Centos 搭建DNS服务器
1:安装DNS服务 yum install bind -y 2:修改/etc/named.conf 配置文件 listen-on port 53 { any; }; listen-on-v6 port ...
- Jmeter+Ant+Jenkins接口自动化持续集成环境搭建(Linux)
一.安装说明 系统环境:CentOS release 6.4 JDK版本:jdk1.8.0_181 Jmeter版本:apache-jmeter-3.0 Ant版本:apache-ant-1.9.13 ...
- Function程序设计及应用
Function也称为函数,它是SAP中一个独物的程序模式,一般是一段单独的程序代码,可独立执行或直接被SAP其他程序所调用.Function支持远程访问模式,即提供接口供SAP程序使用(如VB,.N ...
- Delphi导出Excel的设置操作
procedure CreatRepSheet(SheetName:String;PageSize,PageLay:Integer); {新建Excel工作簿.进行页面设置} begin {新建Exc ...
- 借助marquee实现弹幕效果
HTML标签marquee实现滚动效果 .基于此,实现简易版 弹幕: HTML <div class="right_liuyan"> <marquee id=& ...
- NOIP2013提高问题求解T2(关于递推与递归)
同步发表于我的洛谷博客. NOIP2013提高问题求解2: 现有一只青蛙,初始时在n号荷叶上.当它某一时刻在k号荷叶上时,下一时刻将等概率地随机跳到1,2,--,k号荷叶之一上,直到跳到第1号荷叶为止 ...
- C语言博客作业05
这个作业属于哪个课程 C语言程序设计II 这个作业要求在那里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/9827 我在这个课程的目 ...