Problem p03270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const ll mod = 998244353;
const int maxn = 4010;
int n, k;
ll jc[maxn], iv[maxn], ivjc[maxn], mi[maxn], res[maxn];
ll c(ll x, ll y) {
  if (x < y)
    return 0;
  return jc[x] * ivjc[y] % mod * ivjc[x - y] % mod;
}
ll solve(int N, int K, int b) {
  ll ans = c(N + b - 1, b - 1);
  if (!b) {
    ans = 0;
    for (int q = 1; q <= K; q++)
      ans += c(N - 1, q - 1) * mi[q] % mod * c(K, q) % mod, ans %= mod;
    return ans;
  }
  for (int x = 1; x <= N; x++)
    for (int q = 1; q <= min(x, K); q++)
      ans += c(N - x + b - 1, b - 1) * c(x - 1, q - 1) % mod * mi[q] % mod *
             c(K, q) % mod,
          ans %= mod;
  return ans;
}
int main() {
  // freopen("1.txt","r",stdin);
  read(k);
  read(n);
  jc[0] = iv[0] = ivjc[0] = 1;
  jc[1] = iv[1] = ivjc[1] = 1;
  for (int i = 2; i < maxn; i++) {
    jc[i] = jc[i - 1] * i % mod;
    iv[i] = iv[mod % i] * (mod - mod / i) % mod;
    ivjc[i] = ivjc[i - 1] * iv[i] % mod;
  }
  mi[0] = 1;
  for (int i = 1; i < maxn; i++)
    mi[i] = mi[i - 1] * 2 % mod;
  for (int i = 2; i <= k + 1; i++) {
    int tmp = i / 2;
    if (i % 2 == 0)
      res[i] =
          (solve(n, tmp - 1, k - i + 1) + solve(n - 1, tmp - 1, k - i + 1)) %
          mod;
    else
      res[i] = solve(n, tmp, k - i + 1);
  }
  for (int i = 2; i <= k + 1; i++)
    printf("%lld\n", res[i]);
  for (int i = k; i >= 2; i--)
    printf("%lld\n", res[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const ll mod = 998244353;
const int maxn = 4010;
int n, k;
ll jc[maxn], iv[maxn], ivjc[maxn], mi[maxn], res[maxn];
ll c(ll x, ll y) {
  if (x < y)
    return 0;
  return jc[x] * ivjc[y] % mod * ivjc[x - y] % mod;
}
ll solve(int N, int K, int b) {
  ll ans = c(N + b - 1, b - 1);
  if (!b) {
    ans = 0;
    for (int q = 1; q <= K; q++)
      ans += c(N - 1, q - 1) * mi[q] % mod * c(K, q) % mod, ans %= mod;
    return ans;
  }
  for (int q = 1; q <= min(N, K); q++)
    ans += c(N + b - 1, q + b - 1) * mi[q] % mod * c(K, q) % mod, ans %= mod;
  return ans;
}
int main() {
  // freopen("1.txt","r",stdin);
  read(k);
  read(n);
  jc[0] = iv[0] = ivjc[0] = 1;
  jc[1] = iv[1] = ivjc[1] = 1;
  for (int i = 2; i < maxn; i++) {
    jc[i] = jc[i - 1] * i % mod;
    iv[i] = iv[mod % i] * (mod - mod / i) % mod;
    ivjc[i] = ivjc[i - 1] * iv[i] % mod;
  }
  mi[0] = 1;
  for (int i = 1; i < maxn; i++)
    mi[i] = mi[i - 1] * 2 % mod;
  for (int i = 2; i <= k + 1; i++) {
    int tmp = i / 2;
    if (i % 2 == 0)
      res[i] =
          (solve(n, tmp - 1, k - i + 1) + solve(n - 1, tmp - 1, k - i + 1)) %
          mod;
    else
      res[i] = solve(n, tmp, k - i + 1);
  }
  for (int i = 2; i <= k + 1; i++)
    printf("%lld\n", res[i]);
  for (int i = k; i >= 2; i--)
    printf("%lld\n", res[i]);
  return 0;
}