求组合数

 1 #include <cstdio>
 2 using namespace std;
 3 
 4 #define MAX_NUMBER 100000
 5 //快速幂求x^n%mod
 6 long long quick_pow(long long x, long long n, long long mod)
 7 {
 8     long long res = 1;
 9     while (n)
10     {
11         if (n & 1)
12         {
13             res = res * x % mod;
14         }
15 
16         x = x * x % mod;
17         n >>= 1;
18     }
19 
20     return res;
21 }
22 
23 long long fac[MAX_NUMBER+5];
24 long long n, m, p;
25 
26 int main()
27 {
28     while (~scanf("%lld %lld %lld", &n, &m, &p))
29     {
30         //预处理求fac,fac[i] = i!%p
31         fac[0] = 1;
32         for (int i = 1; i <= n; i++)
33         {
34             fac[i] = fac[i - 1] * i % p;
35         }
36         //组合数 = n!*(m!%p的逆元)*((n-m)!%p的逆元)%p
37         printf("%lld\n", fac[n] * quick_pow(fac[m], p - 2, p) % p * quick_pow(fac[n - m], p - 2, p) % p);
38     }
39 }

 

posted @ 2020-11-07 22:32  JamZF  阅读(180)  评论(0编辑  收藏  举报