Problem p03989 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, k;
const int N = 2010;
const int MOD = 924844033;
int f[N][N][2][2];
int S[N], T[N];
int fac[N], inv[N], ans;
void ADD(int &t, int d) {
  t += d;
  if (t >= MOD)
    t -= MOD;
}
int powi(int a, int b) {
  int c = 1;
  for (; b; b >>= 1, a = 1ll * a * a % MOD)
    if (b & 1)
      c = 1ll * c * a % MOD;
  return c;
}
int C(int a, int b) { return 1ll * fac[a] * inv[b] % MOD * inv[a - b] % MOD; }
int main() {
  cin >> n >> k;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = 1ll * fac[i - 1] * i % MOD;
  inv[n] = powi(fac[n], MOD - 2);
  for (int i = n - 1; ~i; --i)
    inv[i] = 1ll * inv[i + 1] * (i + 1) % MOD;
  for (int i = 1, s = 0; i <= k; ++i) {
    S[s + 1] = 1;
    s += n / k + (i <= n % k);
    T[s] = 1;
  }
  f[0][0][0][0] = 1;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      for (int l = 0; l < 2; ++l)
        for (int r = 0; r < 2; ++r)
          if (f[i][j][l][r]) {
            cerr << i << " " << j << " " << l << " " << r << " : "
                 << f[i][j][l][r] << endl;
            for (int w = -1; w <= 1; ++w) {
              if (w == -1 && S[i + 1])
                continue;
              if (w == 1 && T[i + 1])
                continue;
              if (l == 1 && w == -1)
                continue;
              // cerr << w << endl;
              ADD(f[i + 1][j + (w != 0)][r && !T[i + 1]][w == 1],
                  f[i][j][l][r]);
            }
          }
  // for (int i = 0; i <= n; ++ i) cerr << f[n][i][0][0] << endl;
  for (int i = 0; i <= n; ++i)
    ADD(ans,
        1ll * f[n][i][0][0] * (i & 1 ? MOD - 1 : 1) % MOD * fac[n - i] % MOD);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, k;
const int N = 2010;
const int MOD = 924844033;
int f[N][N][2][2];
int S[N], T[N];
int fac[N], inv[N], ans;
void ADD(int &t, int d) {
  t += d;
  if (t >= MOD)
    t -= MOD;
}
int powi(int a, int b) {
  int c = 1;
  for (; b; b >>= 1, a = 1ll * a * a % MOD)
    if (b & 1)
      c = 1ll * c * a % MOD;
  return c;
}
int C(int a, int b) { return 1ll * fac[a] * inv[b] % MOD * inv[a - b] % MOD; }
int main() {
  cin >> n >> k;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = 1ll * fac[i - 1] * i % MOD;
  inv[n] = powi(fac[n], MOD - 2);
  for (int i = n - 1; ~i; --i)
    inv[i] = 1ll * inv[i + 1] * (i + 1) % MOD;
  for (int i = 1, s = 0; i <= k; ++i) {
    S[s + 1] = 1;
    s += n / k + (i <= n % k);
    T[s] = 1;
  }
  f[0][0][0][0] = 1;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      for (int l = 0; l < 2; ++l)
        for (int r = 0; r < 2; ++r)
          if (f[i][j][l][r]) {
            // cerr << i << " " << j << " " << l << " " << r << " : " <<
            // f[i][j][l][r] << endl;
            for (int w = -1; w <= 1; ++w) {
              if (w == -1 && S[i + 1])
                continue;
              if (w == 1 && T[i + 1])
                continue;
              if (l == 1 && w == -1)
                continue;
              // cerr << w << endl;
              ADD(f[i + 1][j + (w != 0)][r && !T[i + 1]][w == 1],
                  f[i][j][l][r]);
            }
          }
  // for (int i = 0; i <= n; ++ i) cerr << f[n][i][0][0] << endl;
  for (int i = 0; i <= n; ++i)
    ADD(ans,
        1ll * f[n][i][0][0] * (i & 1 ? MOD - 1 : 1) % MOD * fac[n - i] % MOD);
  cout << ans << endl;
}