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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 200005;

int A[MAXN];
ll nxt[MAXN][64];
vector<int> p[MAXN];
bitset<MAXN> inv;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll N, K;
  cin >> N >> K;
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  for (int i = 0; i < N; ++i)
    p[A[i]].push_back(i);
  for (int i = 0; i < MAXN; ++i) {
    if (!p[i].empty()) {
      for (int j = 0; j < p[i].size() - 1; ++j)
        nxt[p[i][j]][0] = p[i][j + 1] + 1 - p[i][j];
      nxt[p[i].back()][0] = p[i].front() + N + 1 - p[i].back();
    }
  }

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 63; ++j) {
      if (nxt[i][j] <= N * K)
        nxt[i][j + 1] = nxt[i][j] + nxt[(i + nxt[i][j]) % N][j];
      else
        nxt[i][j + 1] = N * K + 1;
    }
  }

  ll x = 0;
  int cnt = 0;
  while (1) {
    int l = 0, h = 63;
    while (l < h) {
      int m = (l + h) >> 1;
      x + nxt[x % N][m] >= N *K ? h = m : l = m + 1;
    }
    if (h <= 0)
      break;
    x += nxt[x % N][h - 1];
  }

  vector<int> v;
  for (int i = x % N; i < N; ++i) {
    if (inv[A[i]]) {
      while (v.back() != A[i]) {
        inv[v.back()] = 0;
        v.pop_back();
      }
      inv[A[i]] = 0;
      v.pop_back();
    } else {
      inv[A[i]] = 1;
      v.push_back(A[i]);
    }
  }
  for (auto &i : v)
    cout << i << ' ';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 200005;

int A[MAXN];
ll nxt[MAXN][64];
vector<int> p[MAXN];
bitset<MAXN> inv;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll N, K;
  cin >> N >> K;
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  for (int i = 0; i < N; ++i)
    p[A[i]].push_back(i);
  for (int i = 0; i < MAXN; ++i) {
    if (!p[i].empty()) {
      for (int j = 0; j < p[i].size() - 1; ++j)
        nxt[p[i][j]][0] = p[i][j + 1] + 1 - p[i][j];
      nxt[p[i].back()][0] = p[i].front() + N + 1 - p[i].back();
    }
  }

  for (int j = 0; j < 63; ++j) {
    for (int i = 0; i < N; ++i) {
      if (nxt[i][j] <= N * K)
        nxt[i][j + 1] = nxt[i][j] + nxt[(i + nxt[i][j]) % N][j];
      else
        nxt[i][j + 1] = N * K + 1;
    }
  }

  ll x = 0;
  int cnt = 0;
  while (1) {
    int l = 0, h = 63;
    while (l < h) {
      int m = (l + h) >> 1;
      x + nxt[x % N][m] >= N *K ? h = m : l = m + 1;
    }
    if (h <= 0)
      break;
    x += nxt[x % N][h - 1];
  }

  vector<int> v;
  for (int i = x % N; i < N; ++i) {
    if (inv[A[i]]) {
      while (v.back() != A[i]) {
        inv[v.back()] = 0;
        v.pop_back();
      }
      inv[A[i]] = 0;
      v.pop_back();
    } else {
      inv[A[i]] = 1;
      v.push_back(A[i]);
    }
  }
  for (auto &i : v)
    cout << i << ' ';
}
Problem p02964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int tot, n, a[N << 1], b[N], nex[N], bk[N], pre[N];
ll k, A, B;
bool vis[N];
ll getdis(int s, int t) {
  if (s == 0)
    s = n;
  ll ans = 0;
  while (s != t) {
    ans += nex[s];
    s = (s + nex[s]) % n;
    if (s == 0)
      s = n;
  }
  return ans;
}
void getAns(int now) {
  for (int i = now; i <= n; i++) {
    int x = a[i];
    if (pre[x]) {
      while (b[tot] != x)
        pre[b[tot]]--, tot--;
      pre[x]--;
      tot--;
    } else
      b[++tot] = x, pre[x]++;
  }
}
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i + n] = a[i];
  for (int i = n << 1; i >= 1; i--) {
    if (i <= n)
      nex[i] = bk[a[i]] - i + 1;
    bk[a[i]] = i;
  }
  int now = 1;
  while (!vis[now]) {
    vis[now] = true;
    now = (now + nex[now]) % n;
    if (now == 0)
      now = n;
  }
  A = getdis(1, now);
  B = getdis(now + nex[now], now) + nex[now];
  k *= n;
  k--;
  if (k <= A) {
    int now = 1;
    while (k >= ((nex[now] - now) % n + n) % n)
      k -= ((nex[now] - now) % n + n) % n, now = nex[now];
    getAns(now);
  } else {
    k -= A;
    k %= B;
    while (k >= nex[now]) {
      k -= nex[now];
      now = (now + nex[now]) % n;
      if (now == 0)
        now = n;
    }
    getAns(now);
  }
  for (int i = 1; i <= tot; i++)
    printf(i == tot ? "%d\n" : "%d ", b[i]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int tot, n, a[N << 1], b[N], nex[N], bk[N], pre[N];
ll k, A, B;
bool vis[N];
ll getdis(int s, int t) {
  if (s == 0)
    s = n;
  ll ans = 0;
  while (s != t) {
    ans += nex[s];
    s = (s + nex[s]) % n;
    if (s == 0)
      s = n;
  }
  return ans;
}
void getAns(int now) {
  for (int i = now; i <= n; i++) {
    int x = a[i];
    if (pre[x]) {
      while (b[tot] != x)
        pre[b[tot]]--, tot--;
      pre[x]--;
      tot--;
    } else
      b[++tot] = x, pre[x]++;
  }
}
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i + n] = a[i];
  for (int i = n << 1; i >= 1; i--) {
    if (i <= n)
      nex[i] = bk[a[i]] - i + 1;
    bk[a[i]] = i;
  }
  int now = 1;
  while (!vis[now]) {
    vis[now] = true;
    now = (now + nex[now]) % n;
    if (now == 0)
      now = n;
  }
  A = getdis(1, now);
  B = getdis(now + nex[now], now) + nex[now];
  k *= n;
  k--;
  if (k <= A) {
    int now = 1;
    while (k >= nex[now]) {
      k -= nex[now];
      now = (now + nex[now]) % n;
      if (now == 0)
        now = n;
    }
    getAns(now);
  } else {
    k -= A;
    k %= B;
    while (k >= nex[now]) {
      k -= nex[now];
      now = (now + nex[now]) % n;
      if (now == 0)
        now = n;
    }
    getAns(now);
  }
  for (int i = 1; i <= tot; i++)
    printf(i == tot ? "%d\n" : "%d ", b[i]);
}
Problem p02964 (C++)

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

