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):
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL; const int MAXN = ; int fac[MAXN];
int n, m, p, T; int power(int x, int p, int mod) {
int res = ;
while(p) {
if(p & ) res = (LL)res * x % mod;
x = (LL)x * x % mod;
p >>= ;
}
return res;
} void init_fac(int p) {
fac[] = ;
for(int i = ; i <= p; ++i)
fac[i] = (LL)fac[i - ] * i % p;
} int lucas(int n, int m, int p) {
int res = ;
while(n && m) {
int a = n % p, b = m % p;
if(a < b) return ;
res = (LL)res * fac[a] * power((LL)fac[b] * fac[a - b] % p, p - , p) % p;//三次乘法注意
n /= p;
m /= p;
}
return res;
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &n, &m, &p);
init_fac(p);
printf("%d\n", lucas(n + m, m, p));
}
}