#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
constexpr int MOD = 1000000007;
struct Fp {
int x;
Fp(int x = 0) : x(x) {}
Fp& operator+=(const Fp& t) {
if ((x += t.x) >= MOD) x -= MOD;
return *this;
}
Fp& operator-=(const Fp& t) {
if ((x -= t.x) < 0) x += MOD;
return *this;
}
Fp& operator*=(const Fp& t) {
x = (long long)x * t.x % MOD;
return *this;
}
Fp operator+(const Fp& t) const {
return Fp(*this) += t;
}
Fp operator-(const Fp& t) const {
return Fp(*this) -= t;
}
Fp operator*(const Fp& t) const {
return Fp(*this) *= t;
}
Fp pow(int e) const {
Fp ret(1), mul(*this);
while (e > 0) {
if (e & 1) ret *= mul;
mul *= mul;
e >>= 1;
}
return ret;
}
Fp inv() const {
return pow(MOD - 2);
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
vector<Fp> fact(N + 1), inv_fact(N + 1);
fact[0] = 1;
for (int i = 1; i <= N; ++i) {
fact[i] = fact[i - 1] * i;
}
inv_fact[N] = fact[N].inv();
for (int i = N; i >= 1; --i) {
inv_fact[i - 1] = inv_fact[i] * i;
}
vector<Fp> dp(N + 1);
dp[0] = 1;
for (int i = 1; i <= N; ++i) {
vector<Fp> next(N + 1);
for (int j = 0; j <= N; ++j) {
for (int k = 0; k <= j; ++k) {
if (k != i) {
next[j] += dp[j - k] * inv_fact[k];
}
}
}
dp = std::move(next);
}
std::cout << (Fp(N).pow(N) - fact[N] * dp[N]).x << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
4 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
4 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
41 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
9 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
54 ms |
204 KB |
Output is correct |
22 |
Correct |
6 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
12 ms |
204 KB |
Output is correct |
25 |
Correct |
10 ms |
204 KB |
Output is correct |
26 |
Correct |
16 ms |
312 KB |
Output is correct |
27 |
Correct |
82 ms |
316 KB |
Output is correct |
28 |
Correct |
78 ms |
296 KB |
Output is correct |
29 |
Correct |
78 ms |
292 KB |
Output is correct |
30 |
Correct |
86 ms |
292 KB |
Output is correct |
31 |
Correct |
87 ms |
204 KB |
Output is correct |
32 |
Correct |
82 ms |
204 KB |
Output is correct |
33 |
Correct |
80 ms |
304 KB |
Output is correct |
34 |
Correct |
86 ms |
204 KB |
Output is correct |
35 |
Correct |
85 ms |
204 KB |
Output is correct |
36 |
Correct |
82 ms |
204 KB |
Output is correct |
37 |
Correct |
82 ms |
296 KB |
Output is correct |