inline int read() {
  int ret = 0;
  char c = getchar();
  while (c < 48 || c > 57)
    c = getchar();
  while (c >= 48 && c <= 57)
    ret = ret * 10 + c - 48, c = getchar();
  return ret;
}
#define N 200093
long long k;
int T, f[N], g[N], u, v, st[N], t, n, a[N], h[N], tt, hh[N];
inline void pr(int u) {
  while (u <= n) {
    if (f[u] > u)
      u = f[u];
    else
      printf("%d ", a[u]);
    ++u;
  }
  exit(0);
}

int main() {
  n = read();
  cin >> k;
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 1; i <= n; ++i) {
    if (!h[a[i]])
      h[a[i]] = i;
  }
  for (int i = n; i; --i) {
    if (!g[a[i]])
      f[i] = h[a[i]];
    else
      f[i] = g[a[i]];
    g[a[i]] = i;
  }

  u = 1;

  while (1) {
    v = f[u] + 1;
    if (v <= u + 1)
      ++T;
    if (v == n + 1)
      break;
    u = v;
  }

  /*
  T=1;
  while(1){
          if(f[u]<=u)++T;
          v=f[u]+1;
          if(v==n+1)break;
          u=v;
  }*/

  u = 1;
  --k;
  k %= T;
  if (!k)
    pr(u);
  while (1) {
    v = f[u] + 1;
    if (v > n)
      v = 1;
    if (v <= u + 1)
      --k;
    if (!k)
      pr(v);
    u = v;
  }
}

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

inline int read() {
  int ret = 0;
  char c = getchar();
  while (c < 48 || c > 57)
    c = getchar();
  while (c >= 48 && c <= 57)
    ret = ret * 10 + c - 48, c = getchar();
  return ret;
}
#define N 200093
long long k;
int T, f[N], g[N], u, v, st[N], t, n, a[N], h[N], tt, hh[N];
inline void pr(int u) {
  while (u <= n) {
    if (f[u] > u)
      u = f[u];
    else
      printf("%d ", a[u]);
    ++u;
  }
  exit(0);
}

int main() {
  n = read();
  cin >> k;
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 1; i <= n; ++i) {
    if (!h[a[i]])
      h[a[i]] = i;
  }
  for (int i = n; i; --i) {
    if (!g[a[i]])
      f[i] = h[a[i]];
    else
      f[i] = g[a[i]];
    g[a[i]] = i;
  }

  u = 1;

  T = 1;
  while (1) {
    v = f[u] + 1;
    if (v <= u + 1)
      ++T;
    if (v == n + 1)
      break;
    u = v;
  }

  /*
  T=1;
  while(1){
          if(f[u]<=u)++T;
          v=f[u]+1;
          if(v==n+1)break;
          u=v;
  }*/

  u = 1;
  --k;
  k %= T;
  if (!k)
    pr(u);
  while (1) {
    v = f[u] + 1;
    if (v > n)
      v = 1;
    if (v <= u + 1)
      --k;
    if (!k)
      pr(v);
    u = v;
  }
}
Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define pii pair<int, int>
#define fi first
#define se second
#define PB push_back
#define ll long long
using namespace std;
const int N = 200005;
int a[N], las[N], q[N], n;
ll b[N][60], k;
int main() {
  scanf("%d%lld", &n, &k);
  For(i, 1, n) scanf("%d", &a[i]), a[i + n] = a[i];
  Rep(i, n + n, n + 1) las[a[i]] = i;
  Rep(i, n, 1) {
    b[i][0] = las[a[i]];
    las[a[i]] = i;
  }
  For(i, 1, 58) For(j, 1, n) if (b[j][i - 1] == (1ll << 60)) b[j][i] = 1ll
                                                                       << 60;
  else {
    ll S = b[j][i - 1] / n, id = b[j][i - 1] + 1 - S * n;
    b[j][i] = min(1ll << 60, b[id][i - 1] + S * n);
  }
  ll now = 0;
  Rep(i, 58, 0) {
    ll S = now / n, id = now + 1 - S * n;
    if (b[id][i] + S * n <= k * n)
      now = b[id][i] + S * n;
  }
  For(i, 1, N - 1) las[i] = 0;
  for (now++; now <= k * n; now++) {
    ll S = (now - 1) / n, id = now - S * n;
    if (!las[a[id]])
      q[++*q] = a[id], las[a[id]] = 1;
    else {
      for (; q[*q] != a[id]; --*q)
        las[q[*q]] = 0;
      --*q;
      las[a[id]] = 0;
    }
  }
  For(i, 1, *q) printf("%d ", q[i]);
  puts("");
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define pii pair<int, int>
#define fi first
#define se second
#define PB push_back
#define ll long long
using namespace std;
const int N = 400005;
int a[N], las[N], q[N], n;
ll b[N][60], k;
int main() {
  scanf("%d%lld", &n, &k);
  For(i, 1, n) scanf("%d", &a[i]), a[i + n] = a[i];
  Rep(i, n + n, n + 1) las[a[i]] = i;
  Rep(i, n, 1) {
    b[i][0] = las[a[i]];
    las[a[i]] = i;
  }
  For(i, 1, 58) For(j, 1, n) if (b[j][i - 1] == (1ll << 60)) b[j][i] = 1ll
                                                                       << 60;
  else {
    ll S = b[j][i - 1] / n, id = b[j][i - 1] + 1 - S * n;
    b[j][i] = min(1ll << 60, b[id][i - 1] + S * n);
  }
  ll now = 0;
  Rep(i, 58, 0) {
    ll S = now / n, id = now + 1 - S * n;
    if (b[id][i] + S * n <= k * n)
      now = b[id][i] + S * n;
  }
  For(i, 1, N - 1) las[i] = 0;
  for (now++; now <= k * n; now++) {
    ll S = (now - 1) / n, id = now - S * n;
    if (!las[a[id]])
      q[++*q] = a[id], las[a[id]] = 1;
    else {
      for (; q[*q] != a[id]; --*q)
        las[q[*q]] = 0;
      --*q;
      las[a[id]] = 0;
    }
  }
  For(i, 1, *q) printf("%d ", q[i]);
  puts("");
}
Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2001000;
int n, a[maxn], nxt[maxn], flag[maxn];
long long k, f[maxn][100];
vector<int> pos[maxn];
vector<int> ans;
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i), pos[a[i]].push_back(i);
  for (int i = 1; i <= 200000; i++)
    for (int j = 0; j < (int)pos[i].size(); j++) {
      if (j + 1 == (int)pos[i].size())
        nxt[pos[i][j]] = pos[i][0] + n - pos[i][j] + 1;
      else
        nxt[pos[i][j]] = pos[i][j + 1] - pos[i][j] + 1;
    }
  for (int i = 1; i <= n; i++)
    f[i][0] = nxt[i];
  for (int j = 1; j < 70; j++)
    for (int i = 1; i <= n; i++)
      f[i][j] =
          min(n * k + 1, f[i][j - 1] + f[(i + f[i][j - 1] - 1) % n + 1][j - 1]);
  long long now = 1;
  for (int i = 69; i >= 0; i--)
    if (f[(now - 1) % n + 1][i] + now <= n * k + 1)
      now += f[(now - 1) % n + 1][i];
  for (long long i = now; i <= n * k; i++) {
    int val = a[(i - 1) % n + 1];
    if (flag[val]) {
      while (ans[ans.size() - 1] != val)
        ans.pop_back();
      ans.pop_back();
      flag[val] = 0;
    } else {
      ans.push_back(val);
      flag[val] = 1;
    }
  }
  for (int i = 0; i < (int)ans.size(); i++)
    cout << ans[i] << ' ';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2001000;
