prithivMLmods/Coder-Stat huggingface dataset:
Problem p03268 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

int main() {
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  ans += (n / k) * (n / k) * (n / k);
  if (n % 2 == 0) {
    ans += (n / (k / 2)) * (n / (k / 2)) * (n / (k / 2));
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

int main() {
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  ans += (n / k) * (n / k) * (n / k);
  if (k % 2 == 0) {
    int tmp = (n / k) * k;
    if (tmp + k / 2 <= n)
      ans += (n / k + 1) * (n / k + 1) * (n / k + 1);
    else
      ans += (n / k) * (n / k) * (n / k);
  }
  cout << ans << endl;
}
Problem p03268 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1000000000LL;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, k;
  cin >> n, k;
  vector<ll> num(k, 0);
  for (int i = 1; i <= n; i++)
    num[i % k]++;
  ll res = 0;
  rep(a, k) {
    int b = (k - a) % k;
    int c = (k - a) % k;
    if ((b + c) % k != 0)
      continue;
    res += num[a] * num[b] * num[c];
  }
  cout << res << ln;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1000000000LL;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> num(k, 0);
  for (int i = 1; i <= n; i++)
    num[i % k]++;
  ll res = 0;
  rep(a, k) {
    int b = (k - a) % k;
    int c = (k - a) % k;
    if ((b + c) % k != 0)
      continue;
    res += num[a] * num[b] * num[c];
  }
  cout << res << ln;
}
Problem p03268 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define long long long
using namespace std;
const int N = 1e5 + 1;

int n, k, f[N];
long ans;
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++)
    f[i] = n / k;
  int mod = n % k;
  for (int i = 1; i <= mod; i++)
    ++f[i];
  for (int i = 0; i < k; i++)
    if (2 * i % k == 0)
      ans += 1ll * f[i] * f[i] * f[i];
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define long long long
using namespace std;
const int N = 2e5 + 1;

