Submission #259300

# Submission time Handle Problem Language Result Execution time Memory
259300 2020-08-07T14:12:06 Z Bruteforceman Ruins 3 (JOI20_ruins3) C++11
100 / 100
2609 ms 16132 KB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 605;
const long long mod = 1e9 + 7;
long long ways[maxn];
int a[maxn];
int var;
long long mem[maxn][maxn];
long long C[maxn * 2][maxn * 2];
long long fac[] = {1, 1, (mod + 1) / 2};
long long calc(int cur, int take) {
  long long ans = 0;
  if(cur == 1) {
    for(int i = 0; i <= 2; i++) {
      if(take + i == var + 1) {
        return (C[take + i][i] * (fac[2 - i])) % mod;
      }
    }
    return 0;
  }
  if(mem[cur][take] != -1) return mem[cur][take];
  for(int i = 0; i <= 2; i++) {
    if(take + i <= var - cur + 1) {
      long long add = (calc(cur - 1, take + i) * C[take + i][i]) % mod;
      ans += (add * (fac[2 - i])) % mod;
      ans %= mod;
    }
  }
  return mem[cur][take] = ans;
}
long long dp[maxn][maxn];

int main() {
  int n;
  cin >> n;
  for(int i = 1; i <= n; i++) cin >> a[i];
  for(int i = 0; i <= n + 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]) % mod;
    }
  }
  for(int i = 1; i <= n; i++) {
    memset(mem, -1, sizeof mem);
    var = i;
    ways[i] = calc(var, 0);
  }
  memset(mem, -1, sizeof mem);
  dp[n][0] = 1;
  for(int cur = n - 1; cur >= 0; cur--) {
    for(int bound = 0; bound <= n; bound++) {
      dp[cur][bound] = (1LL * dp[cur + 1][bound] * (n - bound - cur)) % mod;
      for(int i = 0; i < bound; i++) {
        int free = a[cur + 1] - (n - bound + (cur + 1));
        if(free >= bound - i) {
          long long add = ((C[free][bound - i] * ways[bound - i] % mod) * dp[cur + 1][i]) % mod;
          dp[cur][bound] += add;
          dp[cur][bound] %= mod;
        }
      }
    }
  } 
  cout << dp[0][n] << endl;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3200 KB Output is correct
2 Correct 3 ms 3328 KB Output is correct
3 Correct 3 ms 3328 KB Output is correct
4 Correct 3 ms 3328 KB Output is correct
5 Correct 3 ms 3328 KB Output is correct
6 Correct 3 ms 3328 KB Output is correct
7 Correct 3 ms 3328 KB Output is correct
8 Correct 3 ms 3328 KB Output is correct
9 Correct 3 ms 3328 KB Output is correct
10 Correct 5 ms 3328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3200 KB Output is correct
2 Correct 3 ms 3328 KB Output is correct
3 Correct 3 ms 3328 KB Output is correct
4 Correct 3 ms 3328 KB Output is correct
5 Correct 3 ms 3328 KB Output is correct
6 Correct 3 ms 3328 KB Output is correct
7 Correct 3 ms 3328 KB Output is correct
8 Correct 3 ms 3328 KB Output is correct
9 Correct 3 ms 3328 KB Output is correct
10 Correct 5 ms 3328 KB Output is correct
11 Correct 9 ms 3968 KB Output is correct
12 Correct 9 ms 3968 KB Output is correct
13 Correct 9 ms 3968 KB Output is correct
14 Correct 9 ms 3968 KB Output is correct
15 Correct 9 ms 3968 KB Output is correct
16 Correct 9 ms 3968 KB Output is correct
17 Correct 9 ms 3968 KB Output is correct
18 Correct 9 ms 3968 KB Output is correct
19 Correct 10 ms 3968 KB Output is correct
20 Correct 10 ms 3968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3200 KB Output is correct
2 Correct 3 ms 3328 KB Output is correct
3 Correct 3 ms 3328 KB Output is correct
4 Correct 3 ms 3328 KB Output is correct
5 Correct 3 ms 3328 KB Output is correct
6 Correct 3 ms 3328 KB Output is correct
7 Correct 3 ms 3328 KB Output is correct
8 Correct 3 ms 3328 KB Output is correct
9 Correct 3 ms 3328 KB Output is correct
10 Correct 5 ms 3328 KB Output is correct
11 Correct 9 ms 3968 KB Output is correct
12 Correct 9 ms 3968 KB Output is correct
13 Correct 9 ms 3968 KB Output is correct
14 Correct 9 ms 3968 KB Output is correct
15 Correct 9 ms 3968 KB Output is correct
16 Correct 9 ms 3968 KB Output is correct
17 Correct 9 ms 3968 KB Output is correct
18 Correct 9 ms 3968 KB Output is correct
19 Correct 10 ms 3968 KB Output is correct
20 Correct 10 ms 3968 KB Output is correct
21 Correct 2110 ms 15648 KB Output is correct
22 Correct 2141 ms 16132 KB Output is correct
23 Correct 2077 ms 15956 KB Output is correct
24 Correct 2142 ms 15864 KB Output is correct
25 Correct 2137 ms 15688 KB Output is correct
26 Correct 2196 ms 15864 KB Output is correct
27 Correct 2078 ms 15904 KB Output is correct
28 Correct 2109 ms 15608 KB Output is correct
29 Correct 1877 ms 15744 KB Output is correct
30 Correct 2581 ms 15868 KB Output is correct
31 Correct 2398 ms 15868 KB Output is correct
32 Correct 2556 ms 15516 KB Output is correct
33 Correct 2609 ms 15876 KB Output is correct
34 Correct 2379 ms 15764 KB Output is correct
35 Correct 2471 ms 15696 KB Output is correct
36 Correct 2590 ms 15868 KB Output is correct