int n, a[maxn], nxt[maxn], flag[maxn];
long long k, f[maxn][100];
vector<int> pos[maxn];
vector<int> ans;
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i), pos[a[i]].push_back(i);
  for (int i = 1; i <= 200000; i++)
    for (int j = 0; j < (int)pos[i].size(); j++) {
      if (j + 1 == (int)pos[i].size())
        nxt[pos[i][j]] = pos[i][0] + n - pos[i][j] + 1;
      else
        nxt[pos[i][j]] = pos[i][j + 1] - pos[i][j] + 1;
    }
  for (int i = 1; i <= n; i++)
    f[i][0] = nxt[i];
  for (int j = 1; j < 70; j++)
    for (int i = 1; i <= n; i++)
      f[i][j] =
          min(n * k + 1, f[i][j - 1] + f[(i + f[i][j - 1] - 1) % n + 1][j - 1]);
  long long now = 1;
  for (int i = 69; i >= 0; i--)
    if (f[(now - 1) % n + 1][i] + now <= n * k + 1)
      now += f[(now - 1) % n + 1][i];
  for (long long i = now; i <= n * k; i++) {
    int val = a[(i - 1) % n + 1];
    if (flag[val]) {
      while (ans[ans.size() - 1] != val)
        flag[ans[ans.size() - 1]] = 0, ans.pop_back();
      ans.pop_back();
      flag[val] = 0;
    } else {
      ans.push_back(val);
      flag[val] = 1;
    }
  }
  for (int i = 0; i < (int)ans.size(); i++)
    cout << ans[i] << ' ';
}
Problem p02964 (C++)

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

const int maxn = 2e5 + 10;

int n;
long long k;
int a[maxn << 1], nxt[maxn], b[maxn << 1];
bool inq[maxn];
long long vis[maxn];
vector<pair<long long, int>> v;
vector<int> u;

int main() {
  cin >> n >> k;
  k *= n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    a[i + n] = a[i];
  }

  memset(nxt, -1, sizeof(nxt));
  memset(b, -1, sizeof(b));
  for (int i = (n << 1) - 1; ~i; --i) {
    b[i] = nxt[a[i]] + 1;
    nxt[a[i]] = i;
  }
  //	for (int i = 0; i < n; ++i) printf("a[%d]=%d, b[i]=%d\n", i, a[i],
  //b[i]);

  memset(vis, -1, sizeof(vis));
  int i = 0;
  long long loop = 0;
  while (1) {
    if (~vis[i])
      break;
    vis[i] = loop;
    v.push_back(make_pair(vis[i], i));
    //		printf("%d(%d): %lld\n", i, a[i], loop);

    loop += b[i] - i;
    i = b[i] % n;
  }
  loop -= vis[i];
  //	printf("i=%d, loop=%d\n", i, loop);
  if (k >= vis[i])
    k = (k - vis[i]) % loop + vis[i];
  int pos = (--upper_bound(v.begin(), v.end(), make_pair(k, 0)))->second;
  //	printf("pos=%d\n", pos);
  for (int j = vis[pos] + 1, l = pos; j <= k; ++j) {
    if (inq[a[l]]) {
      while (inq[a[l]]) {
        inq[u.back()] = false;
        u.pop_back();
      }
    } else {
      u.push_back(a[l]);
      inq[a[l]] = true;
    }
    l = (l + 1) % n;
    //		for (int _ = 0; _ < u.size(); ++_) printf("%d ", u[_]);
    //puts("");
  }
  for (int j = 0; j < u.size(); ++j)
    printf("%d ", u[j]);
  puts("");
}


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

const int maxn = 2e5 + 10;

int n;
long long k;
int a[maxn << 1], nxt[maxn], b[maxn << 1];
bool inq[maxn];
long long vis[maxn];
vector<pair<long long, int>> v;
vector<int> u;

