Problem p02638 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int MOD;
inline int add(int a, int b) {
  a += b;
  return a >= MOD ? a - MOD : a;
}
inline int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + MOD : a;
}
inline int mul(int a, int b) { return 1ll * a * b % MOD; }
int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1)
      ans = mul(ans, a);
  return ans;
}
const int Q = 255;
int f[Q][Q];
int fac[Q + 1], ifac[Q + 1];
int G(int n, int m, int k) {
  if (n > k || m > k)
    return 0;
  memset(f, 0, sizeof(f));
  for (int j = 0; j <= min(m - 1, k - n); j++)
    f[1][j] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= m; j++) {
      if (!f[i][j])
        continue;
      int r = min(i + k - n, m), l = max(j, m - k + i);
      if (l <= r)
        f[i + 1][l] = add(f[i + 1][l], f[i][j]),
              f[i + 1][r + 1] = sub(f[i + 1][r + 1], f[i][j]);
    }
    for (int j = 1; j <= m; j++)
      f[i + 1][j] = add(f[i + 1][j], f[i + 1][j - 1]);
  }
  int als = 0;
  for (int j = 0; j <= m; j++)
    als = add(als, f[n][j]);
  return mul(als, fac[n + m]);
}
int main() {
  int n, kk;
  scanf("%d%d%d", &n, &kk, &MOD);
  fac[0] = 1;
  for (int i = 1; i <= Q; i++)
    fac[i] = mul(fac[i - 1], i);
  ifac[Q] = ksm(fac[Q], MOD - 2);
  for (int i = Q; i; --i)
    ifac[i - 1] = mul(ifac[i], i);
  int als = mul(kk == n - 1, fac[n]);
  for (int d = 0; d < kk; d++)
    for (int i = 1; i <= n - d - 2; i++)
      als =
          add(als, mul(mul(fac[n], ifac[n - d]), G(i, n - d - i - 1, kk - d)));
  printf("%d\\n", als);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int MOD;
inline int add(int a, int b) {
  a += b;
  return a >= MOD ? a - MOD : a;
}
inline int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + MOD : a;
}
inline int mul(int a, int b) { return 1ll * a * b % MOD; }
int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1)
      ans = mul(ans, a);
  return ans;
}
const int Q = 55;
int f[Q][Q];
int fac[Q + 1], ifac[Q + 1];
int G(int n, int m, int k) {
  if (n > k || m > k)
    return 0;
  memset(f, 0, sizeof(f));
  for (int j = 0; j <= min(m - 1, k - n); j++)
    f[1][j] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= m; j++) {
      if (!f[i][j])
        continue;
      int r = min(i + k - n, m), l = max(j, m - k + i);
      if (l <= r)
        f[i + 1][l] = add(f[i + 1][l], f[i][j]),
              f[i + 1][r + 1] = sub(f[i + 1][r + 1], f[i][j]);
    }
    for (int j = 1; j <= m; j++)
      f[i + 1][j] = add(f[i + 1][j], f[i + 1][j - 1]);
  }
  int als = 0;
  for (int j = 0; j <= m; j++)
    als = add(als, f[n][j]);
  return mul(als, fac[n + m]);
}
int main() {
  int n, kk;
  scanf("%d%d%d", &n, &kk, &MOD);
  fac[0] = 1;
  for (int i = 1; i <= Q; i++)
    fac[i] = mul(fac[i - 1], i);
  ifac[Q] = ksm(fac[Q], MOD - 2);
  for (int i = Q; i; --i)
    ifac[i - 1] = mul(ifac[i], i);
  int als = mul(kk == n - 1, fac[n]);
  for (int d = 0; d < kk; d++)
    for (int i = 1; i <= n - d - 2; i++)
      als =
          add(als, mul(mul(fac[n], ifac[n - d]), G(i, n - d - i - 1, kk - d)));
  printf("%d\\n", als);
  return 0;
}