FZU 2020 :组合 【lucas】
Problem Description
思路:水题,练一下lucas
#include<iostream>
#include<cstdio>
#include <math.h>
#include<algorithm>
#include<string.h>
#include<queue>
#define MOD 1000003
#define maxn 2009
#define LL long long
using namespace std;
LL mpow(LL a,LL n,LL p)
{
if(n==0)return 1;
if(n==1)return a%p;
if(n&1)return (a*mpow(a,n-1,p))%p;
else
{
LL u=mpow(a,n>>1,p)%p;
return (u*u)%p;
}
}
LL C(LL n,LL m,LL p)
{
if(m==0)return 1;
if(m>n-m)m=n-m;
LL up=1,down=1;
for(int i=1;i<=m;i++){
up=(up*(n-i+1))%p;
down=(down*i)%p;
}
return up*mpow(down,p-2,p)%p;
}
long long lucas(long long n,long long m,long long p)
{
if(m==0)return 1;
return C(n%p,m%p,p)*lucas(n/p,m/p,p);
}
int main()
{
long long m,n,p;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d%I64d",&n,&m,&p);
printf("%I64d\n",lucas(n,m,p));
}
return 0;
}