Submission #1055270

# Submission time Handle Problem Language Result Execution time Memory
1055270 2024-08-12T16:11:25 Z juicy Ruins 3 (JOI20_ruins3) C++17
100 / 100
544 ms 3164 KB
// 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 time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2516 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2516 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 1 ms 2648 KB Output is correct
14 Correct 0 ms 2652 KB Output is correct
15 Correct 0 ms 2508 KB Output is correct
16 Correct 0 ms 2652 KB Output is correct
17 Correct 0 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2516 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 1 ms 2648 KB Output is correct
14 Correct 0 ms 2652 KB Output is correct
15 Correct 0 ms 2508 KB Output is correct
16 Correct 0 ms 2652 KB Output is correct
17 Correct 0 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 29 ms 3160 KB Output is correct
22 Correct 19 ms 3164 KB Output is correct
23 Correct 24 ms 3164 KB Output is correct
24 Correct 39 ms 3164 KB Output is correct
25 Correct 2 ms 3160 KB Output is correct
26 Correct 18 ms 3164 KB Output is correct
27 Correct 2 ms 3164 KB Output is correct
28 Correct 3 ms 3164 KB Output is correct
29 Correct 2 ms 3164 KB Output is correct
30 Correct 544 ms 3164 KB Output is correct
31 Correct 343 ms 3164 KB Output is correct
32 Correct 417 ms 3160 KB Output is correct
33 Correct 475 ms 3164 KB Output is correct
34 Correct 346 ms 3160 KB Output is correct
35 Correct 422 ms 3164 KB Output is correct
36 Correct 467 ms 3160 KB Output is correct