This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |