#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
using ll = long long;
int mult(int x, int y) {
return (long long) x * y % MOD;
}
void add(int &x, int y) {
x += y;
if (x >= MOD) x -= MOD;
}
int main() {
ios_base::sync_with_stdio(false);
int N;
cin >> N;
vector<bool> alive(2 * N);
for (int i = 0; i < N; ++i) {
int x;
cin >> x;
alive[--x] = true;
}
vector<int> ways(N + 1);
{
vector<vector<int>> dp(N + 1, vector<int>(N + 1));
dp[0][0] = 1;
for (int i = 1; i <= N; ++i) {
for (int j = 0; j <= i; ++j) {
dp[i][j] = dp[i - 1][j];
if (j > 0) add(dp[i][j], mult(2 * j, dp[i - 1][j - 1]));
if (j > 1) add(dp[i][j], mult(j * (j - 1), dp[i - 1][j - 2]));
}
}
for (int i = 1; i <= N; ++i) {
ways[i] = mult(dp[i - 1][i - 1], i + 1);
}
}
vector<vector<int>> C(N + 1, vector<int>(N + 1));
for (int i = 0; i <= N; ++i) {
C[i][0] = 1;
for (int j = 1; j <= i; ++j) {
C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
if (C[i][j] >= MOD) C[i][j] -= MOD;
}
}
vector<int> dp(N + 1);
dp[0] = 1;
int cnt0 = 0;
int cnt1 = 0;
for (int i = 2 * N - 1; i >= 0; --i) {
vector<int> ndp(N + 1);
if (alive[i]) {
for (int j = cnt0; j <= cnt1; ++j) {
if (!dp[j]) continue;
add(ndp[j], dp[j]);
for (int k = j + 1; k <= cnt1 + 1; ++k) {
add(ndp[k], mult(mult(C[cnt1 - j][k - j - 1], ways[k - j]), dp[j]));
}
}
++cnt1;
} else {
for (int j = cnt0; j <= cnt1; ++j) {
add(ndp[j], mult(j - cnt0, dp[j]));
}
++cnt0;
}
dp = ndp;
}
int ans = dp[N];
for (int i = 0; i < N; ++i) ans = mult(ans, (MOD + 1) >> 1);
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
392 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
392 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
9 ms |
1792 KB |
Output is correct |
22 |
Correct |
10 ms |
1920 KB |
Output is correct |
23 |
Correct |
8 ms |
1792 KB |
Output is correct |
24 |
Correct |
10 ms |
1792 KB |
Output is correct |
25 |
Correct |
8 ms |
1792 KB |
Output is correct |
26 |
Correct |
9 ms |
1792 KB |
Output is correct |
27 |
Correct |
12 ms |
1792 KB |
Output is correct |
28 |
Correct |
8 ms |
1792 KB |
Output is correct |
29 |
Correct |
8 ms |
1792 KB |
Output is correct |
30 |
Correct |
202 ms |
1792 KB |
Output is correct |
31 |
Correct |
108 ms |
1792 KB |
Output is correct |
32 |
Correct |
152 ms |
1792 KB |
Output is correct |
33 |
Correct |
188 ms |
1792 KB |
Output is correct |
34 |
Correct |
124 ms |
1792 KB |
Output is correct |
35 |
Correct |
158 ms |
1792 KB |
Output is correct |
36 |
Correct |
193 ms |
1792 KB |
Output is correct |