当前位置:网站首页>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;
}
边栏推荐
- Download files and preview pictures
- Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk
- Partner cloud form strong upgrade! Pro version, more extraordinary!
- Why is the default of switch followed by break?
- What are eNB, EPC and PGW?
- Unity skframework framework (XVI), package manager development kit Manager
- Jerry's watch reads the alarm clock [chapter]
- Why can't d link DLL
- Error function ERF
- 国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
猜你喜欢
Japan bet on national luck: Web3.0, anyway, is not the first time to fail!
[cloud native database] what to do when encountering slow SQL (Part 1)?
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
Unity skframework framework (XIV), extension extension function
Essential for operation and maintenance - Elk log analysis system
题解:《压缩技术》(原版、续集版)
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
OpenFOAM:lduMatrix&lduAddressing
随机推荐
【youcans 的图像处理学习课】总目录
Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk
JS逆向之巨量创意signature签名
科技的成就(二十七)
[opencv learning] [contour detection]
Let juicefs help you with "remote backup"
Domestic free data warehouse ETL dispatching automation operation and maintenance expert taskctl
Unity SKFramework框架(十九)、POI 兴趣点/信息点
Bridge of undirected graph
题解:《压缩技术》(原版、续集版)
Unity skframework framework (XVI), package manager development kit Manager
Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
Unity SKFramework框架(十四)、Extension 扩展函数
ADB basic commands
Unity SKFramework框架(二十)、VFX Lab 特效库
Jerry's watch delete alarm clock [chapter]
Why can't d link DLL
Jerry's watch stops ringing [article]
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
Unity skframework framework (XIX), POI points of interest / information points