Problem p03989 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;

const int maxn = 4000 + 5;
const int mod = 924844033;
const int oo = 0x3f3f3f3f;

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

#define fst first
#define snd second
#define pb push_back

template <typename T> T read() {
  T n(0), f(1);
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    n = n * 10 + ch - 48;
  return n * f;
}

int fac[maxn];
int f[maxn][maxn][2];
bool vis[maxn][2], a[maxn];

int main() {
#ifndef ONLINE_JUDGE
  freopen("data.txt", "r", stdin);
  freopen("ans.txt", "w", stdout);
#endif

  int n, k, s = 0;
  n = read<int>(), k = read<int>();

  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1LL * fac[i - 1] * i % mod;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 2; j++)
      if (!vis[i][j]) {
        int u = i, v = j, len = 0;
        while (u <= n) {
          vis[u][v] = 1;
          ++len;
          u += k, v ^= 1;
        }
        a[s += len] = 1;
      }
  }

  f[1][0][0] = 1;

  for (int i = 2; i <= s; i++) {
    for (int j = 0; j <= n; j++) {
      (f[i][j][0] += f[i - 1][j][0] + f[i - 1][j][1]) %= mod;
      if (!a[i - 1])
        (f[i][j][1] += f[i - 1][j - 1][0]) %= mod;
    }
  }

  static int M[maxn];
  for (int i = 0; i <= n; i++)
    M[i] = (f[s][i][0] + f[s][i][1]) % mod;

  long long ans = 0;
  for (int i = 0; i <= n; i++)
    ans = (ans + (i & 1 ? -1LL : 1LL) * fac[n - i] * M[i] % mod + mod) % mod;
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;

const int maxn = 4000 + 5;
const int mod = 924844033;
const int oo = 0x3f3f3f3f;

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

#define fst first
#define snd second
#define pb push_back

template <typename T> T read() {
  T n(0), f(1);
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    n = n * 10 + ch - 48;
  return n * f;
}

int fac[maxn];
int f[maxn][maxn][2];
bool vis[maxn][2], a[maxn];

int main() {
#ifndef ONLINE_JUDGE
  // freopen("data.txt", "r", stdin);
  // freopen("ans.txt", "w", stdout);
#endif

  int n, k, s = 0;
  n = read<int>(), k = read<int>();

  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1LL * fac[i - 1] * i % mod;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 2; j++)
      if (!vis[i][j]) {
        int u = i, v = j, len = 0;
        while (u <= n) {
          vis[u][v] = 1;
          ++len;
          u += k, v ^= 1;
        }
        a[s += len] = 1;
      }
  }

  f[1][0][0] = 1;

  for (int i = 2; i <= s; i++) {
    for (int j = 0; j <= n; j++) {
      (f[i][j][0] += f[i - 1][j][0] + f[i - 1][j][1]) %= mod;
      if (!a[i - 1])
        (f[i][j][1] += f[i - 1][j - 1][0]) %= mod;
    }
  }

  static int M[maxn];
  for (int i = 0; i <= n; i++)
    M[i] = (f[s][i][0] + f[s][i][1]) % mod;

  long long ans = 0;
  for (int i = 0; i <= n; i++)
    ans = (ans + (i & 1 ? -1LL : 1LL) * fac[n - i] * M[i] % mod + mod) % mod;
  printf("%lld\n", ans);
  return 0;
}