int main() {
  cin >> n >> k;
  k *= n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    a[i + n] = a[i];
  }

  memset(nxt, -1, sizeof(nxt));
  memset(b, -1, sizeof(b));
  for (int i = (n << 1) - 1; ~i; --i) {
    b[i] = nxt[a[i]] + 1;
    nxt[a[i]] = i;
  }
  //	for (int i = 0; i < n; ++i) printf("a[%d]=%d, b[i]=%d\n", i, a[i],
  //b[i]);

  memset(vis, -1, sizeof(vis));
  int i = 0;
  long long loop = 0;
  while (1) {
    if (~vis[i])
      break;
    vis[i] = loop;
    v.push_back(make_pair(vis[i], i));
    //		printf("%d(%d): %lld\n", i, a[i], loop);

    loop += b[i] - i;
    i = b[i] % n;
  }
  loop -= vis[i];
  //	printf("i=%d, loop=%d\n", i, loop);
  if (k >= vis[i])
    k = (k - vis[i]) % loop + vis[i];
  int pos = (--upper_bound(v.begin(), v.end(), make_pair(k, 0)))->second;
  //	printf("pos=%d\n", pos);
  for (long long j = vis[pos] + 1, l = pos; j <= k; ++j) {
    if (inq[a[l]]) {
      while (inq[a[l]]) {
        inq[u.back()] = false;
        u.pop_back();
      }
    } else {
      u.push_back(a[l]);
      inq[a[l]] = true;
    }
    l = (l + 1) % n;
    //		for (int _ = 0; _ < u.size(); ++_) printf("%d ", u[_]);
    //puts("");
  }
  for (int j = 0; j < u.size(); ++j)
    printf("%d ", u[j]);
  puts("");
}
Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 10;
int a[maxn], n, r[maxn], c[maxn], f[maxn], d[maxn];
ll k;
bool vis[maxn];
int ask() {
  int l = 1, cost = 0;
  while (1) {
    cost += c[l];
    l = r[l];
    if (l == n)
      return cost + 1;
    l++;
    if (l == n + 1)
      l = 1, cost++;
  }
  return cost + 1;
}
bool is[maxn];
void print(int i) {
  stack<int> P, Q;
  for (; i <= n; i++) {
    if (!is[a[i]]) {
      P.push(a[i]);
      is[a[i]] = 1;
    } else {
      while (1) {
        int v = P.top();
        is[a[v]] = 0;
        P.pop();
        if (v == a[i])
          break;
      }
    }
  }
  while (!P.empty()) {
    Q.push(P.top());
    P.pop();
  }
  while (!Q.empty()) {
    printf("%d ", Q.top());
    Q.pop();
  }
}
void work(int K) {
  int l = 1, cost = 0;
  while (1) {
    if (cost + c[l] >= k) {
      print(l);
      return;
    }
    cost += c[l];
    l = r[l];
    l++;
    if (l == n + 1) {
      if (cost + 1 >= k) {
        printf("\n");
        return;
      }
      l = 1, cost++;
    }
  }
}
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (vis[a[i]] == 0) {
      vis[a[i]] = 1;
      f[a[i]] = i;
    }
  }
  for (int i = n; i >= 1; i--) {
    if (d[a[i]]) {
      r[i] = d[a[i]];
    } else {
      r[i] = f[a[i]];
      c[i] = 1;
    }
    d[a[i]] = i;
  }
  int mod = ask();
  k %= mod;
  // if(k==0)
  // printf("\n");
  // else
  { work(k); }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 10;
int a[maxn], n, r[maxn], c[maxn], f[maxn], d[maxn];
ll k;
bool vis[maxn];
int ask() {
  int l = 1, cost = 0;
  while (1) {
    cost += c[l];
    l = r[l];
    if (l == n)
      return cost + 1;
    l++;
    if (l == n + 1)
      l = 1, cost++;
  }
  return cost + 1;
}
bool is[maxn];
void print(int i) {
  stack<int> P, Q;
  for (; i <= n; i++) {
    if (!is[a[i]]) {
      P.push(a[i]);
      is[a[i]] = 1;
    } else {
      while (1) {
        int v = P.top();
        is[v] = 0;
        P.pop();
        if (v == a[i])
          break;
      }
    }
  }
  while (!P.empty()) {
    Q.push(P.top());
    P.pop();
  }
  while (!Q.empty()) {
    printf("%d ", Q.top());
    Q.pop();
  }
}
void work(int K) {
  int l = 1, cost = 0;
  while (1) {
    if (cost + c[l] >= k) {
      print(l);
      return;
    }
    cost += c[l];
    l = r[l];
    l++;
    if (l == n + 1) {
      if (cost + 1 >= k) {
        printf("\n");
        return;
      }
      l = 1, cost++;
    }
  }
}
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (vis[a[i]] == 0) {
      vis[a[i]] = 1;
      f[a[i]] = i;
    }
  }
  for (int i = n; i >= 1; i--) {
    if (d[a[i]]) {
      r[i] = d[a[i]];
    } else {
      r[i] = f[a[i]];
      c[i] = 1;
    }
    d[a[i]] = i;
  }
  int mod = ask();
  k %= mod;
  // if(k==0)
  // printf("\n");
  // else
  { work(k); }
}
Problem p02964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
int A[400000];
ll d[61][200000];
int pos[200001];
int main() {
  ll N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i + N] = A[i];
  }
  for (int i = 2 * N - 1; i >= 0; i--) {
    if (i < N)
      d[0][i] = pos[A[i]] - i + 1;
    pos[A[i]] = i;
  }
  for (int i = 1; i <= 60; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = min(INF, d[i - 1][j] + d[i - 1][(j + d[i - 1][j]) % N]);
    }
  }
  ll now = 0;
  for (int i = 0; i <= 60; i++) {
    if (now + d[i][now % N] <= N * K)
      now += d[i][now % N];
  }
  deque<int> q;
  set<int> S;
  for (ll i = now; i < N * K; i++) {
    if (S.count(A[i % N])) {
      while (q.back() != A[i % N]) {
        S.erase(q.back());
        q.pop_back();
      }
      S.erase(A[i % N]);
      q.pop_back();
    } else {
      S.insert(A[i % N]);
      q.push_back(A[i % N]);
    }
  }
  for (int i = 0; i < q.size(); i++)
    cout << q[i] << " ";
  cout << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
int A[400000];
ll d[61][200000];
int pos[200001];
int main() {
  ll N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i + N] = A[i];
  }
  for (int i = 2 * N - 1; i >= 0; i--) {
    if (i < N)
      d[0][i] = pos[A[i]] - i + 1;
    pos[A[i]] = i;
  }
  for (int i = 1; i <= 60; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = min(INF, d[i - 1][j] + d[i - 1][(j + d[i - 1][j]) % N]);
    }
  }
  ll now = 0;
  for (int i = 60; i >= 0; i--) {
    if (now + d[i][now % N] <= N * K)
      now += d[i][now % N];
  }
  deque<int> q;
  set<int> S;
  for (ll i = now; i < N * K; i++) {
    if (S.count(A[i % N])) {
      while (q.back() != A[i % N]) {
        S.erase(q.back());
        q.pop_back();
      }
      S.erase(A[i % N]);
      q.pop_back();
    } else {
      S.insert(A[i % N]);
      q.push_back(A[i % N]);
    }
  }
  for (int i = 0; i < q.size(); i++)
    cout << q[i] << " ";
  cout << endl;
}
Problem p02964 (C++)

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

vector<int> v[200001];

