标签:
2 1 2 5 2 1 5
3 3
解题:Lucas 求组合数取模
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 LL F[100010] = {1}; 5 void init(LL mod) { 6 for(int i = 1; i <= mod; ++i) 7 F[i] = F[i-1]*i%mod; 8 } 9 LL gcd(LL a,LL b,LL &x,LL &y) { 10 if(!b) { 11 x = 1; 12 y = 0; 13 return a; 14 } 15 LL ret = gcd(b,a%b,y,x); 16 y -= x*(a/b); 17 return ret; 18 } 19 LL Inv(LL b,LL mod) { 20 LL x,y,d = gcd(b,mod,x,y); 21 return d == 1?(x%mod + mod)%mod:-1; 22 } 23 LL inv(LL b,LL mod) { 24 if(b == 1) return 1; 25 return inv(mod%b,mod)*(mod-mod/b)%mod; 26 } 27 LL Lucas(LL n,LL m,LL mod) { 28 LL ret = 1; 29 while(n && m) { 30 LL a = n%mod; 31 LL b = m%mod; 32 if(a < b) return 0; 33 ret = ret*F[a]%mod*Inv(F[b]*F[a-b]%mod,mod)%mod; 34 n /= mod; 35 m /= mod; 36 } 37 return ret; 38 } 39 int main() { 40 int kase,n,m,mod; 41 scanf("%d",&kase); 42 while(kase--) { 43 scanf("%d%d%d",&n,&m,&mod); 44 init(mod); 45 printf("%I64d\n",Lucas(n+m,n,mod)); 46 } 47 return 0; 48 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4729400.html