当前位置:网站首页>P3807 [template] Lucas theorem /lucas theorem
P3807 [template] Lucas theorem /lucas theorem
2022-07-02 13:30:00 【Chasing the beacon】
P3807 【 Templates 】 Lucas theorem /Lucas Theorem
L u c a s Lucas Lucas Theorem : For prime numbers p p p, Yes
( n m ) m o d p = ( ⌊ n / p ⌋ ⌊ m / p ⌋ ) ⋅ ( n m o d p m m o d p ) m o d p \binom{n}{m}\bmod p = \binom{\left\lfloor n/p \right\rfloor}{\left\lfloor m/p\right\rfloor}\cdot\binom{n\bmod p}{m\bmod p}\bmod p (mn)modp=(⌊m/p⌋⌊n/p⌋)⋅(mmodpnmodp)modp
Equivalent to
C ( n , m ) % p = C ( n / p , m / p ) ∗ C ( n % p , m % p ) % p C(n,m)\%p=C(n/p,m/p)*C(n\%p,m\%p)\%p C(n,m)%p=C(n/p,m/p)∗C(n%p,m%p)%p
The boundary conditions : When m = 0 m=0 m=0 When , return 1 1 1 .
C o d e : Code: Code:
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
typedef long long ll;
using namespace std;
ll qpow(ll a,ll b,ll p){
// Fast power
ll ans=1,base=a;
while(b){
if(b&1) ans*=base,ans%=p;
base*=base,base%=p;
b>>=1;
}
return ans;
}
ll C(ll n,ll m,ll p){
// Combinatorial number
if(n<m) return 0;
if(m>n-m) m=n-m;
ll a=1,b=1;
FOR(i,0,m-1){
a=(a*(n-i))%p;
b=(b*(i+1))%p;
}
return a*qpow(b,p-2,p)%p;// Fermat's small Theorem
}
ll Lucas(ll n,ll m,ll p){
// Lucas theorem
if(m==0) return 1;
return Lucas(n/p,m/p,p)*C(n%p,m%p,p)%p;
}
int main(){
int T;cin>>T;
while(T--){
ll n,m,p;
cin>>n>>m>>p;
cout<<Lucas(n+m,m,p)%p<<endl;
}
return 0;
}
边栏推荐
- 嵌入式软件开发
- Unity SKFramework框架(十五)、Singleton 单例
- 文件的下载与图片的预览
- Jerry's weather direction coding table [chapter]
- Unity SKFramework框架(十三)、Question 问题模块
- (6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
- Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
- [youcans' image processing learning course] general contents
- 自主可控三维云CAD:CrownCAD赋能企业创新设计
- 中文姓名提取(玩具代码——准头太小,权当玩闹)
猜你喜欢
Research shows that "congenial" is more likely to become friends
Gee learning notes 2
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
I did it with two lines of code. As a result, my sister had a more ingenious way
Bridge of undirected graph
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
为什么switch 的default后面要跟break?
无向图的桥
Unity SKFramework框架(十五)、Singleton 单例
net share
随机推荐
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
每日一题:1175.质数排列
[opencv learning] [template matching]
Unity skframework framework (XIV), extension extension function
Jerry's watch delete alarm clock [chapter]
Download files and preview pictures
三翼鸟两周年:羽翼渐丰,腾飞指日可待
Unity skframework framework (XX), VFX lab special effects library
【youcans 的图像处理学习课】总目录
OpenFOAM:lduMatrix&lduAddressing
I did it with two lines of code. As a result, my sister had a more ingenious way
OpenApi-Generator:简化RESTful API开发流程
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
Unity skframework Framework (XVI), package manager Development Kit Manager
Jerry's weather direction coding table [chapter]
Unity SKFramework框架(十九)、POI 兴趣点/信息点
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
[cloud native database] what to do when encountering slow SQL (Part 1)?
OpenAPI generator: simplify the restful API development process