long long int vis[200001];
int a[200001];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  long long int k;
  cin >> n >> k;
  for (int i = 0, x; i < n; i++) {
    cin >> x;
    a[i] = x;
    v[x].emplace_back(i);
  }
  memset(vis, -1, sizeof vis);
  int cx = 0, nx;
  long long int ck = 0, nk;
  while (true) {
    if (vis[cx] != -1) {
      ck = vis[cx] + (k - 1 - vis[cx]) / (ck - vis[cx]) * (ck - vis[cx]);
      break;
    }
    vis[cx] = ck;
    auto it = upper_bound(v[a[cx]].begin(), v[a[cx]].end(), cx);
    if (it == v[a[cx]].end()) {
      nx = v[a[cx]][0];
      nk = ck + 1;
    } else {
      nx = *it;
      nk = ck;
    }
    nx++;
    if (nx == n)
      nx = 0, nk++;
    if (nk >= k)
      break;
    cx = nx, ck = nk;
  }
  while (true) {
    auto it = upper_bound(v[a[cx]].begin(), v[a[cx]].end(), cx);
    if (it == v[a[cx]].end()) {
      nx = v[a[cx]][0];
      nk = ck + 1;
    } else {
      nx = *it;
      nk = ck;
    }
    nx++;
    if (nx == n)
      nx = 0, nk++;
    if (nk >= k)
      break;
    cx = nx, ck = nk;
  }
  vector<int> res;
  set<int> st;
  while (ck < k) {
    int x = a[cx];
    if (st.count(x)) {
      while (res.back() != x)
        res.pop_back();
      res.pop_back();
      st.erase(x);
    } else {
      st.insert(x);
      res.emplace_back(x);
    }
    cx++;
    if (cx == n)
      cx = 0, ck++;
  }
  for (int it : res)
    cout << it << ' ';
  cout << endl;
}


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

vector<int> v[200001];

long long int vis[200001];
int a[200001];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  long long int k;
  cin >> n >> k;
  for (int i = 0, x; i < n; i++) {
    cin >> x;
    a[i] = x;
    v[x].emplace_back(i);
  }
  memset(vis, -1, sizeof vis);
  int cx = 0, nx;
  long long int ck = 0, nk;
  while (true) {
    if (vis[cx] != -1) {
      ck = vis[cx] + (k - 1 - vis[cx]) / (ck - vis[cx]) * (ck - vis[cx]);
      break;
    }
    vis[cx] = ck;
    auto it = upper_bound(v[a[cx]].begin(), v[a[cx]].end(), cx);
    if (it == v[a[cx]].end()) {
      nx = v[a[cx]][0];
      nk = ck + 1;
    } else {
      nx = *it;
      nk = ck;
    }
    nx++;
    if (nx == n)
      nx = 0, nk++;
    if (nk >= k)
      break;
    cx = nx, ck = nk;
  }
  while (true) {
    auto it = upper_bound(v[a[cx]].begin(), v[a[cx]].end(), cx);
    if (it == v[a[cx]].end()) {
      nx = v[a[cx]][0];
      nk = ck + 1;
    } else {
      nx = *it;
      nk = ck;
    }
    nx++;
    if (nx == n)
      nx = 0, nk++;
    if (nk >= k)
      break;
    cx = nx, ck = nk;
  }
  vector<int> res;
  set<int> st;
  while (ck < k) {
    int x = a[cx];
    if (st.count(x)) {
      while (res.back() != x)
        st.erase(res.back()), res.pop_back();
      res.pop_back();
      st.erase(x);
    } else {
      st.insert(x);
      res.emplace_back(x);
    }
    cx++;
    if (cx == n)
      cx = 0, ck++;
  }
  for (int it : res)
    cout << it << ' ';
  cout << endl;
}
Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 10;
int n;
long long k;
int a[N], las[N], pos[N], b[N], len[N];
bool c[N];
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = n; i >= 1; i--)
    las[i] = pos[a[i]], pos[a[i]] = i;
  for (int i = 1; i <= n; i++)
    if (!las[i])
      las[i] = pos[a[i]], len[i] = 1;
  for (int i = 1; i <= n; i++)
    if (las[i] == n)
      len[i]++;
  for (int i = 1; i <= n; i++)
    las[i] = las[i] % n + 1;
  int now = 1, l = 0;
  k--;
  while (k > 0) {
    k -= (ll)len[now];
    now = las[now];
    if (c[now])
      break;
    c[now] = 1;
    b[++l] = now;
  }
  if (k < 0)
    return 0;
  if (k > 0) {
    int s = 0;
    for (int i = 1; i <= l; i++)
      if (b[i] == now) {
        for (int j = i; j <= l; j++)
          s += len[j];
        break;
      }
    k = k % (ll)s;
    while (k > 0) {
      k -= (ll)len[now];
      now = las[now];
    }
    if (k < 0)
      return 0;
  }
  memset(pos, 0, sizeof(pos));
  l = 0;
  for (int i = now; i <= n; i++)
    if (!pos[a[i]]) {
      pos[a[i]] = i;
      b[++l] = a[i];
    } else {
      while (b[l] != a[i])
        pos[b[l]] = 0, l--;
      pos[a[i]] = 0;
      l--;
    }
  for (int i = 1; i <= l; i++)
    printf("%d ", b[i]);
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 10;
int n;
long long k;
int a[N], las[N], pos[N], b[N], len[N];
bool c[N];
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = n; i >= 1; i--)
    las[i] = pos[a[i]], pos[a[i]] = i;
  for (int i = 1; i <= n; i++)
    if (!las[i])
      las[i] = pos[a[i]], len[i] = 1;
  for (int i = 1; i <= n; i++)
    if (las[i] == n)
      len[i]++;
  for (int i = 1; i <= n; i++)
    las[i] = las[i] % n + 1;
  int now = 1, l = 0;
  k--;
  while (k > 0) {
    k -= (ll)len[now];
    now = las[now];
    if (c[now])
      break;
    c[now] = 1;
    b[++l] = now;
  }
  if (k < 0)
    return 0;
  if (k > 0) {
    int s = 0;
    for (int i = 1; i <= l; i++)
      if (b[i] == now) {
        for (int j = i; j <= l; j++)
          s += len[b[j]];
        break;
      }
    k = k % (ll)s;
    while (k > 0) {
      k -= (ll)len[now];
      now = las[now];
    }
    if (k < 0)
      return 0;
  }
  memset(pos, 0, sizeof(pos));
  l = 0;
  for (int i = now; i <= n; i++)
    if (!pos[a[i]]) {
      pos[a[i]] = i;
      b[++l] = a[i];
    } else {
      while (b[l] != a[i])
        pos[b[l]] = 0, l--;
      pos[a[i]] = 0;
      l--;
    }
  for (int i = 1; i <= l; i++)
    printf("%d ", b[i]);
}
Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

