当前位置:网站首页>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;
}
边栏推荐
- 能自动更新的万能周报模板,有手就会用!
- Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
- 2022零代码/低代码开发白皮书【伙伴云出品】附下载
- 最近公共祖先LCA的三种求法
- [Unity]使用GB2312,打包后程序不正常解决方案
- Node. JS accessing PostgreSQL database through ODBC
- Unity skframework framework (XX), VFX lab special effects library
- 2、 Frame mode MPLS operation
- Detailed collection of common MySQL commands
- 免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
猜你喜欢

Node. JS accessing PostgreSQL database through ODBC

记忆函数的性能优化
![Jerry's watch modifies the alarm clock [chapter]](/img/d6/04fb8143027578bb707529a05db548.jpg)
Jerry's watch modifies the alarm clock [chapter]

2022零代码/低代码开发白皮书【伙伴云出品】附下载

How to modify the error of easydss on demand service sharing time?

Finally, someone explained the supervised learning clearly

无向图的桥

Chinese name extraction (toy code - accurate head is too small, right to play)

二、帧模式 MPLS 操作

mac(macos Monterey12.2 m1) 个人使用php开发
随机推荐
Web Foundation
三谈exception——错误处理
[unity] using GB2312, the solution to abnormal program after packaging
OpenFOAM:lduMatrix&lduAddressing
验证失败,请检查您的回电网址。您可以按照指导进行操作
【云原生数据库】遇到慢SQL该怎么办(上)?
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
nohup命令
Ali was killed by two programming problems at the beginning, pushed inward again, and finally landed (he has taken an electronic offer)
MAC (MacOS Monterey 12.2 M1) personal use PHP development
Web基础
Research shows that "congenial" is more likely to become friends
Fundamentals of face recognition (facenet)
Jerry's weather code table [chapter]
ADB basic commands
国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
How to modify the error of easydss on demand service sharing time?
Fully autonomous and controllable 3D cloud CAD: crowncad's convenient command search can quickly locate the specific location of the required command.
最近公共祖先LCA的三种求法
Domestic free data warehouse ETL dispatching automation operation and maintenance expert taskctl