Problem p03270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) int(a.size())
#define clr(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int inf = 1e9;
const ll Inf = 1e18;
const int N = 4e3 + 10;
const int mod = 998244353;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
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; };
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
int n, k, C[N][N], pw[N], f[N][N], s[N][N];
void init() {
  const int n = 4e3;
  for (int i = 0; i <= n; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++)
      C[i][j] = add(C[i - 1][j], C[i - 1][j - 1]);
  }
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = 2ll * pw[i - 1] % mod;
  auto calc = [&](int x) {
    for (int i = 0; i <= n; i++)
      s[x][i] = add(i ? s[x][i - 1] : 0, f[x][i]);
  };
  f[0][0] = 1, calc(0);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n; j++)
      f[i][j] = add(f[i - 1][j], j ? 2ll * s[i - 1][j - 1] % mod : 0);
    calc(i);
  }
}
int g(int i, int j) {
  if (i == 0)
    return j == 0;
  return C[i + j - 1][i - 1];
}
int solve(int x, int z, int n) {
  int ret = 0;
  for (int i = 0; i <= n; i++)
    inc(ret, 1ll * f[z][i] * g(x - z - z, n - i) % mod);
  return ret;
}
int solve(int x) {
  int y = 0, z = 0;
  for (int i = 1; i <= k; i++) {
    int j = x - i;
    if (i == j)
      ++y;
    else if (i < j && j <= k)
      ++z;
  }
  int ret = solve(k - y, z, n);
  if (y)
    inc(ret, solve(k - y, z, n - 1));
  return ret;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("arc102e.in", "r", stdin);
  freopen("arc102e.out", "w", stdout);
#endif
  k = gi(), n = gi(), init();
  for (int i = 2; i <= 2 * k; i++)
    printf("%d\n", solve(i));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) int(a.size())
#define clr(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int inf = 1e9;
const ll Inf = 1e18;
const int N = 4e3 + 10;
const int mod = 998244353;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
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; };
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
int n, k, C[N][N], pw[N], f[N][N], s[N][N];
void init() {
  const int n = 4e3;
  for (int i = 0; i <= n; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++)
      C[i][j] = add(C[i - 1][j], C[i - 1][j - 1]);
  }
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = 2ll * pw[i - 1] % mod;
  auto calc = [&](int x) {
    for (int i = 0; i <= n; i++)
      s[x][i] = add(i ? s[x][i - 1] : 0, f[x][i]);
  };
  f[0][0] = 1, calc(0);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n; j++)
      f[i][j] = add(f[i - 1][j], j ? 2ll * s[i - 1][j - 1] % mod : 0);
    calc(i);
  }
}
int g(int i, int j) {
  if (i == 0)
    return j == 0;
  return C[i + j - 1][i - 1];
}
int solve(int x, int z, int n) {
  int ret = 0;
  for (int i = 0; i <= n; i++)
    inc(ret, 1ll * f[z][i] * g(x - z - z, n - i) % mod);
  return ret;
}
int solve(int x) {
  int y = 0, z = 0;
  for (int i = 1; i <= k; i++) {
    int j = x - i;
    if (i == j)
      ++y;
    else if (i < j && j <= k)
      ++z;
  }
  int ret = solve(k - y, z, n);
  if (y)
    inc(ret, solve(k - y, z, n - 1));
  return ret;
}
int main() {
  k = gi(), n = gi(), init();
  for (int i = 2; i <= 2 * k; i++)
    printf("%d\n", solve(i));
  return 0;
}