exLucas Lucas

来日普及。

Lucas
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
56
57
58
59
60
61

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;

const int PP = 1e5 + 10;

int qpow(int a, int x, int mo)
{
int res = 1;
while (x)
{
if (x & 1)
res = 1ll * a * res % mo;
a = 1ll * a * a % mo;
x >>= 1;
}
return res;
}
struct Lucas
{
int fac[PP], inv[PP];
void init(int p)
{
fac[0] = 1;
inv[0] = 0;
for (int i = 1; i <= p; i++)
fac[i] = 1ll * fac[i - 1] * i % p;
inv[1] = 1;
for (int i = 2; i <= p; ++i)
inv[i] = 1ll * (p - p / i) * inv[p % i] % p;
}
int C(int n, int m, int p)
{
if (m > n)
return 0;
return 1ll * fac[n] * inv[fac[n - m]] % p * inv[fac[m]] % p;
}
int calc(int n, int m, int p)
{
if (!m)
return 1;
return 1ll * C(n % p, m % p, p) * calc(n / p, m / p, p) % p;
}
} t;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n, m, p;
scanf("%d%d%d", &n, &m, &p);
t.init(p);
cout << t.calc(n + m, n, p) << endl;
}
}
exLucas
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;

ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (!b)
{
x = 1, y = 0;
return a;
}
ll gd = exgcd(b, a % b, y, x);
y -= a / b * x;
return gd;
}
ll cinv(ll a, ll p)
{
ll x, y;
exgcd(a, p, x, y);
return (x + p) % p;
}
ll qpow(ll a, ll x, ll mo)
{
ll res = 1;
a %= mo;
while (x)
{
if (x & 1)
res = 1ll * res * a % mo;
a = 1ll * a * a % mo;
x >>= 1;
}
return res;
}
ll CRT(ll *p, ll *a, ll len)
{
ll res = 1;
for (int i = 1; i <= len; i++)
res *= p[i];
// cout << res << endl;
ll ans = 0;
for (int i = 1; i <= len; i++)
{
ll L = res / p[i];
ll x, y;
exgcd(L, p[i], x, y);
x = (x % p[i] + p[i]) % p[i];
ans = (ans + L * x % res * a[i] % res) % res;
}
return ans;
}
struct Exlucas
{
ll a[N], b[N];
ll Fac(ll n, ll p, ll pk)
{
if (!n)
return 1;
ll p1 = 1;
ll p2 = 1;
for (ll i = 1; i <= pk; i++)
{
if (i % p)
p1 = p1 * i % pk;
}
for (ll i = pk * (n / pk); i <= n; i++)
{
if (i % p)
p2 = p2 * (i % pk) % pk;
}
return qpow(p1, n / pk, pk) * Fac(n / p, p, pk) % pk * p2 % pk;
}
ll G(ll n, ll P)
{
if (n < P)
return 0;
return G(n / P, P) + (n / P);
}
ll C(ll n, ll m, ll p, ll pk)
{

ll fz = Fac(n, p, pk), fm1 = cinv(Fac(m, p, pk), pk), fm2 = cinv(Fac(n - m, p, pk), pk);
ll gg = qpow(p, G(n, p) - G(m, p) - G(n - m, p), pk);
return fz * fm1 % pk * fm2 % pk * gg % pk;
}
ll calc(ll n, ll m, ll p)
{
int cnt = 0;
for (ll i = 2; i * i <= p; i++)
{

if (p % i == 0)
{

ll pk = 1;
while (p % i == 0)
pk *= i, p /= i;

a[++cnt] = C(n, m, i, pk);
b[cnt] = pk;
}
}
if (p > 1)
a[++cnt] = C(n, m, p, p),
b[cnt] = p;

//return 0;
return CRT(b, a, cnt);
}
} t;

int main()
{
ll n, m, p;
scanf("%lld%lld%lld", &n, &m, &p);
//cout << 2333 << endl;
printf("%lld\n", t.calc(n, m, p));
}