-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathADACAROT.cpp
33 lines (33 loc) · 918 Bytes
/
ADACAROT.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
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/ADACAROT/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = (ll)1e9 + 7;
inline ll binary_expo(ll base, int expo) {
ll result = 1, pot = base;
for (int i = 0; (1 << i) <= expo; i++) {
if (expo & (1 << i)) result = (result * pot) % MOD;
pot = (pot * pot) % MOD;
}
return result;
}
int main() {
int N;
while (scanf("%d", &N) != EOF) {
ll result = 0, factorial = 1;
for (int i = 1; i < N; i++) {
ll v1 = binary_expo(i, N - i - 1);
ll v2 = binary_expo(N - i, i - 1);
result += (v1 * v2) % MOD;
result %= MOD;
factorial = (factorial * i) % MOD;
}
factorial *= N;
factorial %= MOD;
result *= factorial;
result %= MOD;
printf("%lld\n", result);
}
return 0;
}