int n, k, f[N];
long ans;
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++)
    f[i] = n / k;
  int mod = n % k;
  for (int i = 1; i <= mod; i++)
    ++f[i];
  for (int i = 0; i < k; i++)
    if (2 * i % k == 0)
      ans += 1ll * f[i] * f[i] * f[i];
  cout << ans;
}
Problem p03268 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ri register int
#define il inline
#define ll long long
using namespace std;
const int N = 1e6 + 110;
const int MAXN = 110;
const int inf = 0x7fffffff;
const double eps = 1e-8;
il int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, k;
ll ans;
int main() {
  n = read(), k = read();
  int num1 = n / k;
  ans += 1ll * num1 * num1 * num1;
  int num = n / (k / 2);
  ans += (k % 2 == 0) * 1ll * (num - num1) * (num - num1) * (num - num1);
  printf("%lld", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ri register int
#define il inline
#define ll long long
using namespace std;
const int N = 1e6 + 110;
const int MAXN = 110;
const int inf = 0x7fffffff;
const double eps = 1e-8;
il int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, k;
ll ans;
int main() {
  n = read(), k = read();
  int num1 = n / k;
  ans += 1ll * num1 * num1 * num1;
  if (k % 2 == 0) {
    int num = n / (k / 2);
    ans += 1ll * (num - num1) * (num - num1) * (num - num1);
  }
  printf("%lld", ans);
  return 0;
}
Problem p03269 (C++)

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

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int L;
  cin >> L;

  int N = 20;
  int M = 0;
  WGraph res(N);
  for (int i = 0; i < N - 1; ++i) {
    res[i].push_back({i + 1, 0});
    ++M;
  }
  --L;
  int cur = 0;
  for (; cur < N - 1; ++cur) {
    if (L >= (1 << (cur + 1))) {
      res[N - cur - 2].push_back({N - cur - 1, (1 << cur)});
      ++M;
    } else {
      break;
    }
  }
  int mod = L - (1 << cur) + 1;
  int c = 1 << cur;
  for (int i = 0; i < cur + 1; ++i) {
    if (mod & (1 << i)) {
      res[N - cur - 2].push_back({N - i - 1, c});
      ++M;
      c += (1 << i);
    }
  }

  cout << N << ' ' << M << '\n';
  for (int i = 0; i < N; ++i)
    for (auto p : res[i])
      cout << i + 1 << ' ' << p.first + 1 << ' ' << p.second << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int L;
  cin >> L;

  int N = 20;
  int M = 0;
  WGraph res(N);
  for (int i = 0; i < N - 1; ++i) {
    res[i].push_back({i + 1, 0});
    ++M;
  }
  --L;
  int cur = 0;
  for (; cur < N - 1; ++cur) {
    if (L >= (1 << (cur + 1))) {
      res[N - cur - 2].push_back({N - cur - 1, (1 << cur)});
      ++M;
    } else {
      break;
    }
  }
  int mod = L - (1 << cur) + 1;
  int c = 1 << cur;
  if (cur == 19)
    --cur;
  for (int i = 0; i < cur + 1; ++i) {
    if (mod & (1 << i)) {
      res[N - cur - 2].push_back({N - i - 1, c});
      ++M;
      c += (1 << i);
    }
  }

  cout << N << ' ' << M << '\n';
  for (int i = 0; i < N; ++i)
    for (auto p : res[i])
      cout << i + 1 << ' ' << p.first + 1 << ' ' << p.second << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}
Problem p03269 (C++)

Buggy submission (status: Runtime Error):
/*input
4
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
const int mod = 1e9 + 7;
#define int long long
#define pii pair<int, int>
#define f first
#define s second
#define mp make_pair
int m, n;
vector<pii> adjlist[N];
int b, l;
vector<int> bits;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> l;
  m = 0;
  for (int i = 0; (1 << i) <= l; i++)
    b = i;
  for (int i = 0; i < b; i++) {
    adjlist[i + 1].push_back(mp(i + 2, 1 << i));
    adjlist[i + 1].push_back(mp(i + 2, 0));
    n = i + 2;
    m += 2;
  }
  //	l--;
  for (int i = 0; (1 << i) <= l; i++)
    if ((l & (1 << i)))
      bits.push_back(i);

  // if(bits[0]!=0) bits.insert(bits.begin(), 0);
  //	if(((l+1)&l)==0) bits.clear();
  int cur = 1 << bits.back();
  b = bits.size() - 2;
  while (b >= 0) {
    adjlist[bits[b] + 1].push_back(mp(n, cur));
    m++;
    cur += 1 << bits[b];
    b--;
  }
  cout << n << " " << m << endl;
  for (int i = 1; i <= n; i++) {
    for (auto j : adjlist[i]) {
      cout << i << " " << j.f << " " << j.s << endl;
    }
  }
}

Fixed submission:
/*input
4
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 21;
const int mod = 1e9 + 7;
#define int long long
#define pii pair<int, int>
#define f first
#define s second
#define mp make_pair
int m, n;
vector<pii> adjlist[N];
int b, l;
vector<int> bits;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> l;
  m = 0;
  for (int i = 0; (1 << i) <= l; i++)
    b = i;
  for (int i = 0; i < b; i++) {
    adjlist[i + 1].push_back(mp(i + 2, 1 << i));
    adjlist[i + 1].push_back(mp(i + 2, 0));
    n = i + 2;
    m += 2;
  }
  //	l--;
  for (int i = 0; (1 << i) <= l; i++)
    if ((l & (1 << i)))
      bits.push_back(i);

  // if(bits[0]!=0) bits.insert(bits.begin(), 0);
  //	if(((l+1)&l)==0) bits.clear();
  int cur = 1 << bits.back();
  b = bits.size() - 2;
  while (b >= 0) {
    adjlist[bits[b] + 1].push_back(mp(n, cur));
    m++;
    cur += 1 << bits[b];
    b--;
  }
  cout << n << " " << m << endl;
  for (int i = 1; i <= n; i++) {
    for (auto j : adjlist[i]) {
      cout << i << " " << j.f << " " << j.s << endl;
    }
  }
}
Problem p03269 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;

using std::cin;
using std::cout;

int L, limit, pw, point, E;
struct node {
  int x, y, l;
};
std::vector<node> v;

inline void insert(int x, int y, int z) { v.push_back((node){x, y, z}), ++E; }

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> L;
  assert(L < 2);
  for (limit = 1; limit <= L; limit <<= 1, ++point)
    ;
  /*
  if (limit == L) {
      for (int i = 1; i <= point; ++i) insert(i, i + 1, 0), insert(i, i + 1, 1
  << i - 1); cout << point << ' ' << E << '\n'; for (int i = 0; i < E; ++i) cout
  << v[i].x << ' ' << v[i].y << ' ' << v[i].l << '\n'; return 0;
      } */
  for (int i = 1; i < point; ++i)
    insert(i, i + 1, 0), insert(i, i + 1, 1 << i - 1);
  for (int i = 1; i < point; ++i)
    if (L & (1 << i - 1))
      L ^= (1 << i - 1), insert(i, point, L);
  cout << point << ' ' << E << '\n';
  for (int i = 0; i < E; ++i)
    cout << v[i].x << ' ' << v[i].y << ' ' << v[i].l << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;

using std::cin;
using std::cout;

int L, limit, pw, point, E;
struct node {
  int x, y, l;
};
std::vector<node> v;

inline void insert(int x, int y, int z) { v.push_back((node){x, y, z}), ++E; }

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> L;
  for (limit = 1; limit <= L; limit <<= 1, ++point)
    ;
  /*
  if (limit == L) {
      for (int i = 1; i <= point; ++i) insert(i, i + 1, 0), insert(i, i + 1, 1
  << i - 1); cout << point << ' ' << E << '\n'; for (int i = 0; i < E; ++i) cout
  << v[i].x << ' ' << v[i].y << ' ' << v[i].l << '\n'; return 0;
      } */
  for (int i = 1; i < point; ++i)
    insert(i, i + 1, 0), insert(i, i + 1, 1 << i - 1);
  for (int i = 1; i < point; ++i)
    if (L & (1 << i - 1))
      L ^= (1 << i - 1), insert(i, point, L);
  cout << point << ' ' << E << '\n';
  for (int i = 0; i < E; ++i)
    cout << v[i].x << ' ' << v[i].y << ' ' << v[i].l << '\n';
}
Problem p03269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int l, n, m;
struct node {
  int x, y, z;
  node(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {}
};
vector<node> e;
int main() {
  scanf("%d", &l);
  int n = log2(l) + 1;
  int sum = 0;
  for (int i = 1, j = log2(l) - 1; i < n; i++, j--)
    e.push_back(node(i, i + 1, 0)), e.push_back(node(i, i + 1, 1 << j)),
        sum += 1 << j;
  while (sum < l - 1) {
    int s = l - 1 - sum;
    int res = 0;
    for (int i = n - 1, j = 0; i >= 2; i--, j++) {
      res += 1 << j;
      if (res + 1 > s) {
        res -= 1 << j;
        e.push_back(node(1, i + 1, sum + 1));
        sum += res + 1;
        break;
      } else if (res + 1 == s || i == 2) {
        e.push_back(node(1, i, sum + 1));
        sum += res + 1;
        break;
      }
    }
  }
  printf("%d %d\n", n, e.size());
  for (int i = 0; i < e.size(); i++)
    printf("%d %d %d\n", e[i].x, e[i].y, e[i].z);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int l, n, m;
struct node {
  int x, y, z;
  node(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {}
};
vector<node> e;
int main() {
  scanf("%d", &l);
  if (l <= 30) {
    printf("%d %d\n", 2, l);
    for (int i = 0; i < l; i++)
      printf("%d %d %d\n", 1, 2, i);
    return 0;
  }
  int n = log2(l) + 1;
  int sum = 0;
  for (int i = 1, j = log2(l) - 1; i < n; i++, j--)
    e.push_back(node(i, i + 1, 0)), e.push_back(node(i, i + 1, 1 << j)),
        sum += 1 << j;
  while (sum < l - 1) {
    int s = l - 1 - sum;
    int res = 0;
    for (int i = n - 1, j = 0; i >= 2; i--, j++) {
      res += 1 << j;
      if (res + 1 > s) {
        res -= 1 << j;
        e.push_back(node(1, i + 1, sum + 1));
        sum += res + 1;
        break;
      } else if (res + 1 == s || i == 2) {
        e.push_back(node(1, i, sum + 1));
        sum += res + 1;
        break;
      }
    }
  }
  printf("%d %d\n", n, e.size());
  for (int i = 0; i < e.size(); i++)
    printf("%d %d %d\n", e[i].x, e[i].y, e[i].z);
}
Problem p03269 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define maxn 100005
int l, n, m, id[maxn], u[maxn], v[maxn], w[maxn];
void Add(int u1, int v1, int w1) {
  m++;
  u[m] = id[u1], v[m] = id[v1], w[m] = w1;
}
void dfs(int x) {
  if (id[x])
    return;
  if (!x) {
    id[x] = ++n;
    return;
  }
  if (x & 1) {
    dfs(x / 2);
    if (!id[x])
      id[x] = ++n;
    Add(x / 2, x, x / 2 + 1);
    Add(x / 2, x, 0);
  } else {
    dfs(x / 2 - 1);
    if (!id[x])
      id[x] = ++n;
    Add(x / 2 - 1, x, x / 2);
    Add(x / 2 - 1, x, 0);
    Add(0, x, x);
  }
}
int main() {
  scanf("%d", &l);
  l--;
  dfs(l);
  printf("%d %d\n", n, m);
  for (int i = 1; i <= m; i++)
    printf("%d %d %d\n", u[i], v[i], w[i]);
}

Fixed submission:
#include <cstdio>
#define maxn 1000005
int l, n, m, id[maxn], u[maxn], v[maxn], w[maxn];
void Add(int u1, int v1, int w1) {
  m++;
  u[m] = id[u1], v[m] = id[v1], w[m] = w1;
}
void dfs(int x) {
  if (id[x])
    return;
  if (!x) {
    id[x] = ++n;
    return;
  }
  if (x & 1) {
    dfs(x / 2);
    if (!id[x])
      id[x] = ++n;
    Add(x / 2, x, x / 2 + 1);
    Add(x / 2, x, 0);
  } else {
    dfs(x / 2 - 1);
    if (!id[x])
      id[x] = ++n;
    Add(x / 2 - 1, x, x / 2);
    Add(x / 2 - 1, x, 0);
    Add(0, x, x);
  }
}
int main() {
  scanf("%d", &l);
  l--;
  dfs(l);
  printf("%d %d\n", n, m);
  for (int i = 1; i <= m; i++)
    printf("%d %d %d\n", u[i], v[i], w[i]);
}
Problem p03269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
/* clang-format on */

using E = pair<int, int>; // to, weight

int main() {
  int L;
  while (cin >> L) {
    int N;
    while ((1 << N) <= L)
      ++N;
    vec<E, 2> G(N);
    for (int i = 0; i + 1 < N; i++) {
      G[i].push_back(E(i + 1, 0));
      G[i].push_back(E(i + 1, 1 << i));
    }
    int K = 1 << (N - 1);
    for (int i = N - 2; i >= 0; i--) {
      if ((L >> i) & 1) {
        G[i].push_back(E(N - 1, K));
        K += 1 << i;
      }
    }
    int M = 0;
    for (int i = 0; i < N; i++)
      M += G[i].size();
    cout << N << " " << M << endl;
    for (int i = 0; i < N; i++) {
      for (auto e : G[i]) {
        int j = e.fst;
        int w = e.snd;
        cout << (i + 1) << " " << (j + 1) << " " << w << endl;
      }
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
/* clang-format on */

using E = pair<int, int>; // to, weight

int main() {
  int L;
  while (cin >> L) {
    int N = 0;
    while ((1 << N) <= L)
      ++N;
    vec<E, 2> G(N);
    for (int i = 0; i + 1 < N; i++) {
      G[i].push_back(E(i + 1, 0));
      G[i].push_back(E(i + 1, 1 << i));
    }
    int K = 1 << (N - 1);
    for (int i = N - 2; i >= 0; i--) {
      if ((L >> i) & 1) {
        G[i].push_back(E(N - 1, K));
        K += 1 << i;
      }
    }
    int M = 0;
    for (int i = 0; i < N; i++)
      M += G[i].size();
    cout << N << " " << M << endl;
    for (int i = 0; i < N; i++) {
      for (auto e : G[i]) {
        int j = e.fst;
        int w = e.snd;
        cout << (i + 1) << " " << (j + 1) << " " << w << endl;
      }
    }
  }
  return 0;
}
Problem p03269 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int l;
  cin >> l;
  vector<int> v;
  for (int i = 0; i <= 18; i++) {
    if (l & (1 << i)) {
      v.push_back(i);
    }
  }
  reverse(v.begin(), v.end());
  int p = v.size();
  vector<int> val(p + 3);
  val[0] = 1 << v[0];
  for (int i = 1; i < p; i++) {
    val[i] = val[i - 1] + (1 << v[i]);
  }
  vector<pair<pair<int, int>, int>> edgelist;
  for (int i = 2; i <= v[0] + 1; i++) {
    edgelist.push_back(make_pair(make_pair(i - 1, i), (1 << (i - 2))));
    edgelist.push_back(make_pair(make_pair(i - 1, i), 0));
  }
  for (int i = 1; i < p; i++) {
    edgelist.push_back(make_pair(make_pair(v[i] + 1, v[0] + 1), val[i - 1]));
  }
  cout << v[0] + 1 << ' ' << edgelist.size() << '\n';
  for (int i = 0; i < edgelist.size(); i++) {
    cout << edgelist[i].first.first << ' ' << edgelist[i].first.second << ' '
         << edgelist[i].second << '\n';
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int l;
  cin >> l;
  vector<int> v;
  for (int i = 0; i <= 22; i++) {
    if (l & (1 << i)) {
      v.push_back(i);
    }
  }
  reverse(v.begin(), v.end());
  int p = v.size();
  vector<int> val(p + 3);
  val[0] = 1 << v[0];
  for (int i = 1; i < p; i++) {
    val[i] = val[i - 1] + (1 << v[i]);
  }
  vector<pair<pair<int, int>, int>> edgelist;
  for (int i = 2; i <= v[0] + 1; i++) {
    edgelist.push_back(make_pair(make_pair(i - 1, i), (1 << (i - 2))));
    edgelist.push_back(make_pair(make_pair(i - 1, i), 0));
  }
  for (int i = 1; i < p; i++) {
    edgelist.push_back(make_pair(make_pair(v[i] + 1, v[0] + 1), val[i - 1]));
  }
  cout << v[0] + 1 << ' ' << edgelist.size() << '\n';
  for (int i = 0; i < edgelist.size(); i++) {
    cout << edgelist[i].first.first << ' ' << edgelist[i].first.second << ' '
         << edgelist[i].second << '\n';
  }
  return 0;
}
Problem p03269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

int main() {
  int l;
  cin >> l;
  // rは頂点数
  // 2^(頂点数-1)だけパスが作れるので、2^rがlを超えるような最小のrが頂点数となる
  int r = 1;
  for (int i = 1; i <= 20; i++) {
    if (pow(2, i) > l) {
      r = i;
      break;
    }
  }
  queue<vector<int>> x;
  int rm = l - 1;
  // もし辺iから辺rに長さxの辺を貼ったとすると、2^(i-1)個の新しいパスができる
  // このとき、そのパスの長さをrm,rm-1,...,rm-2^(i-1)+1とすると、rm-2^(i-1)+1が2^r以上であれば良い
  // そしてx=rm-2^(i-1)+1となる
  for (int i = r; i > 0; i++) {
    if (rm - pow(2, i - 1) + 1 >= pow(2, r - 1)) {
      x.push({i, r, rm - (int)pow(2, i - 1) + 1});
      rm -= pow(2, i - 1);
    }
  }
  cout << r << " " << (r - 1) * 2 + x.size() << endl;
  for (int i = 1; i < r; i++) {
    cout << i << " " << i + 1 << " " << 0 << endl;
    cout << i << " " << i + 1 << " " << (int)pow(2, i - 1) << endl;
  }
  vector<int> ans(3);
  int k = (int)x.size();
  for (int i = 0; i < k; i++) {
    ans = x.front();
    x.pop();
    cout << ans[0] << " " << ans[1] << " " << ans[2] << endl;
  }
}


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

int main() {
  int l;
  cin >> l;
  // rは頂点数
  // 2^(頂点数-1)だけパスが作れるので、2^rがlを超えるような最小のrが頂点数となる
  int r = 1;
  for (int i = 1; i <= 20; i++) {
    if (pow(2, i) > l) {
      r = i;
      break;
    }
  }
  queue<vector<int>> x;
  int rm = l - 1;
  // もし辺iから辺rに長さxの辺を貼ったとすると、2^(i-1)個の新しいパスができる
  // このとき、そのパスの長さをrm,rm-1,...,rm-2^(i-1)+1とすると、rm-2^(i-1)+1が2^r以上であれば良い
  // そしてx=rm-2^(i-1)+1となる
  for (int i = r; i > 0; i--) {
    if (rm - pow(2, i - 1) + 1 >= pow(2, r - 1)) {
      x.push({i, r, rm - (int)pow(2, i - 1) + 1});
      rm -= pow(2, i - 1);
    }
  }
  cout << r << " " << (r - 1) * 2 + x.size() << endl;
  for (int i = 1; i < r; i++) {
    cout << i << " " << i + 1 << " " << 0 << endl;
    cout << i << " " << i + 1 << " " << (int)pow(2, i - 1) << endl;
  }
  vector<int> ans(3);
  int k = (int)x.size();
  for (int i = 0; i < k; i++) {
    ans = x.front();
    x.pop();
    cout << ans[0] << " " << ans[1] << " " << ans[2] << endl;
  }
}
Problem p03269 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int L;
int from[66], to[66], cost[66];
int edgecnt;

int bitlen(int x) {
  int res = 0;
  while (x) {
    res++;
    x /= 2;
  }
  return res;
}

void e1(int N) {
  int k = 1;
  for (int i = N; i >= 3; --i) {
    from[edgecnt] = i - 1;
    to[edgecnt] = i;
    cost[edgecnt] = k;
    from[edgecnt + 1] = i - 1;
    to[edgecnt + 1] = i;
    cost[edgecnt + 1] = 0;
    k *= 2;
    edgecnt += 2;
  }
}

int leftbit(int x) {
  if (x == 0)
    return 0;
  int res = 1 << 30;
  while (res) {
    if (x & res)
      return res;
    res >>= 1;
  }
  return -1;
}

int e2(int x) {
  int res = 0;
  rep(res, 100) {
    if (x == (1 << res))
      return res;
  }
}

signed main() {
  cin >> L;
  int N = bitlen(L) + 1;
  e1(N);
  L ^= leftbit(L);
  int c = (1 << (N - 2));
  while (L) {
    int l = leftbit(L);
    L ^= l;
    from[edgecnt] = 1;
    to[edgecnt] = N - e2(l);
    cost[edgecnt] = c;
    c += l;
    edgecnt++;
  }
  if (N > 20 || edgecnt > 60)
    return 1;
  rep(i, edgecnt) {
    if (from[i] > 1)
      from[i]--;
    if (to[i] > 1)
      to[i]--;
  }
  cout << N - 1 << " " << edgecnt << endl;
  rep(i, edgecnt) { cout << from[i] << " " << to[i] << " " << cost[i] << endl; }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int L;
int from[66], to[66], cost[66];
int edgecnt;

int bitlen(int x) {
  int res = 0;
  while (x) {
    res++;
    x /= 2;
  }
  return res;
}

void e1(int N) {
  int k = 1;
  for (int i = N; i >= 3; --i) {
    from[edgecnt] = i - 1;
    to[edgecnt] = i;
    cost[edgecnt] = k;
    from[edgecnt + 1] = i - 1;
    to[edgecnt + 1] = i;
    cost[edgecnt + 1] = 0;
    k *= 2;
    edgecnt += 2;
  }
}

int leftbit(int x) {
  if (x == 0)
    return 0;
  int res = 1 << 30;
  while (res) {
    if (x & res)
      return res;
    res >>= 1;
  }
  return -1;
}

int e2(int x) {
  int res = 0;
  rep(res, 100) {
    if (x == (1 << res))
      return res;
  }
}

signed main() {
  cin >> L;
  int N = bitlen(L) + 1;
  e1(N);
  L ^= leftbit(L);
  int c = (1 << (N - 2));
  while (L) {
    int l = leftbit(L);
    L ^= l;
    from[edgecnt] = 1;
    to[edgecnt] = N - e2(l);
    cost[edgecnt] = c;
    c += l;
    edgecnt++;
  }
  rep(i, edgecnt) {
    if (from[i] > 1)
      from[i]--;
    if (to[i] > 1)
      to[i]--;
  }
  cout << N - 1 << " " << edgecnt << endl;
  rep(i, edgecnt) { cout << from[i] << " " << to[i] << " " << cost[i] << endl; }
  return 0;
}
Problem p03269 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 100100
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

int L, n = 2, m = 0, l = 0;

vector<pii> g[MAXN];

void mult() {
  for (int i = 1; i <= 20; i++) {
    for (auto &j : g[i]) {
      j.second *= 2;
    }
  }
  g[n].pb({n + 1, 0});
  g[n].pb({n + 1, 1});
  m += 2;
  l *= 2;
  n++;
}

void add() {
  g[1].pb({n, l++});
  m++;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> L;
  vector<bool> v;
  while (L > 0) {
    v.pb(L % 2);
    L /= 2;
  }
  reverse(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (i != 0 and l != 1)
      mult();
    if (i != 0 and l == 1)
      add();
    if (v[i])
      add();
  }
  cout << n << " " << m << endl;
  for (int i = 1; i <= 20; i++) {
    for (auto &j : g[i]) {
      cout << i << " " << j.first << " " << j.second << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXN 100100
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

int L, n = 2, m = 0, l = 0;

vector<pii> g[MAXN];

void mult() {
  for (int i = 1; i <= 20; i++) {
    for (auto &j : g[i]) {
      j.second *= 2;
    }
  }
  g[n].pb({n + 1, 0});
  g[n].pb({n + 1, 1});
  m += 2;
  l *= 2;
  n++;
}

void add() {
  g[1].pb({n, l++});
  m++;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> L;
  vector<bool> v;
  while (L > 0) {
    v.pb(L % 2);
    L /= 2;
  }
  reverse(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (i != 0 and l != 1)
      mult();
    if (i != 0 and l == 1)
      add();
    if (v[i])
      add();
  }
  cout << n << " " << m << endl;
  for (int i = 1; i <= 20; i++) {
    for (auto &j : g[i]) {
      cout << i << " " << j.first << " " << j.second << endl;
    }
  }
}
Problem p03269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
using namespace std;
typedef long long ll;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
  char str[20];
  sprintf(str, "in%s.txt", s);
  freopen(str, "r", stdin);
  //	sprintf(str,"out%s.txt",s);
  //	freopen(str,"w",stdout);
#endif
}
inline int rd() {
  static int x, f;
  x = 0;
  f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return f > 0 ? x : -x;
}
int bin[21], L;

int main() {
  open("d");
  bin[0] = 1;
  fo(i, 1, 20) bin[i] = bin[i - 1] << 1;
  L = rd();
  int t = upper_bound(bin, bin + 21, L) - bin - 1;
  int n, m, d;
  n = t + 1;
  m = t << 1;
  int p = L - bin[t];
  fo(i, 0, t) if (bin[i] & p) m++;
  cout << n << ' ' << m << endl;
  fo(i, 1, n - 1) {
    cout << i << ' ' << i + 1 << ' ' << bin[i - 1] << endl;
    cout << i << ' ' << i + 1 << " 0\n";
  }
  int K = L - bin[t];
  d = L;
  p = 0;
  fo(i, 0, t - 1) {
    if (bin[i] & K) {
      cout << i + 1 << ' ' << n << ' ' << (d -= bin[i]) << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
using namespace std;
typedef long long ll;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
  char str[20];
  sprintf(str, "in%s.txt", s);
  freopen(str, "r", stdin);
  //	sprintf(str,"out%s.txt",s);
  //	freopen(str,"w",stdout);
#endif
}
inline int rd() {
  static int x, f;
  x = 0;
  f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return f > 0 ? x : -x;
}
int bin[21], L;

int main() {
  bin[0] = 1;
  fo(i, 1, 20) bin[i] = bin[i - 1] << 1;
  L = rd();
  int t = upper_bound(bin, bin + 21, L) - bin - 1;
  int n, m, d;
  n = t + 1;
  m = t << 1;
  int p = L - bin[t];
  fo(i, 0, t) if (bin[i] & p) m++;
  cout << n << ' ' << m << endl;
  fo(i, 1, n - 1) {
    cout << i << ' ' << i + 1 << ' ' << bin[i - 1] << endl;
    cout << i << ' ' << i + 1 << " 0\n";
  }
  int K = L - bin[t];
  d = L;
  p = 0;
  fo(i, 0, t - 1) {
    if (bin[i] & K) {
      cout << i + 1 << ' ' << n << ' ' << (d -= bin[i]) << endl;
    }
  }
  return 0;
}
Problem p03270 (C++)

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

#define int long long

#define pb push_back
#define mp make_pair
#define st first
#define nd second
typedef pair<int, int> pp;
const int mod = 998244353;
const int N = 2e3 + 3;

int C[N][N], M[N][N][3];

int f(int i, int k, int h) {
  if (i == 0)
    return k == 0;
  int &r = M[i][k][h];
  if (r != -1)
    return r;
  r = f(i - 1, k, 0);
  if (k == 0)
    return r;

  if (h == 0)
    r = (r + f(i, k - 1, 1) + f(i, k - 1, 2)) % mod;
  if (h == 1)
    r = (r + f(i, k - 1, 1)) % mod;
  if (h == 2)
    r = (r + f(i, k - 1, 2)) % mod;
  return r;
}

int g(int a, int b) {
  if (a < 0 || b < 0)
    return 0;
  if (a == 0)
    return 1;
  if (b == 0)
    return 0;
  return C[a + b - 1][b - 1];
}

signed main() {
  int n, k;
  cin >> k >> n;
  for (int i = 0; i < N; i++)
    for (int j = C[i][0] = 1; j <= i; j++)
      C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;

  memset(M, -1, sizeof M);
  for (int i = 2; i <= k + k; i++) {
    int ans = 0;
    for (int j = 0; j <= n; j++) {
      if (i & 1) {
        int mn = min(i / 2, k - i / 2);
        ans = (ans + f(mn, j, 0) * g(n - j, k - 2 * mn)) % mod;
      } else {
        int mn = min(i / 2 - 1, k - i / 2);
        ans = (ans + f(mn, j, 0) * g(n - j, k - 2 * mn - 1)) % mod;
        if (j < n)
          ans = (ans + f(mn, j, 0) * g(n - j - 1, k - 2 * mn - 1)) % mod;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

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

#define int long long

#define pb push_back
#define mp make_pair
#define st first
#define nd second
typedef pair<int, int> pp;
const int mod = 998244353;
const int N = 4e3 + 3;

int C[N][N], M[N][N][3];

int f(int i, int k, int h) {
  if (i == 0)
    return k == 0;
  int &r = M[i][k][h];
  if (r != -1)
    return r;
  r = f(i - 1, k, 0);
  if (k == 0)
    return r;

  if (h == 0)
    r = (r + f(i, k - 1, 1) + f(i, k - 1, 2)) % mod;
  if (h == 1)
    r = (r + f(i, k - 1, 1)) % mod;
  if (h == 2)
    r = (r + f(i, k - 1, 2)) % mod;
  return r;
}

int g(int a, int b) {
  if (a < 0 || b < 0)
    return 0;
  if (a == 0)
    return 1;
  if (b == 0)
    return 0;
  return C[a + b - 1][b - 1];
}

signed main() {
  int n, k;
  cin >> k >> n;
  for (int i = 0; i < N; i++)
    for (int j = C[i][0] = 1; j <= i; j++)
      C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;

  memset(M, -1, sizeof M);
  for (int i = 2; i <= k + k; i++) {
    int ans = 0;
    for (int j = 0; j <= n; j++) {
      if (i & 1) {
        int mn = min(i / 2, k - i / 2);
        ans = (ans + f(mn, j, 0) * g(n - j, k - 2 * mn)) % mod;
      } else {
        int mn = min(i / 2 - 1, k - i / 2);
        ans = (ans + f(mn, j, 0) * g(n - j, k - 2 * mn - 1)) % mod;
        if (j < n)
          ans = (ans + f(mn, j, 0) * g(n - j - 1, k - 2 * mn - 1)) % mod;
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p03270 (Python)

Buggy submission (status: Runtime Error):
def prepare(k, n, MOD):
    def get_factorials(m):
        f = 1
        factorials = [1]
        for m in range(1, m + 1):
            f *= m
            f %= MOD
            factorials.append(f)
        inv = pow(f, MOD - 2, MOD)
        invs = [1] * (m + 1)
        invs[m] = inv
        for m in range(m, 1, -1):
            inv *= m
            inv %= MOD
            invs[m - 1] = inv

        return factorials, invs

    def solve(p):
        """Number of patterns where no pair of p appears when n dices are rolled"""
        if cache[p] > -1:
            return cache[p]

        ret = 0
        fp = factorials[p]
        for q in range(1, min(p, n // 2) + 1):
            tmp1 = (fp * invs[q] % MOD) * invs[p - q] % MOD
            tmp2 = (factorials[k + n - 2 * q - 1] * invs[n - 2 * q] % MOD) * ik % MOD
            if q % 2 == 1:
                ret += tmp1 * tmp2
            else:
                ret -= tmp1 * tmp2
            ret %= MOD

        cache[p] = ret = (all_patterns - ret) % MOD
        return ret

    factorials, invs = get_factorials(k + n)
    ik = invs[k - 1]
    all_patterns = factorials[k + n - 1] * invs[n] * ik % MOD
    cache = [-1] * (k // 2 + 1)

    return solve


MOD = 998244353
k, n = map(int, input().split())
if k == 1:
    print(0)
else:
    solve = prepare(k, n, MOD)
    ans = [solve(i // 2 - max(0, i - k - 1)) for i in range(2, k + 2)]
    print("\n".join(map(str, ans)))
    print("\n".join(map(str, ans[-2::-1])))


Fixed submission:
def prepare(k, n, MOD):
    def get_factorials(m):
        f = 1
        factorials = [1]
        for m in range(1, m + 1):
            f *= m
            f %= MOD
            factorials.append(f)
        inv = pow(f, MOD - 2, MOD)
        invs = [1] * (m + 1)
        invs[m] = inv
        for m in range(m, 1, -1):
            inv *= m
            inv %= MOD
            invs[m - 1] = inv

        return factorials, invs

    def solve(p):
        """Number of patterns where no pair of p appears when n dices are rolled"""
        if cache[p] > -1:
            return cache[p]

        ret = 0
        fp = factorials[p]
        for q in range(1, min(p, n // 2) + 1):
            tmp1 = (fp * invs[q] % MOD) * invs[p - q] % MOD
            tmp2 = (factorials[k + n - 2 * q - 1] * invs[n - 2 * q] % MOD) * ik % MOD
            if q % 2 == 1:
                ret += tmp1 * tmp2
            else:
                ret -= tmp1 * tmp2
            ret %= MOD

        cache[p] = ret = (all_patterns - ret) % MOD
        return ret

    factorials, invs = get_factorials(k + n)
    ik = invs[k - 1]
    all_patterns = factorials[k + n - 1] * invs[n] * ik % MOD
    cache = [-1] * (k // 2 + 2)

    return solve


MOD = 998244353
k, n = map(int, input().split())
if k == 1:
    print(0)
else:
    solve = prepare(k, n, MOD)
    ans = [solve(i // 2 - max(0, i - k - 1)) for i in range(2, k + 2)]
    print("\n".join(map(str, ans)))
    print("\n".join(map(str, ans[-2::-1])))
Problem p03270 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
constexpr ll MOD = 998244353;

template <typename T> T mypow(T a, T b) {
  if (b == 0)
    return 1;
  T tmp = mypow(a, b / 2);
  if (b % 2)
    return (((tmp * tmp) % MOD) * a) % MOD;
  else
    return (tmp * tmp) % MOD;
}

// v[i][j] := iCj
std::vector<std::vector<ll>> comb2(int n) {
  std::vector<std::vector<ll>> v(n + 1, std::vector<ll>(n + 1, 0));
  for (int i = 0; i < (int)v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (int j = 1; j < (int)v.size(); j++) {
    for (int k = 1; k < j; k++) {
      v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]);
      v[j][k] %= MOD;
    }
  }
  return v;
}

auto comb = comb2(4040);

// nHk = n+k-1 C k
ll H(int n, int k) {
  assert(k >= 0);
  return comb[n + k - 1][k];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int K, N;
  cin >> K >> N;
  for (int i = 2; i <= 2 * K; i++) {
    ll ret = 0;
    int p = 0;
    for (int j = 1; 2 * j <= i; j++) {
      if (K < i - j)
        continue;
      p++;
    }
    for (int q = 1; q <= p; q++) {
      ll sum = comb[p][q];
      sum = sum * H(K, N - 2 * q) % MOD;
      if (q % 2) {
        ret += sum;
        ret %= MOD;
      } else {
        ret = ret + MOD - sum;
        ret %= MOD;
      }
    }
    cout << (H(K, N) + MOD - ret) % MOD << endl;
  }
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
constexpr ll MOD = 998244353;

template <typename T> T mypow(T a, T b) {
  if (b == 0)
    return 1;
  T tmp = mypow(a, b / 2);
  if (b % 2)
    return (((tmp * tmp) % MOD) * a) % MOD;
  else
    return (tmp * tmp) % MOD;
}

// v[i][j] := iCj
std::vector<std::vector<ll>> comb2(int n) {
  std::vector<std::vector<ll>> v(n + 1, std::vector<ll>(n + 1, 0));
  for (int i = 0; i < (int)v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (int j = 1; j < (int)v.size(); j++) {
    for (int k = 1; k < j; k++) {
      v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]);
      v[j][k] %= MOD;
    }
  }
  return v;
}

auto comb = comb2(4040);

// nHk = n+k-1 C k
ll H(int n, int k) {
  if (k < 0)
    return 0;
  return comb[n + k - 1][k];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int K, N;
  cin >> K >> N;
  for (int i = 2; i <= 2 * K; i++) {
    ll ret = 0;
    int p = 0;
    for (int j = 1; 2 * j <= i; j++) {
      if (K < i - j)
        continue;
      p++;
    }
    for (int q = 1; q <= p; q++) {
      ll sum = comb[p][q];
      sum = sum * H(K, N - 2 * q) % MOD;
      if (q % 2) {
        ret += sum;
        ret %= MOD;
      } else {
        ret = ret + MOD - sum;
        ret %= MOD;
      }
    }
    cout << (H(K, N) + MOD - ret) % MOD << endl;
  }
}
Problem p03270 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define mod 998244353
#define mad(a, b) a = (a + b) % mod;
#define mul(a, b) a = a * b % mod;
#define N 100010
ll fac[N], inv[N], ivf[N];
ll C(ll n, ll k) {
  if (n < k)
    return 0;
  return fac[n] * ivf[n - k] % mod * ivf[k] % mod;
}
ll H(ll n, ll k) {
  // if(k==0)return 1;
  return C(n + k - 1, k);
}
ll P(ll n, ll k) { return fac[n] * ivf[n - k] % mod; }
ll po(ll x, ll y) {
  ll res = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
  }
  return res;
}
ll solve(ll a, ll b, ll n) {
  ll res = 0;
  for (ll p = 0; p <= b; p++) {
    ll cur = C(b, p) * po(2, p) % mod;
    mul(cur, H(a + p, n - p));
    mad(res, cur);
  }
  // cout<<"#"<<a<<" "<<b<<" "<<n<<" "<<res<<endl;
  return res;
}
int main() {
  fac[0] = ivf[0] = inv[1] = 1;
  for (int i = 1; i < N; i++) {
    if (i > 1)
      inv[i] = (mod - mod / i * inv[mod % i] % mod) % mod;
    fac[i] = fac[i - 1] * i % mod;
    ivf[i] = ivf[i - 1] * inv[i] % mod;
  }
  ll k, n;
  cin >> k >> n;
  for (ll i = 2; i <= 2 * k; i++) {
    ll a, b;
    a = abs(k - i + 1);
    b = (k - a) / 2;
    ll ans = solve(a, b, n);
    if (i % 2 == 0) {
      mad(ans, solve(a, b, n - 1));
    }
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define mod 998244353
#define mad(a, b) a = (a + b) % mod;
#define mul(a, b) a = a * b % mod;
#define N 100010
ll fac[N], inv[N], ivf[N];
ll C(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * ivf[n - k] % mod * ivf[k] % mod;
}
ll H(ll n, ll k) {
  // if(k==0)return 1;
  return C(n + k - 1, k);
}
ll P(ll n, ll k) { return fac[n] * ivf[n - k] % mod; }
ll po(ll x, ll y) {
  ll res = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
  }
  return res;
}
ll solve(ll a, ll b, ll n) {
  ll res = 0;
  for (ll p = 0; p <= b; p++) {
    ll cur = C(b, p) * po(2, p) % mod;
    mul(cur, H(a + p, n - p));
    mad(res, cur);
  }
  // cout<<"#"<<a<<" "<<b<<" "<<n<<" "<<res<<endl;
  return res;
}
int main() {
  fac[0] = ivf[0] = inv[1] = 1;
  for (int i = 1; i < N; i++) {
    if (i > 1)
      inv[i] = (mod - mod / i * inv[mod % i] % mod) % mod;
    fac[i] = fac[i - 1] * i % mod;
    ivf[i] = ivf[i - 1] * inv[i] % mod;
  }
  ll k, n;
  cin >> k >> n;
  for (ll i = 2; i <= 2 * k; i++) {
    ll a, b;
    a = abs(k - i + 1);
    b = (k - a) / 2;
    ll ans = solve(a, b, n);
    if (i % 2 == 0) {
      mad(ans, solve(a, b, n - 1));
    }
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
  }
  return 0;
}
Problem p03270 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define all(x) x.begin(), x.end()
const int maxn = 2010;
const int mod = 998244353;

int fac[maxn];
map<int, int> mp;

int expo(int x, int exp) {
  int res = 1, base = x;
  while (exp) {
    if (exp & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    exp >>= 1;
  }
  return res;
}

int inv(int x) {
  if (mp.find(x) != mp.end())
    return mp[x];
  else
    return mp[x] = expo(x, mod - 2);
}

int choose(int N, int k) {
  return fac[N] * inv(fac[k] * fac[N - k] % mod) % mod;
}

int K, n;

int32_t main() {
  // FAST
  cin >> K >> n;
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = fac[i - 1] * i % mod;

  for (int i = 2; i <= 2 * K; i++) {
    int ans = 0;
    int pairs = i / 2 - max(1ll, i - K) + 1;

    for (int x = 0; x <= pairs; x++) {
      if (n < x * 2)
        break;
      int v = choose(n - x * 2 + K - 1, n - x * 2) * choose(pairs, x) % mod;
      if (x % 2 == 0)
        ans = (ans + v) % mod;
      else
        ans = (ans - v + mod) % mod;
      // cout << i << " " << x << " " << v << " " << ans << "\n";
    }
    cout << ans << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define all(x) x.begin(), x.end()
const int maxn = 20100;
const int mod = 998244353;

int fac[maxn];
map<int, int> mp;

int expo(int x, int exp) {
  int res = 1, base = x;
  while (exp) {
    if (exp & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    exp >>= 1;
  }
  return res;
}

int inv(int x) {
  if (mp.find(x) != mp.end())
    return mp[x];
  else
    return mp[x] = expo(x, mod - 2);
}

int choose(int N, int k) {
  return fac[N] * inv(fac[k] * fac[N - k] % mod) % mod;
}

int K, n;

int32_t main() {
  // FAST
  cin >> K >> n;
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = fac[i - 1] * i % mod;

  for (int i = 2; i <= 2 * K; i++) {
    int ans = 0;
    int pairs = i / 2 - max(1ll, i - K) + 1;

    for (int x = 0; x <= pairs; x++) {
      if (n < x * 2)
        break;
      int v = choose(n - x * 2 + K - 1, n - x * 2) * choose(pairs, x) % mod;
      if (x % 2 == 0)
        ans = (ans + v) % mod;
      else
        ans = (ans - v + mod) % mod;
      // cout << i << " " << x << " " << v << " " << ans << "\n";
    }
    cout << ans << "\n";
  }
}
Problem p03270 (C++)

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

using namespace std;

const int P = 998244353;
int fac[4005], ifac[4005];

int power(int a, int x) {
  int ans = 1;
  for (; x; x >>= 1, a = 1LL * a * a % P)
    if (x & 1)
      ans = 1LL * ans * a % P;
  return ans;
}
int calc(int N, int M) {
  return !M ? !N : 1LL * fac[N + M - 1] * ifac[M - 1] % P * ifac[N] % P;
}

int main() {
  int K, N;
  scanf("%d%d", &K, &N);
  for (int i = fac[0] = 1; i <= N + K - 2; ++i)
    fac[i] = 1LL * fac[i - 1] * i % P;
  ifac[N + K - 2] = power(fac[N + K - 2], P - 2);
  for (int i = N + K - 2; i; --i)
    ifac[i - 1] = 1LL * ifac[i] * i % P;
  vector<int> v(N + 1), res(K + 1);
  v[0] = 1;
  for (int S = 2; S <= 2 * K; ++S) {
    if (S <= K + 1 && S % 2) {
      for (int i = 1; i <= N; ++i)
        v[i] = (v[i] + v[i - 1]) % P;
      for (int i = N; i; --i)
        v[i] = (v[i] + v[i - 1]) % P;
    }
    int ans = 0;
    if (S <= K + 1) {
      for (int i = 0; i <= N; ++i) {
        int sum = ((S % 2 || !i ? 0 : v[i - 1]) + v[i]) % P;
        ans = (ans + 1LL * sum * calc(N - i, K + 1 - S)) % P;
      }
      res[S] = ans;
    } else
      ans = res[2 * K + 2 - S];
    printf("%d\n", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int P = 998244353;
int fac[4005], ifac[4005];

int power(int a, int x) {
  int ans = 1;
  for (; x; x >>= 1, a = 1LL * a * a % P)
    if (x & 1)
      ans = 1LL * ans * a % P;
  return ans;
}
int calc(int N, int M) {
  return !M ? !N : 1LL * fac[N + M - 1] * ifac[M - 1] % P * ifac[N] % P;
}

int main() {
  int K, N;
  scanf("%d%d", &K, &N);
  for (int i = fac[0] = 1; i <= N + K - 2; ++i)
    fac[i] = 1LL * fac[i - 1] * i % P;
  ifac[N + K - 2] = power(fac[N + K - 2], P - 2);
  for (int i = N + K - 2; i; --i)
    ifac[i - 1] = 1LL * ifac[i] * i % P;
  vector<int> v(N + 1), res(K + 2);
  v[0] = 1;
  for (int S = 2; S <= 2 * K; ++S) {
    if (S <= K + 1 && S % 2) {
      for (int i = 1; i <= N; ++i)
        v[i] = (v[i] + v[i - 1]) % P;
      for (int i = N; i; --i)
        v[i] = (v[i] + v[i - 1]) % P;
    }
    int ans = 0;
    if (S <= K + 1) {
      for (int i = 0; i <= N; ++i) {
        int sum = ((S % 2 || !i ? 0 : v[i - 1]) + v[i]) % P;
        ans = (ans + 1LL * sum * calc(N - i, K + 1 - S)) % P;
      }
      res[S] = ans;
    } else
      ans = res[2 * K + 2 - S];
    printf("%d\n", ans);
  }
  return 0;
}