[BOJ] 11402 이항 계수 4

이항 계수 4

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
#include <bits/stdc++.h>

#define MAX_N 2001
#define ll long long
using namespace std;

ll n, r, mod;
ll dp[MAX_N][MAX_N];

vector<ll> kRadix(ll n, ll k) {
vector<ll> radix;
while(n) {
radix.push_back(n % k);
n /= k;
}
return radix;
}

ll comb(ll n, ll r) {
if(n < r) return 0;
if(n / 2 < r) r = n - r;

ll& res = dp[n][r];
if(res != -1) return res;
if(r == 0) return res = 1;
if(r == 1) return res = n;
return res = (comb(n - 1, r - 1) + comb(n - 1, r)) % mod;
}

ll solve() {
auto N = kRadix(n, mod);
auto R = kRadix(r, mod);

ll res = 1, mi = min(N.size(), R.size());

for(ll i = 0; i < mi; i++) {
res = res * comb(N[i], R[i]) % mod;
}

return res;
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(dp,-1,sizeof(dp));
cin >> n >> r >> mod;

cout<<solve();
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/20/PS/BOJ/11402/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.