Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = j; i <= k; ++i)
#define dep(i, j, k) for (int i = j; i >= k; --i)
#define inv(x) (ksm(x, P - 2))

using namespace std;

const int maxn = 4e3 + 5;
const int P = 924844033;

int n, k, f[2005][2005], fac[maxn], ifac[maxn], vis[maxn], a[maxn], tot,
    sum[maxn];

inline int read() {
  char ch = getchar();
  int u = 0, f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    u = u * 10 + ch - 48;
    ch = getchar();
  }
  return u * f;
}

inline int pls(int x, int y) {
  x += y;
  return x >= P ? x - P : x;
}
inline int dec(int x, int y) {
  x -= y;
  return x < 0 ? x + P : x;
}
inline int ksm(int x, int k, int rnt = 1) {
  for (int i = k; i; i >>= 1, x = 1ll * x * x % P)
    if (i & 1)
      rnt = 1ll * rnt * x % P;
  return rnt;
}

const int inv2 = inv(2);

inline int C(int n, int m) {
  return 1ll * fac[n] * ifac[n - m] % P * ifac[m] % P;
}

int main() {
  n = read();
  k = read();
  fac[0] = 1;
  int N = n;
  rep(i, 1, N) fac[i] = 1ll * fac[i - 1] * i % P;
  ifac[N] = inv(fac[N]);
  dep(i, N - 1, 0) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % P;
  rep(i, 1, n) rep(o, 0, 1) if (!vis[i + o * n]) {
    int con = 0;
    for (int j = i, ty = o; !vis[j + ty * n] && j <= n; j += k, ty ^= 1)
      con++, vis[j + ty * n] = 1;
    a[++tot] = con - 1;
  }
  f[0][0] = 1;
  rep(i, 1, tot) sum[i] = sum[i - 1] + (a[i] + 1 >> 1);
  rep(i, 1, tot) rep(j, 0, sum[i - 1]) rep(k, 0, (a[i] + 1) / 2) f[i][j + k] =
      pls(f[i][j + k], 1ll * f[i - 1][j] * C(a[i] - k + 1, k) % P);
  int ans = 0;
  rep(i, 0, n) if (i & 1) ans = dec(ans, 1ll * f[tot][i] * fac[n - i] % P);
  else ans = pls(ans, 1ll * f[tot][i] * fac[n - i] % P);
  cout << ans << endl;
  return 0;
}
/*
 */

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = j; i <= k; ++i)
#define dep(i, j, k) for (int i = j; i >= k; --i)
#define inv(x) (ksm(x, P - 2))

using namespace std;

const int maxn = 4e3 + 5;
const int P = 924844033;

int n, k, f[maxn][maxn], fac[maxn], ifac[maxn], vis[maxn], a[maxn], tot,
    sum[maxn];

inline int read() {
  char ch = getchar();
  int u = 0, f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    u = u * 10 + ch - 48;
    ch = getchar();
  }
  return u * f;
}

inline int pls(int x, int y) {
  x += y;
  return x >= P ? x - P : x;
}
inline int dec(int x, int y) {
  x -= y;
  return x < 0 ? x + P : x;
}
inline int ksm(int x, int k, int rnt = 1) {
  for (int i = k; i; i >>= 1, x = 1ll * x * x % P)
    if (i & 1)
      rnt = 1ll * rnt * x % P;
  return rnt;
}

const int inv2 = inv(2);

inline int C(int n, int m) {
  return 1ll * fac[n] * ifac[n - m] % P * ifac[m] % P;
}

int main() {
  n = read();
  k = read();
  fac[0] = 1;
  int N = n;
  rep(i, 1, N) fac[i] = 1ll * fac[i - 1] * i % P;
  ifac[N] = inv(fac[N]);
  dep(i, N - 1, 0) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % P;
  rep(i, 1, n) rep(o, 0, 1) if (!vis[i + o * n]) {
    int con = 0;
    for (int j = i, ty = o; !vis[j + ty * n] && j <= n; j += k, ty ^= 1)
      con++, vis[j + ty * n] = 1;
    a[++tot] = con - 1;
  }
  f[0][0] = 1;
  rep(i, 1, tot) sum[i] = sum[i - 1] + (a[i] + 1 >> 1);
  rep(i, 1, tot) rep(j, 0, sum[i - 1]) rep(k, 0, (a[i] + 1) / 2) f[i][j + k] =
      pls(f[i][j + k], 1ll * f[i - 1][j] * C(a[i] - k + 1, k) % P);
  int ans = 0;
  rep(i, 0, n) if (i & 1) ans = dec(ans, 1ll * f[tot][i] * fac[n - i] % P);
  else ans = pls(ans, 1ll * f[tot][i] * fac[n - i] % P);
  cout << ans << endl;
  return 0;
}
/*
 */