-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_13174.cpp
More file actions
45 lines (37 loc) · 1.03 KB
/
BOJ_13174.cpp
File metadata and controls
45 lines (37 loc) · 1.03 KB
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 <iostream>
#include <vector>
using namespace std;
const long long MOD = 1000000007;
vector<long long> factorial;
long long get_pow(long long num, int cnt) {
if (cnt == 0)
return 1;
long long res = get_pow(num, cnt / 2);
if (cnt % 2 == 1)
return (((res * res) % MOD) * num) % MOD;
else
return (res * res) % MOD;
}
long long mod_inv(long long num) { return get_pow(num, MOD - 2) % MOD; }
int main(void) {
long long n, k;
cin >> n >> k;
factorial.resize(n + 2);
factorial[0] = 1;
for (int i = 1; i <= n + 1; i++)
factorial[i] = (factorial[i - 1] * i) % MOD;
long long k_pow = 1;
for (int i = 0; i < n / 2; i++)
k_pow = (k_pow * k) % MOD;
long long result = 0;
for (long long i = n / 2; i <= n; i++) {
long long j = n - i;
long long temp = ((i - j + 1) * factorial[n]) % MOD;
temp = (temp * mod_inv((factorial[i + 1] * factorial[j]) % MOD)) % MOD;
temp = (temp * k_pow) % MOD;
result = (result + temp) % MOD;
k_pow = (k_pow * k) % MOD;
}
cout << result;
return 0;
}