Submission #895487

# Submission time Handle Problem Language Result Execution time Memory
895487 2023-12-30T03:18:23 Z box Ruins 3 (JOI20_ruins3) C++17
100 / 100
158 ms 3416 KB
#include <bits/stdc++.h>
using namespace std;

#define ar array
#define sz(v) int(std::size(v))
using i64 = long long;

const int MAXN = 606;
const int MOD = 1e9 + 7;

int N;
bool use[MAXN * 2];
int take[MAXN][MAXN];
int nck[MAXN][MAXN];
int dp[MAXN + 1], ndp[MAXN + 1];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N;
    for (int i = 0; i < N; i++) {
        int x;
        cin >> x;
        use[x] = 1;
    }
    take[0][0] = 1;
    for (int n = 1; n <= N; n++) for (int k = 0; k <= n; k++) {
        i64 x = take[n - 1][k];
        if (k >= 1) x += (i64) take[n - 1][k - 1] * 2 * k;
        if (k >= 2) x += (i64) take[n - 1][k - 2] * k * (k - 1);
        take[n][k] = x % MOD;
    }
    for (int n = 0; n <= N; n++) {
        nck[n][0] = 1;
        for (int k = 1; k <= n; k++) {
            nck[n][k] = nck[n - 1][k] + nck[n - 1][k - 1];
            if (nck[n][k] >= MOD) nck[n][k] -= MOD;
        }
    }
    int cnt_rem = 0, cnt_use = 0;
    dp[0] = 1;
    for (int i = N * 2; i >= 1; i--) {
        fill(ndp, ndp + N + 1, 0);
        if (!use[i]) {
            for (int j = cnt_rem; j <= cnt_use; j++) if (dp[j])
                ndp[j] = (i64) dp[j] * (j - cnt_rem) % MOD;
            cnt_rem++;
        } else {
            for (int j = cnt_rem; j <= cnt_use; j++) if (dp[j]) {
                ndp[j] = (ndp[j] + dp[j]) % MOD;
                for (int k = 1; j + k <= cnt_use + 1; k++)
                    ndp[j + k] = (ndp[j + k] + (i64) dp[j] * (k + 1) % MOD * nck[cnt_use - j][k - 1] % MOD * take[k - 1][k - 1]) % MOD;
            }
            cnt_use++;
        }
        swap(dp, ndp);
    }
    i64 ans = dp[N];
    for (int i = 0; i < N; i++) ans = ans * (MOD + 1) / 2 % MOD;
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2408 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2408 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2408 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2520 KB Output is correct
21 Correct 3 ms 3164 KB Output is correct
22 Correct 4 ms 3336 KB Output is correct
23 Correct 3 ms 3164 KB Output is correct
24 Correct 5 ms 3332 KB Output is correct
25 Correct 2 ms 3164 KB Output is correct
26 Correct 3 ms 3164 KB Output is correct
27 Correct 2 ms 3416 KB Output is correct
28 Correct 2 ms 3160 KB Output is correct
29 Correct 2 ms 3164 KB Output is correct
30 Correct 158 ms 3312 KB Output is correct
31 Correct 78 ms 3160 KB Output is correct
32 Correct 116 ms 3416 KB Output is correct
33 Correct 142 ms 3312 KB Output is correct
34 Correct 78 ms 3160 KB Output is correct
35 Correct 115 ms 3312 KB Output is correct
36 Correct 146 ms 3164 KB Output is correct