Problem Description
Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a problem. They suppose that they will save beans in n different trees. However, since the food is not sufficient nowadays, they will get no more than m beans. They want to know that how many ways there are to save no more than m beans (they are the same) in n trees.
Now they turn to you for help, you should give them the answer. The result may be extremely huge; you should output the result modulo p, because squirrels can’t recognize large numbers.
Input
The first line contains one integer T, means the number of cases.
Then followed T lines, each line contains three integers n, m, p, means that squirrels will save no more than m same beans in n different trees, 1 <= n, m <= 1000000000, 1 < p < 100000 and p is guaranteed to be a prime.
Output
You should output the answer modulo p.
题目大意:求C(n + m, m) % p
思路:http://blog.csdn.net/acm_cxlove/article/details/7844973
代码(1234MS):
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 typedef long long LL; 7 8 const int MAXN = 100010; 9 10 int fac[MAXN]; 11 int n, m, p, T; 12 13 int power(int x, int p, int mod) { 14 int res = 1; 15 while(p) { 16 if(p & 1) res = (LL)res * x % mod; 17 x = (LL)x * x % mod; 18 p >>= 1; 19 } 20 return res; 21 } 22 23 void init_fac(int p) { 24 fac[0] = 1; 25 for(int i = 1; i <= p; ++i) 26 fac[i] = (LL)fac[i - 1] * i % p; 27 } 28 29 int lucas(int n, int m, int p) { 30 int res = 1; 31 while(n && m) { 32 int a = n % p, b = m % p; 33 if(a < b) return 0; 34 res = (LL)res * fac[a] * power((LL)fac[b] * fac[a - b] % p, p - 2, p) % p;//三次乘法注意 35 n /= p; 36 m /= p; 37 } 38 return res; 39 } 40 41 int main() { 42 scanf("%d", &T); 43 while(T--) { 44 scanf("%d%d%d", &n, &m, &p); 45 init_fac(p); 46 printf("%d\n", lucas(n + m, m, p)); 47 } 48 }