当前位置:网站首页>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;
}
边栏推荐
- 上海交大教授:何援军——包围盒(包容体/包围盒子)
- linux下清理系统缓存并释放内存
- 口袋奇兵点评
- 伙伴云表格强势升级!Pro版,更非凡!
- Fundamentals of face recognition (facenet)
- 屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
- Jerry's weather code table [chapter]
- 互联网常见34个术语解释
- 题解《子数整数》、《欢乐地跳》、《开灯》
- We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
猜你喜欢

We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse

Unity skframework framework (XII), score scoring module

操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
![[opencv learning] [moving object detection]](/img/2e/9b437b7fe22f1d57334529eda68e37.jpg)
[opencv learning] [moving object detection]

伙伴云表格强势升级!Pro版,更非凡!

Unity skframework Framework (XVI), package manager Development Kit Manager

Unity skframework framework (XIX), POI points of interest / information points

无向图的桥

Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China

完全自主可控三维云CAD:CrownCAD便捷的命令搜索,快速定位所需命令具体位置。
随机推荐
诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
嵌入式软件开发
验证失败,请检查您的回电网址。您可以按照指导进行操作
Lucky numbers in the [leetcode daily question] matrix
[opencv learning] [moving object detection]
Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
Jerry's weather code table [chapter]
科技的成就(二十七)
Jerry's watch delete alarm clock [chapter]
leetcode621. task scheduler
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
JS generates 4-digit verification code
Can automatically update the universal weekly report template, you can use it with your hand!
Unity skframework framework (XIII), question module
[opencv learning] [contour detection]
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
EasyDSS点播服务分享时间出错如何修改?
2022 Heilongjiang provincial examination on the writing skills of Application Essays
题解:《你的飞碟在这儿》、《哥德巴赫猜想》