#define MAX_A 200000
#define MAX 50

signed main() {
  ll n, k;
  cin >> n >> k;
  int a[n];
  rep(i, n) { cin >> a[i]; }
  vector<vector<ll>> dp(MAX + 1, vector<ll>(n));
  int nxt[MAX_A + 1];
  for (int i = 2 * n - 1; i >= 0; i--) {
    dp[0][i % n] = nxt[a[i % n]] - i + 1;
    nxt[a[i % n]] = i;
  }
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < n; j++) {
      dp[i + 1][j] = dp[i][j] + dp[i][(dp[i][j] + j) % n];
    }
  }
  int now = 0;
  ll sum = n * k;
  while (sum > 0) {
    for (int i = MAX; i >= 0; i--) {
      if (dp[i][now] < sum) {
        sum -= dp[i][now];
        now = (now + dp[i][now]) % n;
        goto ok;
      }
    }
    break;
  ok:;
  }
  bool exist[MAX_A + 1] = {};
  vector<int> ans;
  for (int i = now; i < n; i++) {
    if (exist[a[i]]) {
      while (exist[a[i]]) {
        exist[ans.back()] = false;
        ans.pop_back();
      }
    } else {
      ans.emplace_back(a[i]);
      exist[a[i]] = true;
    }
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

#define MAX_A 200000
#define MAX 40

signed main() {
  ll n, k;
  cin >> n >> k;
  int a[n];
  rep(i, n) { cin >> a[i]; }
  vector<vector<ll>> dp(MAX + 1, vector<ll>(n));
  int nxt[MAX_A + 1];
  for (int i = 2 * n - 1; i >= 0; i--) {
    dp[0][i % n] = nxt[a[i % n]] - i + 1;
    nxt[a[i % n]] = i;
  }
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < n; j++) {
      dp[i + 1][j] = dp[i][j] + dp[i][(dp[i][j] + j) % n];
    }
  }
  int now = 0;
  ll sum = n * k;
  while (sum > 0) {
    for (int i = MAX; i >= 0; i--) {
      if (dp[i][now] < sum) {
        sum -= dp[i][now];
        now = (now + dp[i][now]) % n;
        goto ok;
      }
    }
    break;
  ok:;
  }
  bool exist[MAX_A + 1] = {};
  vector<int> ans;
  for (int i = now; i < n; i++) {
    if (exist[a[i]]) {
      while (exist[a[i]]) {
        exist[ans.back()] = false;
        ans.pop_back();
      }
    } else {
      ans.emplace_back(a[i]);
      exist[a[i]] = true;
    }
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}
Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <iomanip>
#include <iostream>
// #include<cstdio>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int Nmax = 2e5;
int N, A[Nmax];
ll K;

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<int> next(N, -1), pos(N + 1, -1);
  for (int i = 0; i < N; i++) {
    if (pos[A[i]] >= 0) {
      next[pos[A[i]]] = i;
    }
    pos[A[i]] = i;
  }
  for (int i = 0; i < N; i++) {
    if (next[pos[A[i]]] < 0) {
      next[pos[A[i]]] = i;
    }
  }

  int interval = 1;
  int idx = 0;
  while (idx != N) {
    if (next[idx] <= idx)
      interval++;
    idx = next[idx] + 1;
  }

  K %= interval;

  // cout << K << endl;

  idx = 0;
  while (K > 1) {
    if (next[idx] <= idx)
      K--;
    idx = next[idx] + 1;
    // cout << idx << " " << K << endl;
  }

  vector<int> ans;
  while (idx < N) {
    if (next[idx] > idx)
      idx = next[idx] + 1;
    else {
      ans.push_back(A[idx]);
      idx++;
    }
  }

  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << (i == ans.size() - 1 ? '\n' : ' ');

  return 0;
}


Fixed submission:
#include <iomanip>
#include <iostream>
// #include<cstdio>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int Nmax = 2e5;
int N, A[Nmax];
ll K;

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<int> next(N, -1), pos(Nmax + 1, -1);
  for (int i = 0; i < N; i++) {
    if (pos[A[i]] >= 0) {
      next[pos[A[i]]] = i;
    }
    pos[A[i]] = i;
  }
  for (int i = 0; i < N; i++) {
    if (next[pos[A[i]]] < 0) {
      next[pos[A[i]]] = i;
    }
  }

  int interval = 1;
  int idx = 0;
  while (idx != N) {
    if (next[idx] <= idx)
      interval++;
    idx = next[idx] + 1;
  }

  K %= interval;

  // cout << K << endl;

  idx = 0;
  while (K > 1) {
    if (next[idx] <= idx)
      K--;
    idx = next[idx] + 1;
    // cout << idx << " " << K << endl;
  }

  vector<int> ans;
  while (idx < N) {
    if (next[idx] > idx)
      idx = next[idx] + 1;
    else {
      ans.push_back(A[idx]);
      idx++;
    }
  }

  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << (i == ans.size() - 1 ? '\n' : ' ');

  return 0;
}
Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

long long N, K, A[1 << 18], P[1 << 18], nex[1 << 18];
long long par[1 << 18][66];
bool used[1 << 18];

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[N + i] = A[i];
  }
  for (int i = N * 2 - 1; i >= 0; i--) {
    nex[i] = P[A[i]] + 1;
    P[A[i]] = i;
  }
  for (int i = 0; i < N; i++)
    par[i][0] = (nex[i] - i);
  for (int i = 1; i < 66; i++) {
    for (int j = 0; j < N; j++) {
      long long v1 = par[j][i - 1];
      long long pos = (j + v1) % N;
      long long v2 = par[pos][i - 1];
      long long v3 = v1 + v2;
      v3 = min(v3, (1LL << 61));
      par[j][i] = v3;
    }
  }

  long long cx = 0, px = N * K;
  for (int i = 65; i >= 0; i--) {
    if (px >= par[cx][i]) {
      px -= par[cx][i];
      cx = (cx + par[cx][i]) % N;
    }
  }

  vector<int> vec;
  for (long long i = N * K - px; i < N * K; i++) {
    if (used[A[i % N]] == false) {
      vec.push_back(A[i % N]);
      used[A[i % N]] = true;
    } else {
      while (used[A[i % N]] == true) {
        used[vec[vec.size() - 1]] = false;
        vec.pop_back();
      }
    }
  }

  for (int i = 0; i < vec.size(); i++) {
    if (i)
      printf(" ");
    printf("%d", vec[i]);
  }
  printf("\n");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

