代码如下

#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定理的更多相关文章

  1. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  2. xdoj-1057(Lucas定理的证明及其模板)

    Lucas定理的证明: 转自百度百科(感觉写的还不错) 首先你需要这个算式:    ,其中f > 0&& f < p,然后 (1 + x) nΞ(1 + x) sp+q Ξ ...

  3. Lucas定理模板【bzoj2982】【combination】

    (上不了p站我要死了,侵权度娘背锅) Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ ...

  4. 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数

    typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...

  5. 【组合数+Lucas定理模板】HDU 3037 Saving

    acm.hdu.edu.cn/showproblem.php?pid=3037 [题意] m个松果,n棵树 求把最多m个松果分配到最多n棵树的方案数 方案数有可能很大,模素数p 1 <= n, ...

  6. BZOJ 4403 2982 Lucas定理模板

    思路: Lucas定理的模板题.. 4403 //By SiriusRen #include <cstdio> using namespace std; ; #define int lon ...

  7. lucas定理 模板

    lucas定理 (nm)&VeryThinSpace;mod&VeryThinSpace;p=(⌊np⌋⌊mp⌋)(n&VeryThinSpace;mod&VeryTh ...

  8. 【luogu P3807】【模板】卢卡斯定理/Lucas 定理(含 Lucas 定理证明)

    [模板]卢卡斯定理/Lucas 定理 题目链接:luogu P3807 题目大意 求 C(n,n+m)%p 的值. p 保证是质数. 思路 Lucas 定理内容 对于非负整数 \(n\),\(m\), ...

  9. [模板] 数学基础:快速幂/乘/逆元/exGCD/(ex)CRT/(ex)Lucas定理

    方便复制 快速乘/幂 时间复杂度 \(O(\log n)\). ll nmod; //快速乘 ll qmul(ll a,ll b){ ll l=a*(b>>hb)%nmod*(1ll< ...

  10. 洛谷.3807.[模板]卢卡斯定理(Lucas)

    题目链接 Lucas定理 日常水题...sublime和C++字体死活不同步怎么办... //想错int范围了...不要被longlong坑 //这个范围现算阶乘比预处理快得多 #include &l ...

随机推荐

  1. Responsive web design 学习笔记

    Advanced Styling with Responsive Design 此笔记为Coursera同名课程笔记. Week1 什么是响应式设计? 响应式设计: It is designing y ...

  2. Linux(Debian)发行版中文输入法

    Linux发行版下有两大输入法框架:ibus 和fcitx,其中fcitx 的体验要比ibus 好,因此选择 fcitx 框架,并安装中文输入法. 中文输入法中你可以选择fcitx-pinyin or ...

  3. gitlab中的CI

    https://blog.csdn.net/chengzi_comm/article/details/78778284

  4. IIS部署网站 HTTP 错误 500.21 - Internal Server Error

    HTTP 错误 500.21 - Internal Server Error处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipel ...

  5. cocos2dx基础篇(2) 第一个程序

    [本节内容] 1.程序的基本组成:CCSprite(精灵).CCLayer(层).CCScene(场景).CCDirector(导演) 2.分析HelloWorld源码. 一.基本组成 cocos2d ...

  6. Spring IoC容器管理Action

    Spring IoC容器管理Action有两种方式:DelegatingRequestProcessor.DelegatingActionProxy 不管采用哪一种方式,都需要随应用启动时创建Appl ...

  7. SpringMvc 整合mybatis项目搭建

    1.使用idea创建maven项目 2.在项目src目录下 添加java文件夹 并设置类型为sources,添加resource文件夹 设置为resources 4.修改pom文件 添加引用 < ...

  8. Elasticsearch-集群增加节点

    ES-在集群中加入节点 查看分片信息 FengZhendeMacBook-Pro:nacos FengZhen$ curl 'localhost:9200/_cat/shards?v' index s ...

  9. [转帖]56核Xeon Platinum 9200现身 - 英特尔有史以来最大的CPU封装

    56核Xeon Platinum 9200现身 - 英特尔有史以来最大的CPU封装 https://www.cnbeta.com/articles/tech/835271.htm 当英特尔宣布上周正式 ...

  10. 优秀编程学习网站&博文记录

    记录优秀讲解知识点博客内容,侵删! 编程者学习网站 LearnKu终身编程者的知识社区 自动化测试内容 Python 接口自动化测试 应用开源接口网站:https://httpbin.org/#/St ...