www.cnblogs.com/shaokele/
luoguP3807 【模板】卢卡斯定理##
Time Limit: 1 Sec
Memory Limit: 128 MBDescription###
给定n,m,p ((1≤ n,m,p ≤ 10^5))
求 $$C_{n+m}^{m} mod p$$
保证P为prime
C表示组合数。
一个测试点内包含多组数据。
Input###
第一行一个整数T(( Tle 10 )),表示数据组数
第二行开始共T行,每行三个数n m p,意义如上
Output###
共T行,每行一个整数表示答案。
Sample Input###
2
1 2 5
2 1 5
Sample Output###
3
3
题目地址: luoguP3807 【模板】卢卡斯定理
题目大意: 题目已经很简洁了>_<
题解:
模板题
AC代码
#include <cstdio>
#define ll long long
using namespace std;
const int N=1e5+5;
int Q,n,m,mo;
ll fac[N],inv[N];
ll C(int n,int m){
if(n<m)return 0;
if(n<mo)return ((fac[n]*inv[m])%mo*inv[n-m])%mo;
return C(n/mo,m/mo)*C(n%mo,m%mo)%mo;
}
int main(){
scanf("%d",&Q);
while(Q--){
scanf("%d%d%d",&n,&m,&mo);
fac[0]=1;
for(int i=1;i<=100000;i++)
fac[i]=fac[i-1]*i%mo;
inv[0]=1;inv[1]=1;
for(int i=2;i<=mo-1;i++)inv[i]=(inv[mo%i]*(mo-mo/i))%mo;
for(int i=1;i<=mo-1;i++)inv[i]=(inv[i]*inv[i-1])%mo;
printf("%d
",C(n+m,m));
}
return 0;
}