Loading

代码仓库4 卢卡斯定理

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<deque>
#include<cstdlib>
#include<ctime>
#define dd double
#define ld long double
#define ll long long
#define ull unsigned long long
#define N 200010
#define M number
using namespace std;

inline ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

ll t,n,m,p,inv[N],jie[N],jieinv[N];

inline ll c(ll n,ll m){
	if(n<m) return 0;
	return jie[n]*jieinv[m]*jieinv[n-m]%p;
}

inline ll solve(ll n,ll m){
	if(n<m) return 0;
	else if(n<p) return c(n,m);
	else return solve(n/p,m/p)*solve(n%p,m%p)%p;
}

int main(){
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld%lld",&n,&m,&p);
		inv[1]=1;jie[1]=1;jieinv[1]=1;
		for(int i=2;i<=p;i++) jie[i]=jie[i-1]*i%p;
		for(int i=2;i<=p;i++) inv[i]=(p-p/i)*inv[p%i]%p,jieinv[i]=jieinv[i-1]*inv[i]%p;
		jie[0]=jieinv[0]=1;
		printf("%lld\n",solve(m+n,n));
	}
	return 0;
}
posted @ 2021-03-11 16:03  hyl天梦  阅读(63)  评论(0编辑  收藏  举报