Submission #520381

# Submission time Handle Problem Language Result Execution time Memory
520381 2022-01-29T18:13:53 Z Alex_tz307 NoM (RMI21_nom) C++17
100 / 100
49 ms 364 KB
#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;
const int kN = 2e3;
int f[1 + 2 * kN], invf[1 + 2 * kN], cnt[1 + kN], dp[2][1 + kN];

void multSelf(int &x, int y) {
  x = (int64_t)x * y % mod;
}

int mult(int x, int y) {
  multSelf(x, y);
  return x;
}

void addSelf(int &x, int y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
}

int invers(int x) {
  int ans = 1, n = mod - 2;
  while (n) {
    if (n % 2) {
      multSelf(ans, x);
    }
    multSelf(x, x);
    n /= 2;
  }
  return ans;
}

void computeFactorials(int n) {
  f[0] = 1;
  for (int i = 1; i <= 2 * n; ++i) {
    f[i] = mult(f[i - 1], i);
  }
  invf[2 * n] = invers(f[2 * n]);
  for (int i = 2 * n - 1; i >= 0; --i) {
    invf[i] = mult(invf[i + 1], i + 1);
  }
}

int arr(int n, int k) {
  return mult(f[n], invf[n - k]);
}

int nck(int n, int k) {
  return mult(f[n], mult(invf[k], invf[n - k]));
}

void testCase() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= 2 * n; ++i) {
    ++cnt[1 + i % m];
  }
  computeFactorials(n);
  int sum = 0;
  dp[0][0] = dp[1][0] = 1;
  for (int i = 1, t = 1; i <= m; ++i, t ^= 1) {
    sum += cnt[i];
    for (int j = 1; j * 2 <= sum; ++j) {
      dp[t][j] = 0;
      for (int k = 0; k <= j && 2 * k <= cnt[i]; ++k) {
        addSelf(dp[t][j], mult(dp[t ^ 1][j - k], mult(arr(cnt[i], 2 * k), nck(j, k))));
      }
    }
  }
  int ans = f[2 * n];
  for (int i = 1; i <= n; ++i) {
    int contribution = mult(nck(n, i), mult(f[2 * n - 2 * i], dp[m % 2][i]));
    if (i % 2) {
      contribution = mod - contribution;
    }
    addSelf(ans, contribution);
  }
  cout << ans << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests = 1;
  for (int tc = 0; tc < tests; ++tc) {
    testCase();
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 268 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 268 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 0 ms 320 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 312 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 268 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 0 ms 320 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 312 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 316 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 268 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 0 ms 320 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 312 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 316 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 204 KB Output is correct
28 Correct 3 ms 316 KB Output is correct
29 Correct 5 ms 316 KB Output is correct
30 Correct 6 ms 336 KB Output is correct
31 Correct 3 ms 332 KB Output is correct
32 Correct 6 ms 332 KB Output is correct
33 Correct 7 ms 332 KB Output is correct
34 Correct 11 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 268 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 0 ms 320 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 312 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 316 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 204 KB Output is correct
28 Correct 3 ms 316 KB Output is correct
29 Correct 5 ms 316 KB Output is correct
30 Correct 6 ms 336 KB Output is correct
31 Correct 3 ms 332 KB Output is correct
32 Correct 6 ms 332 KB Output is correct
33 Correct 7 ms 332 KB Output is correct
34 Correct 11 ms 312 KB Output is correct
35 Correct 18 ms 332 KB Output is correct
36 Correct 36 ms 332 KB Output is correct
37 Correct 14 ms 364 KB Output is correct
38 Correct 23 ms 364 KB Output is correct
39 Correct 28 ms 332 KB Output is correct
40 Correct 49 ms 360 KB Output is correct