// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
template<class T> using V = vector<T>;
using vi = V<int>;
const int MOD = 1e9 + 7;
int main() {
cin.tie(0)->sync_with_stdio(0);
auto add = [&](int &x, int y) {
x += y; if (x >= MOD) x -= MOD;
};
auto mul = [&](int &x, int y) {
x = (x * 1LL * y) % MOD;
};
auto POW = [&](int a, int p) {
int ans = 1;
for(; p; p /= 2, mul(a, a)) if (p & 1) mul(ans, a);
return ans;
};
auto INV = [&](int a) {
return POW(a, MOD - 2);
};
int N; cin >> N;
vi F = {1}, IF = {1}; for(int i = 1; i <= N; i++) {
F.pb(F.back()); mul(F.back(), i);
IF.pb(INV(F.back()));
}
auto nCk = [&](int n, int k) {
int ways = F[n];
mul(ways, IF[n - k]); mul(ways, IF[k]);
return ways;
};
V<vi> dp(N + 1, vi(2)), ndp(N + 1, vi(2)); // dp[index][left][happy] = # of ways
dp[N][0] = 1;
for(int i = 1; i <= N; i++) {
for(int x = 0; x <= N; x++) for(int h = 0; h < 2; h++) if (dp[x][h]) {
for(int g = 0; g <= x; g++) {
int nx = x - g, nh = h | (g == i);
int ways = dp[x][h]; mul(ways, nCk(x, g));
// cout << i << " " << x << " " << h << " => " << nx << " " << nh << " => " << ways << endl;
add(ndp[nx][nh], ways);
}
}
dp.swap(ndp);
ndp = V<vi>(N + 1, vi(2));
}
cout << dp[0][1] << nl;
exit(0-0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
137 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
344 KB |
Output is correct |
19 |
Correct |
31 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
186 ms |
492 KB |
Output is correct |
22 |
Correct |
20 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
39 ms |
344 KB |
Output is correct |
25 |
Correct |
26 ms |
344 KB |
Output is correct |
26 |
Correct |
53 ms |
488 KB |
Output is correct |
27 |
Correct |
264 ms |
592 KB |
Output is correct |
28 |
Correct |
259 ms |
496 KB |
Output is correct |
29 |
Correct |
262 ms |
496 KB |
Output is correct |
30 |
Correct |
265 ms |
348 KB |
Output is correct |
31 |
Correct |
265 ms |
344 KB |
Output is correct |
32 |
Correct |
272 ms |
500 KB |
Output is correct |
33 |
Correct |
274 ms |
492 KB |
Output is correct |
34 |
Correct |
272 ms |
600 KB |
Output is correct |
35 |
Correct |
274 ms |
344 KB |
Output is correct |
36 |
Correct |
280 ms |
492 KB |
Output is correct |
37 |
Correct |
289 ms |
500 KB |
Output is correct |