-
Notifications
You must be signed in to change notification settings - Fork 0
/
lucas.cpp
63 lines (48 loc) · 1.16 KB
/
lucas.cpp
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
// BOJ 11402 이항 계수 4
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
ll ipow(ll base, ll exp, ll MOD) {
ll ret = 1;
while (exp) {
if (exp & 1)
ret = ret * base % MOD;
exp >>= 1;
base = base * base % MOD;
}
return ret;
}
ll inverse(ll x, ll MOD) { return ipow(x, MOD - 2, MOD); }
ll comb(ll n, ll k, vector<ll> &fac, vector<ll> &inv, ll MOD) {
if (n < k)
return 0;
return fac[n] * inv[k] % MOD * inv[n - k] % MOD;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, k, MOD;
cin >> n >> k >> MOD;
k = min(k, n - k);
vector<ll> fac(MOD);
fac[0] = 1;
for (int i = 1; i < MOD; i++)
fac[i] = fac[i - 1] * i % MOD;
vector<ll> inv(MOD);
inv.bk = inverse(fac.bk, MOD);
for (int i = MOD - 2; i >= 0; i--)
inv[i] = inv[i + 1] * (i + 1) % MOD;
ll ans = 1;
while (k) {
ans = ans * comb(n % MOD, k % MOD, fac, inv, MOD) % MOD;
n /= MOD;
k /= MOD;
}
cout << ans;
}