long long N, K, A[1 << 19], P[1 << 19], nex[1 << 19];
long long par[1 << 19][66];
bool used[1 << 19];

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[N + i] = A[i];
  }
  for (int i = N * 2 - 1; i >= 0; i--) {
    nex[i] = P[A[i]] + 1;
    P[A[i]] = i;
  }
  for (int i = 0; i < N; i++)
    par[i][0] = (nex[i] - i);
  for (int i = 1; i < 66; i++) {
    for (int j = 0; j < N; j++) {
      long long v1 = par[j][i - 1];
      long long pos = (j + v1) % N;
      long long v2 = par[pos][i - 1];
      long long v3 = v1 + v2;
      v3 = min(v3, (1LL << 61));
      par[j][i] = v3;
    }
  }

  long long cx = 0, px = N * K;
  for (int i = 65; i >= 0; i--) {
    if (px >= par[cx][i]) {
      px -= par[cx][i];
      cx = (cx + par[cx][i]) % N;
    }
  }

  vector<int> vec;
  for (long long i = N * K - px; i < N * K; i++) {
    if (used[A[i % N]] == false) {
      vec.push_back(A[i % N]);
      used[A[i % N]] = true;
    } else {
      while (used[A[i % N]] == true) {
        used[vec[vec.size() - 1]] = false;
        vec.pop_back();
      }
    }
  }

  for (int i = 0; i < vec.size(); i++) {
    if (i)
      printf(" ");
    printf("%d", vec[i]);
  }
  printf("\n");
  return 0;
}
Problem p02965 (C++)

Buggy submission (status: Runtime Error):
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long ll;
const int N = 2e6;
const ll MOD = 998244353;

ll Efac[N + 3], Einv[N + 3];

ll QPow(ll Va, ll Vb) {
  ll Rr = 1;
  while (Vb) {
    if (Vb & 1)
      Rr = Va * Rr % MOD;
    Va = Va * Va % MOD;
    Vb >>= 1;
  }
  return Rr;
}
void Process() {
  Efac[0] = 1;
  for (int i = 1; i <= N; i++)
    Efac[i] = Efac[i - 1] * i % MOD;
  Einv[N] = QPow(Efac[N], MOD - 2);
  for (int i = N - 1; i >= 0; i--)
    Einv[i] = Einv[i + 1] * (i + 1) % MOD;
}
ll fC(int Va, int Vb) {
  return Efac[Vb] * Einv[Va] % MOD * Einv[Vb - Va] % MOD;
}
int main() {
  Process();
  int n, m;
  scanf("%d%d", &n, &m);
  ll Vans = fC(n - 1, 3 * m + n - 1);
  Vans = (Vans - fC(n - 1, m + n - 2) * n % MOD + MOD) % MOD;
  for (int i = m + 1; i <= n && i <= 3 * m; i++)
    if ((3 * m - i) % 2 == 0) {
      Vans =
          (Vans - fC(i, n) * fC(n - 1, (3 * m - i) / 2 + n - 1) % MOD + MOD) %
          MOD;
    }
  printf("%lld\n", (Vans % MOD + MOD) % MOD);
  return 0;
}

Fixed submission:
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long ll;
const int N = 3e6;
const ll MOD = 998244353;

ll Efac[N + 3], Einv[N + 3];

ll QPow(ll Va, ll Vb) {
  ll Rr = 1;
  while (Vb) {
    if (Vb & 1)
      Rr = Va * Rr % MOD;
    Va = Va * Va % MOD;
    Vb >>= 1;
  }
  return Rr;
}
void Process() {
  Efac[0] = 1;
  for (int i = 1; i <= N; i++)
    Efac[i] = Efac[i - 1] * i % MOD;
  Einv[N] = QPow(Efac[N], MOD - 2);
  for (int i = N - 1; i >= 0; i--)
    Einv[i] = Einv[i + 1] * (i + 1) % MOD;
}
ll fC(int Va, int Vb) {
  return Efac[Vb] * Einv[Va] % MOD * Einv[Vb - Va] % MOD;
}
int main() {
  Process();
  int n, m;
  scanf("%d%d", &n, &m);
  ll Vans = fC(n - 1, 3 * m + n - 1);
  Vans = (Vans - fC(n - 1, m + n - 2) * n % MOD + MOD) % MOD;
  for (int i = m + 1; i <= n && i <= 3 * m; i++)
    if ((3 * m - i) % 2 == 0) {
      Vans =
          (Vans - fC(i, n) * fC(n - 1, (3 * m - i) / 2 + n - 1) % MOD + MOD) %
          MOD;
    }
  printf("%lld\n", (Vans % MOD + MOD) % MOD);
  return 0;
}
Problem p02965 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define llint long long
#define mod 998244353

using namespace std;

llint n, m;
const int FACT_MAX = 4000005;
llint fact[FACT_MAX], fact_inv[FACT_MAX];

llint modpow(llint a, llint n) {
  if (n == 0)
    return 1;
  if (n % 2) {
    return ((a % mod) * (modpow(a, n - 1) % mod)) % mod;
  } else {
    return modpow((a * a) % mod, n / 2) % mod;
  }
}

void make_fact() {
  llint val = 1;
  fact[0] = 1;
  for (int i = 1; i < FACT_MAX; i++) {
    val *= i;
    val %= mod;
    fact[i] = val;
  }
  fact_inv[FACT_MAX - 1] = modpow(fact[FACT_MAX - 1], mod - 2);
  for (int i = FACT_MAX - 2; i >= 0; i--) {
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod;
  }
}

llint comb(llint n, llint k) {
  llint ret = 1;
  ret *= fact[n];
  ret *= fact_inv[k], ret %= mod;
  ret *= fact_inv[n - k], ret %= mod;
  return ret;
}

