Problem p03270 (C++)

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

signed main() {
  ios::sync_with_stdio(false);

  const int M = 998244353;

  int K, N;
  cin >> K >> N;

  vector<int> ptwo(1 << 17);
  for (int i = ptwo[0] = 1; i < 1 << 17; ++i)
    ptwo[i] = 2LL * ptwo[i - 1] % M;

  vector<int> fac(1 << 17);
  for (int i = fac[0] = 1; i < 1 << 17; ++i)
    fac[i] = 1LL * fac[i - 1] * i % M;

  auto ipow = [](int v, int p, int m) {
    int r = 1;
    for (int i = p; i; i /= 2) {
      if (i & 1)
        r = 1LL * r * v % m;
      v = 1LL * v * v % m;
    }
    return r;
  };
  vector<int> ifac(1 << 17);
  ifac.back() = ipow(fac.back(), M - 2, M);
  for (int i = (1 << 17) - 1; i; --i)
    ifac[i - 1] = 1LL * ifac[i] * i % M;

  auto choose = [&](int n, int m) {
    assert(-1 < m);
    return n < m ? 0 : 1LL * fac[n] * ifac[m] % M * ifac[n - m] % M;
  };

  auto hchoose = [&](int n, int m) -> int {
    if (n == 0)
      return m == 0;
    return choose(n + m - 1, m);
  };

  for (int s = 2; s < 2 * K + 1; ++s) {
    int q = 0;
    for (int i = 1; i < K + 1; ++i)
      q += i < s - i && 0 < s - i && s - i < K + 1;
    int ans = 0;
    if (s & 1) {
      for (int i = 0; i < min(N, q) + 1; ++i) {
        (ans += 1LL * ptwo[i] * choose(q, i) % M *
                hchoose(K - (2 * q - i), N - i) % M) %= M;
      }
    } else {
      for (int i = 0; i < min(N, q) + 1; ++i) {
        (ans += 1LL * ptwo[i] * choose(q, i) % M *
                (hchoose(K - (2 * q - i + 1), N - i) +
                 hchoose(K - (2 * q - i + 1), N - (i + 1))) %
                M) %= M;
      }
    }
    if (ans < 0)
      ans += M;
    cout << ans << endl;
  }

  return 0;
}


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

signed main() {
  ios::sync_with_stdio(false);

  const int M = 998244353;

  int K, N;
  cin >> K >> N;

  vector<int> ptwo(1 << 17);
  for (int i = ptwo[0] = 1; i < 1 << 17; ++i)
    ptwo[i] = 2LL * ptwo[i - 1] % M;

  vector<int> fac(1 << 17);
  for (int i = fac[0] = 1; i < 1 << 17; ++i)
    fac[i] = 1LL * fac[i - 1] * i % M;

  auto ipow = [](int v, int p, int m) {
    int r = 1;
    for (int i = p; i; i /= 2) {
      if (i & 1)
        r = 1LL * r * v % m;
      v = 1LL * v * v % m;
    }
    return r;
  };
  vector<int> ifac(1 << 17);
  ifac.back() = ipow(fac.back(), M - 2, M);
  for (int i = (1 << 17) - 1; i; --i)
    ifac[i - 1] = 1LL * ifac[i] * i % M;

  auto choose = [&](int n, int m) {
    return m < 0 || n < m ? 0 : 1LL * fac[n] * ifac[m] % M * ifac[n - m] % M;
  };

  auto hchoose = [&](int n, int m) -> int {
    if (n == 0)
      return m == 0;
    return choose(n + m - 1, m);
  };

  for (int s = 2; s < 2 * K + 1; ++s) {
    int q = 0;
    for (int i = 1; i < K + 1; ++i)
      q += i < s - i && 0 < s - i && s - i < K + 1;
    int ans = 0;
    if (s & 1) {
      for (int i = 0; i < min(N, q) + 1; ++i) {
        (ans += 1LL * ptwo[i] * choose(q, i) % M *
                hchoose(K - (2 * q - i), N - i) % M) %= M;
      }
    } else {
      for (int i = 0; i < min(N, q) + 1; ++i) {
        (ans += 1LL * ptwo[i] * choose(q, i) % M *
                (hchoose(K - (2 * q - i + 1), N - i) +
                 hchoose(K - (2 * q - i + 1), N - (i + 1))) %
                M) %= M;
      }
    }
    if (ans < 0)
      ans += M;
    cout << ans << endl;
  }

  return 0;
}