Submission #1055270

#TimeUsernameProblemLanguageResultExecution timeMemory
1055270juicyRuins 3 (JOI20_ruins3)C++17
100 / 100
544 ms3164 KiB
// https://www.cnblogs.com/legendstane/p/loj-3276-uoj-506-joisc-2020-day-2-ruins-2-solution-counting-dp.html #include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #endif const int N = 605, MD = 1e9 + 7; int n; int dp[N], f[N][N], C[N][N], fact[N]; bool spec[2 * N]; int add(int a, int b) { if ((a += b) >= MD) { a -= MD; } return a; } int bpow(int a, int b = MD - 2) { int res = 1; for (; b; a = (long long) a * a % MD, b /= 2) { if (b & 1) { res = (long long) res * a % MD; } } return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 1; i <= n; ++i) { int x; cin >> x; spec[x] = 1; } f[0][0] = 1; fact[0] = 1; for (int i = 0; i <= n; ++i) { fact[i + 1] = (long long) fact[i] * (i + 1) % MD; C[i][0] = 1; for (int j = 1; j <= i; ++j) { C[i][j] = add(C[i - 1][j], C[i - 1][j - 1]); } for (int j = i; j <= min(i * 2, n); ++j) { f[i + 1][j] = add(f[i + 1][j], f[i][j]); f[i + 1][j + 1] = add(f[i + 1][j + 1], 2 * f[i][j] % MD); f[i + 1][j + 2] = add(f[i + 1][j + 2], f[i][j]); } } int cnt = 0; dp[0] = 1; for (int i = 2 * n; i > 0; --i) { if (spec[i]) { for (int j = n - 1; j >= 0; --j) { if (dp[j]) { for (int k = 1; j + k <= n; ++k) { dp[j + k] = add(dp[j + k], (long long) dp[j] * C[cnt - j][k - 1] % MD * fact[k - 1] % MD * f[k - 1][k - 1] % MD * (k + 1) % MD); } } } ++cnt; } else { int x = 2 * n - i - cnt; for (int j = 0; j <= n; ++j) { dp[j] = (long long) dp[j] * (j - x) % MD; } } } cout << (long long) dp[n] * bpow(bpow(2, n)) % MD; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...