int main(void) {
  cin >> n >> m;
  make_fact();

  llint ans = 0;
  for (int i = 0; i <= m; i++) {
    if (m % 2 != i % 2)
      continue;
    llint rem = (3 * m - i) / 2;
    llint tmp = comb(rem + n - 1, n - 1);
    if (rem - (m + 1) >= 0)
      tmp += mod - (n - i) * comb(rem - (m + 1) + n - 1, n - 1) % mod,
          tmp %= mod;
    if (rem - m >= 0)
      tmp += mod - i * comb(rem - m + n - 1, n - 1) % mod, tmp %= mod;
    ans += tmp * comb(n, i) % mod, ans %= mod;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#define llint long long
#define mod 998244353

using namespace std;

llint n, m;
const int FACT_MAX = 4000005;
llint fact[FACT_MAX], fact_inv[FACT_MAX];

llint modpow(llint a, llint n) {
  if (n == 0)
    return 1;
  if (n % 2) {
    return ((a % mod) * (modpow(a, n - 1) % mod)) % mod;
  } else {
    return modpow((a * a) % mod, n / 2) % mod;
  }
}

void make_fact() {
  llint val = 1;
  fact[0] = 1;
  for (int i = 1; i < FACT_MAX; i++) {
    val *= i;
    val %= mod;
    fact[i] = val;
  }
  fact_inv[FACT_MAX - 1] = modpow(fact[FACT_MAX - 1], mod - 2);
  for (int i = FACT_MAX - 2; i >= 0; i--) {
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod;
  }
}

llint comb(llint n, llint k) {
  llint ret = 1;
  ret *= fact[n];
  ret *= fact_inv[k], ret %= mod;
  ret *= fact_inv[n - k], ret %= mod;
  return ret;
}

int main(void) {
  cin >> n >> m;
  make_fact();

  llint ans = 0;
  for (int i = 0; i <= min(m, n); i++) {
    if (m % 2 != i % 2)
      continue;
    llint rem = (3 * m - i) / 2;
    llint tmp = comb(rem + n - 1, n - 1);
    if (rem - (m + 1) >= 0)
      tmp += mod - (n - i) * comb(rem - (m + 1) + n - 1, n - 1) % mod,
          tmp %= mod;
    if (rem - m >= 0)
      tmp += mod - i * comb(rem - m + n - 1, n - 1) % mod, tmp %= mod;
    ans += tmp * comb(n, i) % mod, ans %= mod;
  }
  cout << ans << endl;

  return 0;
}
Problem p02965 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = 998244353;

int N, M;
long long dat[2000009];

long long quickpow(long long b, long long e) {
  return e ? ((e & 1 ? b : 1) * quickpow((b * b) % MOD, e >> 1)) % MOD : 1;
}

long long combi(int n, int m) {
  long long retval = dat[n];
  retval = retval * quickpow(dat[n - m], MOD - 2);
  retval = retval % MOD;
  retval = retval * quickpow(dat[m], MOD - 2);
  retval = retval % MOD;
  return retval;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> M;
  dat[0] = 1;
  for (int i = 1; i <= 2000009; i++) {
    dat[i] = dat[i - 1] * i;
    dat[i] = dat[i] % MOD;
  }
  int lb = min(N, M);
  long long ans = 0, tmp;
  for (int i = 0; i <= lb; i++) {
    if (3 * M - i >= 0 && (3 * M - i) % 2 == 0) {
      tmp = combi((3 * M - i) / 2 + N - 1, N - 1);
      tmp = tmp * combi(N, i);
      tmp = tmp % MOD;
      ans += tmp;
      ans = ans % MOD;
    }
  }
  tmp = combi(M + N - 1, N - 1);
  tmp = tmp + MOD - combi(M + N - 2, N - 2);
  tmp = tmp * N;
  tmp = tmp % MOD;
  ans = ans + MOD - tmp;
  ans = ans % MOD;
  cout << ans << E;
  // system("pause");
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = 998244353;

int N, M;
long long dat[2000009];

long long quickpow(long long b, long long e) {
  return e ? ((e & 1 ? b : 1) * quickpow((b * b) % MOD, e >> 1)) % MOD : 1;
}

long long combi(int n, int m) {
  long long retval = dat[n];
  retval = retval * quickpow(dat[n - m], MOD - 2);
  retval = retval % MOD;
  retval = retval * quickpow(dat[m], MOD - 2);
  retval = retval % MOD;
  return retval;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> M;
  dat[0] = 1;
  for (int i = 1; i < 2000009; i++) {
    dat[i] = dat[i - 1] * i;
    dat[i] = dat[i] % MOD;
  }
  int lb = min(N, M);
  long long ans = 0, tmp;
  for (int i = 0; i <= lb; i++) {
    if (3 * M - i >= 0 && (3 * M - i) % 2 == 0) {
      tmp = combi((3 * M - i) / 2 + N - 1, N - 1);
      tmp = tmp * combi(N, i);
      tmp = tmp % MOD;
      ans += tmp;
      ans = ans % MOD;
    }
  }
  tmp = combi(M + N - 1, N - 1);
  tmp = tmp + MOD - combi(M + N - 2, N - 2);
  tmp = tmp * N;
  tmp = tmp % MOD;
  ans = ans + MOD - tmp;
  ans = ans % MOD;
  cout << ans << E;
  // system("pause");
  return 0;
}
Problem p02965 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<ll, pll> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-9;
const ll mod = 998244353;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

ll Pow(ll n, ll p) {
  ll r = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1)
      r = (r * n) % mod;
    n = (n * n) % mod;
  }
  return r;
}

const int M = 4000005;
ll F[M];

void Init() {
  F[0] = 1;
  for (int i = 1; i < M; i++)
    F[i] = F[i - 1] * i % mod;
}

ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; }

ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); }

ll nHk(ll n, ll k) { return nCk(n + k - 1, k); }

int n, m;

int main() {
  Init();
  cin >> n >> m;
  ll res = nHk(n, 3 * m);
  for (int i = m + 1; i <= n; i++)
    if ((3 * m - i) % 2 == 0)
      res = (res - nHk(n, (3 * m - i) / 2) * nCk(n, i) % mod + mod) % mod;
  for (int i = 2 * m + 1; i <= 3 * m; i++)
    res = (res - n * nHk(n - 1, 3 * m - i) % mod + mod) % mod;
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<ll, pll> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-9;
const ll mod = 998244353;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

ll Pow(ll n, ll p) {
  ll r = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1)
      r = (r * n) % mod;
    n = (n * n) % mod;
  }
  return r;
}

const int M = 4000005;
ll F[M];

void Init() {
  F[0] = 1;
  for (int i = 1; i < M; i++)
    F[i] = F[i - 1] * i % mod;
}

ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; }

ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); }

ll nHk(ll n, ll k) { return nCk(n + k - 1, k); }

int n, m;

int main() {
  Init();
  cin >> n >> m;
  ll res = nHk(n, 3 * m);
  for (int i = m + 1; i <= min(3 * m, n); i++)
    if ((3 * m - i) % 2 == 0)
      res = (res - nHk(n, (3 * m - i) / 2) * nCk(n, i) % mod + mod) % mod;
  for (int i = 2 * m + 1; i <= 3 * m; i++)
    res = (res - n * nHk(n - 1, 3 * m - i) % mod + mod) % mod;
  cout << res << endl;
}