C(m, n)
<span style="font-size:18px;">const int mod=1e9+7;
typedef long long ll;
//返回d=gcd(a,b);和對應於等式ax+by=d中的x,y
ll extend_gcd(ll a,ll b,ll &x,ll &y)
{
if(a==0&&b==0) return -1;//無最大公約數
if(b==0){x=1;y=0;return a;}
ll d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
//*********求逆元素*******************
//ax = 1(mod n)
ll mod_reverse(ll a,ll n)
{
ll x,y;
ll d=extend_gcd(a,n,x,y);
if(d==1) return (x%n+n)%n;
else return -1;
}
ll c(ll m,ll n)
{
ll i,j,t1,t2,ans;
t1=t2=1;
for(i=n;i>=n-m+1;i--) t1=t1*i%mod;
for(i=1;i<=m;i++) t2=t2*i%mod;
return t1*mod_reverse(t2,mod)%mod;
}
</span>
Lucas定理求組合數C(n, m)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL Power_mod(LL a, LL b, LL p)
{
LL res = 1;
while(b!=0)
{
if(b&1) res = (res*a)%p;
a = (a*a)%p;
b >>= 1;
}
return res;
}
LL Comb(LL a,LL b, LL p)
{
if(a < b) return 0;
if(a == b) return 1;
if(b > a-b) b = a-b;
LL ans = 1, ca = 1, cb = 1;
for(LL i=0; i<b; ++i)
{
ca = (ca*(a-i))%p;
cb = (cb*(b-i))%p;
}
ans = (ca*Power_mod(cb, p-2, p))%p;
return ans;
}
LL Lucas(int n, int m, int p)
{
LL ans = 1;
while(n && m && ans)
{
ans = (ans * Comb(n%p, m%p, p))%p;
n /= p;
m /= p;
}
return ans;
}
int main()
{
int n,m,p;
while(scanf("%d%d%d",&n,&m,&p) !=EOF)
{
printf("%lld\n",Lucas(n,m,p));
}
return 0;
}