[BOJ] 11439 이항 계수 5

이항 계수 5

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

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

ll n, k, mod;
bool sieve[MAX_N] {1,1};

ll modpow(int x, int p) {
if(p == 1) return x;
if(p == 0) return 1;

ll res = modpow(x, p>>1);
res = (res * res) % mod;
if(p & 1) res = (res * x) % mod;
return res;
}

ll solve() {
ll res = 1;
for(ll i = 2; i <= n and res; i++) {
if(sieve[i]) continue;

for(ll j = i * i; j <= n; j += i)
sieve[j] = true;

ll c = 0;

for(ll j = i; j <= n; j *= i) {
c += (n / j) - (k / j) - ((n - k) / j);
}

res = res * modpow(i, c) % mod;
}

return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> mod;
cout<<solve();
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/20/PS/BOJ/11439/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.