Problem p03989 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define print(v, r)                                                            \
  f(i, 0, r) cout << v[i] << " ";                                              \
  cout << endl;
#define Get(x, y) scanf("%I64d%I64d", &x, &y);
#define is_bit(x, y) (x % (1 << (y + 1)) >= (1 << y))
#define eror(x) cout << #x << '=' << (x) << endl;
#define f_(i, a, b) for (int i = a; i >= b; i--)
#define Gett(x, y) scanf("%d%d", &x, &y);
#define f(i, a, b) for (int i = a; i < b; i++)
#define get(x) scanf("%I64d", &x);
#define All(x) x.begin(), x.end()
#define Add(x, y) x = (x + y) % mod;
#define gett(x) scanf("%d", &x);
#define maxm(a, b) a = max(a, b);
#define minm(a, b) a = min(a, b);
#define lst(x) x[x.size() - 1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N = 2002, mod = 924844033;

int n, k, res, fac[N], dp[N][N][4], ans[N], pd[N];

void calc(int x) {
  f(i, 0, N) f(j, 0, N) f(mask, 0, 4) dp[i][j][mask] = pd[i] = 0;
  dp[0][0][0] = 1;
  f(i, 0, x + 1) f(j, 0, i + 1) f(mask, 0, 4) {
    Add(dp[i + 1][j][mask / 2], dp[i][j][mask]);
    if (mask % 2 == 0 && i)
      Add(dp[i + 1][j + 1][mask / 2], dp[i][j][mask]);
    Add(dp[i + 1][j + 1][mask / 2 + 2], dp[i][j][mask]);
  }
  f(i, 0, x + 1) { f(mask, 0, 2) Add(pd[i], dp[x][i][mask]); }
}
void add(int x) {
  ans[0] = 1;
  f_(i, n, 0) f(j, 1, i + 1) Add(ans[i], 1ll * ans[i - j] * pd[j] % mod);
}
void solve() {
  fac[0] = 1;
  f(i, 1, N) fac[i] = 1ll * fac[i - 1] * i % mod;
  f(i, 0, n + 1) {
    if (i & 1)
      Add(res, mod - 1ll * ans[i] * fac[n - i] %
                         mod) else Add(res, 1ll * ans[i] * fac[n - i] % mod);
  }
  cout << res;
}

int main() {
  cin >> n >> k;
  calc((n + k - 1) / k);
  f(i, 0, n % k) add((n + k - 1) / k);
  calc(n / k);
  f(i, 0, k - (n % k)) add(n / k);
  solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define print(v, r)                                                            \
  f(i, 0, r) cout << v[i] << " ";                                              \
  cout << endl;
#define Get(x, y) scanf("%I64d%I64d", &x, &y);
#define is_bit(x, y) (x % (1 << (y + 1)) >= (1 << y))
#define eror(x) cout << #x << '=' << (x) << endl;
#define f_(i, a, b) for (int i = a; i >= b; i--)
#define Gett(x, y) scanf("%d%d", &x, &y);
#define f(i, a, b) for (int i = a; i < b; i++)
#define get(x) scanf("%I64d", &x);
#define All(x) x.begin(), x.end()
#define Add(x, y) x = (x + y) % mod;
#define gett(x) scanf("%d", &x);
#define maxm(a, b) a = max(a, b);
#define minm(a, b) a = min(a, b);
#define lst(x) x[x.size() - 1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N = 2002, mod = 924844033;

int n, k, res, fac[N], dp[N][N][4], ans[N], pd[N];

void calc(int x) {
  f(i, 0, N) f(j, 0, N) f(mask, 0, 4) dp[i][j][mask] = pd[i] = 0;
  dp[0][0][0] = 1;
  f(i, 0, x + 1) f(j, 0, i + 1) f(mask, 0, 4) {
    Add(dp[i + 1][j][mask / 2], dp[i][j][mask]);
    if (mask % 2 == 0 && i)
      Add(dp[i + 1][j + 1][mask / 2], dp[i][j][mask]);
    Add(dp[i + 1][j + 1][mask / 2 + 2], dp[i][j][mask]);
  }
  f(i, 0, x + 1) { f(mask, 0, 2) Add(pd[i], dp[x][i][mask]); }
}
void add(int x) {
  ans[0] = 1;
  f_(i, n, 0) f(j, 1, min(i, x) + 1)
      Add(ans[i], 1ll * ans[i - j] * pd[j] % mod);
}
void solve() {
  fac[0] = 1;
  f(i, 1, N) fac[i] = 1ll * fac[i - 1] * i % mod;
  f(i, 0, n + 1) {
    if (i & 1)
      Add(res, mod - 1ll * ans[i] * fac[n - i] %
                         mod) else Add(res, 1ll * ans[i] * fac[n - i] % mod);
  }
  cout << res;
}

int main() {
  cin >> n >> k;
  calc((n + k - 1) / k);
  f(i, 0, n % k) add((n + k - 1) / k);
  calc(n / k);
  f(i, 0, k - (n % k)) add(n / k);
  solve();
}