study and life share of wss

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

//并且p最大为 10^5, m, n可以超级大
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 100010;
typedef long long LL;
LL factorial[maxn];

LL pow_mod(LL a, LL b, LL p)
{
LL ans = 1;
LL base = a;
while(b){
if(b & 1) ans = (base*ans) % p;
base = (base*base) % p;
b >>= 1;
}
return ans;
}

void init(LL p)
{
factorial[0] = 1;
for(int i = 1; i <= p; i++)
factorial[i] = (factorial[i-1]*i) % p;
return ;
}
LL Lucas(LL n, LL m, LL p)
{
LL ans = 1;
while(n && m){
LL aa = n % p, bb = m % p;
if(aa < bb) return 0;
ans = ans * factorial[aa] * pow_mod(factorial[bb]*factorial[aa-bb]%p, p-2, p) % p;
n /= p, m /= p;
}
return ans;
}

int main()
{
int T;
LL n, m, p;
scanf("%d", &T);
while(T--){
scanf("%lld %lld %lld", &n, &m, &p);
init(p);
LL ans = Lucas(m+n, m, p);
cout << ans << endl;
}
return 0;
}