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

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

const int M = 924844033, N = 2010;
long long dp[N][N][4];
long long ways[N][N];
long long dp2[N];
long long fac[N];

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

  dp[0][0][0] = 1;
  for (int i = 1; i + 5 < N; i++) {
    for (int p = 0; p <= i + 1; p++) {
      dp[i][p][0] = (dp[i - 1][p][0] + dp[i - 1][p][1] + dp[i - 1][p][2] +
                     dp[i - 1][p][3]) %
                    M;
      dp[i][p + 1][1] = (dp[i - 1][p][0] + dp[i - 1][p][1]) % M;
      dp[i][p + 1][2] = (dp[i - 1][p][0] + dp[i - 1][p][2]) % M;
      dp[i][p + 2][3] = dp[i - 1][p][0];
    }
  }

  for (int i = 0; i + 5 < N; i++) {
    for (int j = 0; j <= i + 2; j++) {
      ways[i][j] = (dp[i][j][0] + dp[i][j][1] + dp[i][j][2] + dp[i][j][3]) % M;
    }
  }

  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = (fac[i - 1] * i) % M;

  int n, k;
  cin >> n >> k;

  dp2[0] = 1;
  for (int i = 0; i < k; i++) {
    int cnt = n / k + (i < n % k);
    for (int cur = n; cur >= 0; cur--) {
      long long vv = 0;
      for (int j = 0; j <= cur; j++)
        vv = (vv + dp2[cur - j] * ways[cnt - 1][j]) % M;
      dp2[cur] = vv;
    }
  }

  long long ans = 0;

  for (int i = 0; i <= n; i++) {
    if (i % 2 == 0)
      ans = (ans + dp2[i] * fac[n - i]) % M;
    else
      ans = (ans - dp2[i] * fac[n - i]) % M;
  }
  ans = (ans % M + M) % M;
  cout << ans << endl;
}


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

const int M = 924844033, N = 2010;
long long dp[N][N][4];
long long ways[N][N];
long long dp2[N];
long long fac[N];

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

  dp[0][0][0] = 1;
  for (int i = 1; i + 5 < N; i++) {
    for (int p = 0; p <= i + 1; p++) {
      dp[i][p][0] = (dp[i - 1][p][0] + dp[i - 1][p][1] + dp[i - 1][p][2] +
                     dp[i - 1][p][3]) %
                    M;
      dp[i][p + 1][1] = (dp[i - 1][p][0] + dp[i - 1][p][1]) % M;
      dp[i][p + 1][2] = (dp[i - 1][p][0] + dp[i - 1][p][2]) % M;
      dp[i][p + 2][3] = dp[i - 1][p][0];
    }
  }

  for (int i = 0; i + 5 < N; i++) {
    for (int j = 0; j <= i + 2; j++) {
      ways[i][j] = (dp[i][j][0] + dp[i][j][1] + dp[i][j][2] + dp[i][j][3]) % M;
    }
  }

  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = (fac[i - 1] * i) % M;

  int n, k;
  cin >> n >> k;

  dp2[0] = 1;
  for (int i = 0; i < k; i++) {
    int cnt = n / k + (i < n % k);
    for (int cur = n; cur >= 0; cur--) {
      long long vv = 0;
      for (int j = 0; j <= cur && j <= cnt; j++)
        vv = (vv + dp2[cur - j] * ways[cnt - 1][j]) % M;
      dp2[cur] = vv;
    }
  }

  long long ans = 0;

  for (int i = 0; i <= n; i++) {
    if (i % 2 == 0)
      ans = (ans + dp2[i] * fac[n - i]) % M;
    else
      ans = (ans - dp2[i] * fac[n - i]) % M;
  }
  ans = (ans % M + M) % M;
  cout << ans << endl;
}
Problem p03989 (C++)

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

using namespace std;

#define rep(i, l, r) for (int i = (l), ___ = (r); i <= ___; ++i)
#define per(i, r, l) for (int i = (r), ___ = (l); i >= ___; --i)

typedef long long LL;

template <typename Type> inline bool chkmin(Type &x, const Type &y) {
  return x > y ? (x = y, 1) : 0;
}
template <typename Type> inline bool chkmax(Type &x, const Type &y) {
  return x < y ? (x = y, 1) : 0;
}

template <typename Type> inline Type read(Type &f) {
  f = 0;
  int x = 1;
  char c = getchar();
  while (!isdigit(c))
    x = (c == '-' ? -1 : 1), c = getchar();
  while (isdigit(c))
    (f *= 10) += c & 15, c = getchar();
  return f = x * f;
}

const int N = 2000 + 5, KCZ = 924844033;

int n, k, S, vis[N];

LL Ans, f[N][N], fac[N];

void add(LL &x, const LL &y) {
  x += y;
  if (x >= KCZ)
    x -= KCZ;
}
void sub(LL &x, const LL &y) {
  x -= y;
  if (x < 0)
    x += KCZ;
}

void init() {
  fac[0] = 1;
  rep(i, 1, 2000) fac[i] = fac[i - 1] * i % KCZ;
}

int main() {
  read(n), read(k), init();
  rep(i, 1, k) {
    S += (n - i) / k + 1, vis[S] = 1;
    S += (n - i) / k + 1, vis[S] = 1;
  }
  rep(i, 0, S) f[i][0] = 1;
  rep(i, 2, S) rep(j, 1, n) {
    add(f[i][j], f[i - 1][j]);
    if (!vis[i - 1])
      add(f[i][j], f[i - 2][j - 1]);
  }
  rep(i, 0, n) {
    LL v = f[S][i] * fac[n - i] % KCZ;
    i & 1 ? sub(Ans, v) : add(Ans, v);
  }
  cout << Ans << endl;
  return 0;
}


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

using namespace std;

#define rep(i, l, r) for (int i = (l), ___ = (r); i <= ___; ++i)
#define per(i, r, l) for (int i = (r), ___ = (l); i >= ___; --i)

typedef long long LL;

template <typename Type> inline bool chkmin(Type &x, const Type &y) {
  return x > y ? (x = y, 1) : 0;
}
template <typename Type> inline bool chkmax(Type &x, const Type &y) {
  return x < y ? (x = y, 1) : 0;
}

template <typename Type> inline Type read(Type &f) {
  f = 0;
  int x = 1;
  char c = getchar();
  while (!isdigit(c))
    x = (c == '-' ? -1 : 1), c = getchar();
  while (isdigit(c))
    (f *= 10) += c & 15, c = getchar();
  return f = x * f;
}

const int N = 4000 + 5, KCZ = 924844033;

int n, k, S, vis[N];

LL Ans, f[N][N], fac[N];

void add(LL &x, const LL &y) {
  x += y;
  if (x >= KCZ)
    x -= KCZ;
}
void sub(LL &x, const LL &y) {
  x -= y;
  if (x < 0)
    x += KCZ;
}

void init() {
  fac[0] = 1;
  rep(i, 1, 2000) fac[i] = fac[i - 1] * i % KCZ;
}

int main() {
  read(n), read(k), init();
  rep(i, 1, k) {
    S += (n - i) / k + 1, vis[S] = 1;
    S += (n - i) / k + 1, vis[S] = 1;
  }
  rep(i, 0, S) f[i][0] = 1;
  rep(i, 2, S) rep(j, 1, n) {
    add(f[i][j], f[i - 1][j]);
    if (!vis[i - 1])
      add(f[i][j], f[i - 2][j - 1]);
  }
  rep(i, 0, n) {
    LL v = f[S][i] * fac[n - i] % KCZ;
    i & 1 ? sub(Ans, v) : add(Ans, v);
  }
  cout << Ans << endl;
  return 0;
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

typedef long long ll;

const ll INF = 1e9, MOD = 924844033;

ll n, k, cnt;
string s;

int d[4000][4000][2], a[4000], f[4000];
int main() {
  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    for (int j = i; j < n; j += k)
      a[cnt++] = (j != i);

    for (int j = i; j < n; j += k)
      a[cnt++] = (j != i);
  }

  d[0][0][0] = 1;
  d[0][0][1] = 1;

  for (ll i = 1; i < cnt; i++)
    for (ll j = 0; j <= cnt; j++) {
      d[i][j][0] = d[i - 1][j][0];
      if (a[i] && j)
        d[i][j][0] += d[i - 1][j - 1][1];
      if (d[i][j][0] >= MOD)
        d[i][j][0] -= MOD;

      d[i][j][1] = d[i - 1][j][0];
    }

  f[0] = 1;

  for (ll i = 1; i <= n; i++)
    f[i] = (i * f[i - 1]) % MOD;

  ll ans = 0;

  for (ll i = 0; i <= n; i++) {
    ll t = f[n - i] * d[cnt - 1][i][0] % MOD;
    if (i & 1)
      t = -t;
    ans += MOD + t;
    ans %= MOD;
  }

  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

typedef long long ll;

const ll INF = 1e9, MOD = 924844033;

ll n, k, cnt;
string s;

ll d[4000][4001][2], a[4000], f[4000];
int main() {
  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    for (int j = i; j < n; j += k)
      a[cnt++] = (j != i);

    for (int j = i; j < n; j += k)
      a[cnt++] = (j != i);
  }

  d[0][0][0] = 1;
  d[0][0][1] = 1;

  for (ll i = 1; i < cnt; i++)
    for (ll j = 0; j <= cnt; j++) {
      d[i][j][0] = d[i - 1][j][0];
      if (a[i] && j)
        d[i][j][0] += d[i - 1][j - 1][1];
      if (d[i][j][0] >= MOD)
        d[i][j][0] -= MOD;

      d[i][j][1] = d[i - 1][j][0];
    }

  f[0] = 1;

  for (ll i = 1; i <= n; i++)
    f[i] = (i * f[i - 1]) % MOD;

  ll ans = 0;

  for (ll i = 0; i <= n; i++) {
    ll t = f[n - i] * d[cnt - 1][i][0] % MOD;
    if (i & 1)
      t = -t;
    ans += MOD + t;
    ans %= MOD;
  }

  cout << ans;
}
Problem p03989 (C++)

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

typedef long long s64;

const int MaxN = 2000;
const int MaxNK = MaxN - 1;

const int M = 924844033;

inline int add(int a, const int &b) {
  a += b;
  return a >= M ? a - M : a;
}

int n, nK;

int f[MaxN + 1][MaxN / 2 + 1][4];
int g[MaxN / 2 + 1];
int h[MaxN / 2 + 1];

int main() {
  cin >> n >> nK;

  f[1][0][0] = 1;
  f[1][1][2] = 1;
  for (int i = 2; i <= n; ++i)
    for (int j = 0; j < i; ++j)
      for (int s = 0; s < 4; ++s) {
        int v = f[i - 1][j][s];
        if (v == 0)
          continue;

        int p = s >> 1, q = p | 2;
        f[i][j + 0][p] = add(f[i][j + 0][p], v);
        f[i][j + 1][q] = add(f[i][j + 1][q], v);
        if (~s & 1)
          f[i][j + 1][p] = add(f[i][j + 1][p], v);
      }

  g[0] = 1;
  for (int r = 1; r <= nK; ++r) {
    int m = n / nK + (n % nK >= r);

    for (int i = 0; i <= n; ++i)
      h[i] = 0;
    for (int i = 0; i <= n; ++i)
      for (int j = 0; j <= m; ++j) {
        int k = i + j;
        int d = add(f[m][j][0], f[m][j][1]);

        if (k <= n)
          h[k] = add(h[k], (s64)d * g[i] % M);
      }

    for (int i = 0; i <= n; ++i)
      g[i] = h[i];
  }

  int res = 0;
  int fac = 1, coe = n & 1 ? M - 1 : 1;
  for (int i = 1; i <= n + 1; ++i) {
    int v = g[n - i + 1];
    int c = (s64)fac * coe % M;
    res = add(res, (s64)v * c % M);

    coe = M - coe;
    fac = (s64)fac * i % M;
  }

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

typedef long long s64;

const int MaxN = 2000;
const int MaxNK = MaxN - 1;

const int M = 924844033;

inline int add(int a, const int &b) {
  a += b;
  return a >= M ? a - M : a;
}

int n, nK;

int f[MaxN + 1][MaxN + 1][4];
int g[MaxN + 1];
int h[MaxN + 1];

int main() {
  cin >> n >> nK;

  f[1][0][0] = 1;
  f[1][1][2] = 1;
  for (int i = 2; i <= n; ++i)
    for (int j = 0; j < i; ++j)
      for (int s = 0; s < 4; ++s) {
        int v = f[i - 1][j][s];
        if (v == 0)
          continue;

        int p = s >> 1, q = p | 2;
        f[i][j + 0][p] = add(f[i][j + 0][p], v);
        f[i][j + 1][q] = add(f[i][j + 1][q], v);
        if (~s & 1)
          f[i][j + 1][p] = add(f[i][j + 1][p], v);
      }

  g[0] = 1;
  for (int r = 1; r <= nK; ++r) {
    int m = n / nK + (n % nK >= r);

    for (int i = 0; i <= n; ++i)
      h[i] = 0;
    for (int i = 0; i <= n; ++i)
      for (int j = 0; j <= m; ++j) {
        int k = i + j;
        int d = add(f[m][j][0], f[m][j][1]);

        if (k <= n)
          h[k] = add(h[k], (s64)d * g[i] % M);
      }

    for (int i = 0; i <= n; ++i)
      g[i] = h[i];
  }

  int res = 0;
  int fac = 1, coe = n & 1 ? M - 1 : 1;
  for (int i = 1; i <= n + 1; ++i) {
    int v = g[n - i + 1];
    int c = (s64)fac * coe % M;
    res = add(res, (s64)v * c % M);

    coe = M - coe;
    fac = (s64)fac * i % M;
  }

  cout << res << endl;

  return 0;
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
#define int long long
const int N = 2010, mod = 924844033;
int n, k, g[N], f[N << 1][N][2], cnt, fac[N], ans;

signed main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= k; i++) {
    for (int j = i; j <= n; j += k)
      g[++cnt] = (j == i);
    for (int j = i; j <= n; j += k)
      g[++cnt] = (j == i);
  }
  f[0][0][0] = fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1LL * i * fac[i - 1] % mod;
  for (int i = 0; i < n * 2; i++)
    for (int j = 0; j <= min(i, n); j++) {
      f[i + 1][j][0] = (f[i][j][0] + f[i][j][1]) % mod;
      if (!g[i + 1])
        f[i + 1][j + 1][1] = f[i][j][0];
    }
  for (int i = 0; i <= n; i++) {
    int tmp = 1LL * (f[n << 1][i][0] + f[n << 1][i][1]) * fac[n - i] % mod;
    ans = (mod + (ans + (i & 1 ? -1 : 1) * tmp) % mod) % mod;
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
#define int long long
const int N = 2010, mod = 924844033;
int n, k, g[N << 1], f[N << 1][N][2], cnt, fac[N], ans;

signed main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= k; i++) {
    for (int j = i; j <= n; j += k)
      g[++cnt] = (j == i);
    for (int j = i; j <= n; j += k)
      g[++cnt] = (j == i);
  }
  f[0][0][0] = fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1LL * i * fac[i - 1] % mod;
  for (int i = 0; i < n * 2; i++)
    for (int j = 0; j <= min(i, n); j++) {
      f[i + 1][j][0] = (f[i][j][0] + f[i][j][1]) % mod;
      if (!g[i + 1])
        f[i + 1][j + 1][1] = f[i][j][0];
    }
  for (int i = 0; i <= n; i++) {
    int tmp = 1LL * (f[n << 1][i][0] + f[n << 1][i][1]) * fac[n - i] % mod;
    ans = (mod + (ans + (i & 1 ? -1 : 1) * tmp) % mod) % mod;
  }
  printf("%lld\n", ans);
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#define int long long
using namespace std;
const int MAXN = 2010;
const int MOD = 924844033;
int n, k, a[MAXN], cnt, vis[MAXN];
long long fac[MAXN], f[MAXN][MAXN][2][2];
signed main() {
  //	freopen("test.in","r",stdin);
  //	freopen("test.out","w",stdout);
  scanf("%lld%lld", &n, &k);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % MOD;
  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    for (int j = i; j <= n; j += k)
      a[++cnt] = j;
  }
  f[0][0][0][0] = 1;
  a[0] = -MAXN;
  for (int i = 1; i <= n; i++) {
    f[i][0][0][0] = 1;
    for (int j = 1; j <= i; j++) {
      f[i][j][0][0] = (f[i - 1][j][0][0] + f[i - 1][j][1][0] +
                       (a[i] - a[i - 1] == k) * f[i - 1][j - 1][0][0]) %
                      MOD;
      f[i][j][0][1] = (a[i + 1] - a[i] == k) *
                      (f[i - 1][j - 1][0][0] + f[i - 1][j - 1][1][0]) % MOD;
      f[i][j][1][0] = (f[i - 1][j][0][1] + f[i - 1][j][1][1] +
                       (a[i] - a[i - 1] == k) * f[i - 1][j - 1][0][1]) %
                      MOD;
      f[i][j][1][1] = (a[i + 1] - a[i] == k) *
                      (f[i - 1][j - 1][0][1] + f[i - 1][j - 1][1][1]) % MOD;
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    int sum =
        (f[n][i][0][0] + f[n][i][0][1] + f[n][i][1][0] + f[n][i][1][1]) % MOD;
    if (i & 1)
      (ans += MOD - sum * fac[n - i] % MOD) %= MOD;
    else
      (ans += sum * fac[n - i] % MOD) %= MOD;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#define int long long
using namespace std;
const int MAXN = 2010;
const int MOD = 924844033;
int n, k, a[MAXN], cnt, vis[MAXN];
long long fac[MAXN], f[MAXN][MAXN][2][2];
signed main() {
  //	freopen("test.in","r",stdin);
  //	freopen("test.out","w",stdout);
  scanf("%lld%lld", &n, &k);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % MOD;
  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    for (int j = i; j <= n; j += k)
      a[++cnt] = j, vis[j] = 1;
  }
  f[0][0][0][0] = 1;
  a[0] = -MAXN;
  for (int i = 1; i <= n; i++) {
    f[i][0][0][0] = 1;
    for (int j = 1; j <= i; j++) {
      f[i][j][0][0] = (f[i - 1][j][0][0] + f[i - 1][j][1][0] +
                       (a[i] - a[i - 1] == k) * f[i - 1][j - 1][0][0]) %
                      MOD;
      f[i][j][0][1] = (a[i + 1] - a[i] == k) *
                      (f[i - 1][j - 1][0][0] + f[i - 1][j - 1][1][0]) % MOD;
      f[i][j][1][0] = (f[i - 1][j][0][1] + f[i - 1][j][1][1] +
                       (a[i] - a[i - 1] == k) * f[i - 1][j - 1][0][1]) %
                      MOD;
      f[i][j][1][1] = (a[i + 1] - a[i] == k) *
                      (f[i - 1][j - 1][0][1] + f[i - 1][j - 1][1][1]) % MOD;
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    int sum =
        (f[n][i][0][0] + f[n][i][0][1] + f[n][i][1][0] + f[n][i][1][1]) % MOD;
    if (i & 1)
      (ans += MOD - sum * fac[n - i] % MOD) %= MOD;
    else
      (ans += sum * fac[n - i] % MOD) %= MOD;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 2020
#define mod 924844033
#define ll long long
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0')
    ch == '-' && (f = 0) || (ch = getchar());
  while (ch <= '9' && ch >= '0')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return f ? x : -x;
}
bool is_end[N];
int dp[N][N][2], a[N];
ll frac[N];
int main(int argc, char const *argv[]) {
  // freopen("../../temp/.in", "r", stdin);
  int n = read(), k = read();
  int top = 1;
  for (int i = frac[0] = 1; i <= n; ++i) {
    frac[i] = frac[i - 1] * i % mod;
  }
  for (int i = 1; i <= k; ++i) {
    int len = (n - i) / k + 1;
    is_end[top] = 1;
    top += len;
    is_end[top] = 1;
    top += len;
  }
  dp[0][0][0] = 1;
  for (int i = 1; i <= 2 * n; ++i) {
    for (int j = 0; j <= i / 2; ++j) {
      dp[i][j][0] = (dp[i - 1][j][0] + dp[i - 1][j][1]) % mod;
      if (!is_end[i]) {
        dp[i][j][1] = dp[i - 1][j - 1][0];
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= n; ++i) {
    ll value = (dp[2 * n][i][0] + dp[2 * n][i][1]) % mod * frac[n - i] % mod;
    if (i & 1)
      value = (mod - value) % mod;
    ans = (ans + value) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 2020
#define mod 924844033
#define ll long long
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0')
    ch == '-' && (f = 0) || (ch = getchar());
  while (ch <= '9' && ch >= '0')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return f ? x : -x;
}
bool is_end[N];
int dp[N << 1][N][2], a[N];
ll frac[N];
int main(int argc, char const *argv[]) {
  // freopen("../../temp/.in", "r", stdin);
  int n = read(), k = read();
  int top = 1;
  for (int i = frac[0] = 1; i <= n; ++i) {
    frac[i] = frac[i - 1] * i % mod;
  }
  for (int i = 1; i <= k; ++i) {
    int len = (n - i) / k + 1;
    is_end[top] = 1;
    top += len;
    is_end[top] = 1;
    top += len;
  }
  dp[0][0][0] = 1;
  for (int i = 1; i <= 2 * n; ++i) {
    for (int j = 0; j <= i / 2; ++j) {
      dp[i][j][0] = (dp[i - 1][j][0] + dp[i - 1][j][1]) % mod;
      if (!is_end[i]) {
        dp[i][j][1] = dp[i - 1][j - 1][0];
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= n; ++i) {
    ll value = (dp[2 * n][i][0] + dp[2 * n][i][1]) % mod * frac[n - i] % mod;
    if (i & 1)
      value = (mod - value) % mod;
    ans = (ans + value) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03989 (C++)

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

const int mod = 924844033;
int n, k;
int dp[4005][4005][2];
int ways[4005][4005];
int fac[4005];

void add(int &a, int b) {
  a += b % mod;
  if (a >= mod)
    a -= mod;
}
int mul(int a, int b) { return 1LL * a * b % mod; }

int main() {
  scanf("%d %d", &n, &k);
  ways[0][0] = 1;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = 1LL * fac[i - 1] * i % mod;
  int nw = 0;
  for (int i = 0; i < min(2 * k, n); ++i) {
    for (int j = i; j - 2 * k < n; j += 2 * k) {
      for (int l = 0; l <= n / (2 * k) + 1; ++l) {
        dp[j][l][0] = dp[j][l][1] = 0;
      }
    }
    dp[i][0][0] = 1;
    int lst = i;
    for (int j = i; j < n; j += 2 * k, lst = j) {
      for (int l = 0; l <= n / (2 * k) + 1; ++l)
        if (dp[j][l][0] || dp[j][l][1]) {
          add(dp[j + 2 * k][l][0], dp[j][l][0] + dp[j][l][1]);
          if (j - k >= 0)
            add(dp[j + 2 * k][l + 1][0], dp[j][l][0]);
          if (j + k < n)
            add(dp[j + 2 * k][l + 1][1], dp[j][l][0] + dp[j][l][1]);
        }
    }
    nw += n / (2 * k) + 1;
    for (int j = 0; j <= nw; ++j) {
      for (int l = 0; l <= n / (2 * k) + 1 && l <= j; ++l) {
        add(ways[i + 1][j], mul(ways[i][j - l], dp[lst][l][0] + dp[lst][l][1]));
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    int cur = mul(ways[min(2 * k, n)][i], fac[n - i]);
    if (i & 1)
      (ans += mod - cur) %= mod;
    else
      (ans += cur) %= mod;
  }
  printf("%d\n", ans);
  return 0;
}

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

const int mod = 924844033;
int n, k;
int dp[6005][6005][2];
int ways[6005][6005];
int fac[6005];

void add(int &a, int b) {
  a += b % mod;
  if (a >= mod)
    a -= mod;
}
int mul(int a, int b) { return 1LL * a * b % mod; }

int main() {
  scanf("%d %d", &n, &k);
  ways[0][0] = 1;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = 1LL * fac[i - 1] * i % mod;
  int nw = 0;
  for (int i = 0; i < min(2 * k, n); ++i) {
    for (int j = i; j - 2 * k < n; j += 2 * k) {
      for (int l = 0; l <= n / (2 * k) + 1; ++l) {
        dp[j][l][0] = dp[j][l][1] = 0;
      }
    }
    dp[i][0][0] = 1;
    int lst = i;
    for (int j = i; j < n; j += 2 * k, lst = j) {
      for (int l = 0; l <= n / (2 * k) + 1; ++l)
        if (dp[j][l][0] || dp[j][l][1]) {
          add(dp[j + 2 * k][l][0], dp[j][l][0] + dp[j][l][1]);
          if (j - k >= 0)
            add(dp[j + 2 * k][l + 1][0], dp[j][l][0]);
          if (j + k < n)
            add(dp[j + 2 * k][l + 1][1], dp[j][l][0] + dp[j][l][1]);
        }
    }
    nw += n / (2 * k) + 1;
    for (int j = 0; j <= nw; ++j) {
      for (int l = 0; l <= n / (2 * k) + 1 && l <= j; ++l) {
        add(ways[i + 1][j], mul(ways[i][j - l], dp[lst][l][0] + dp[lst][l][1]));
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    int cur = mul(ways[min(2 * k, n)][i], fac[n - i]);
    if (i & 1)
      (ans += mod - cur) %= mod;
    else
      (ans += cur) %= mod;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define inf 1e9
#define eps 1e-6
#define N 2010
#define mod 924844033
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read() {
  char ch = getchar();
  ll s = 0, w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
ll f[N][N][2];
int n, k;
int ed[N], tot;
ll fac[N];
int vis[N][2];
int main() {
  n = read(), k = read();
  fac[0] = 1;
  for (register int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mod;
  for (register int i = 1; i <= n; i++) {
    for (register int j = 0; j <= 1; j++) {
      if (!vis[i][j]) {
        for (register int now = i, p = j; now <= n; now += k, p ^= 1)
          ++tot, vis[now][p] = 1;
        ed[tot] = 1;
      }
    }
  }
  //	cout<<ed[tot]<<endl;
  f[1][0][0] = 1;
  for (register int i = 1; i <= tot; i++) {
    for (register int j = 0; j <= n; j++) {
      f[i + 1][j][0] = (f[i][j][0] + f[i][j][1]) % mod;
      if (!ed[i])
        f[i + 1][j + 1][1] = f[i][j][0];
    }
  }
  ll ans = fac[n], c = -1;
  for (register int i = 1; i <= n; i++) {
    ans = (ans + (f[tot][i][0] + f[tot][i][1]) % mod * fac[n - i] % mod * c) %
          mod;
    c = -c;
  }
  printf("%lld\n", (ans + mod) % mod);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define inf 1e9
#define eps 1e-6
#define N 4010
#define mod 924844033
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read() {
  char ch = getchar();
  ll s = 0, w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
ll f[N][N][2];
int n, k;
int ed[N], tot;
ll fac[N];
int vis[N][2];
int main() {
  n = read(), k = read();
  fac[0] = 1;
  for (register int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mod;
  for (register int i = 1; i <= n; i++) {
    for (register int j = 0; j <= 1; j++) {
      if (!vis[i][j]) {
        for (register int now = i, p = j; now <= n; now += k, p ^= 1)
          ++tot, vis[now][p] = 1;
        ed[tot] = 1;
      }
    }
  }
  //	cout<<ed[tot]<<endl;
  f[1][0][0] = 1;
  for (register int i = 1; i <= tot; i++) {
    for (register int j = 0; j <= n; j++) {
      f[i + 1][j][0] = (f[i][j][0] + f[i][j][1]) % mod;
      if (!ed[i])
        f[i + 1][j + 1][1] = f[i][j][0];
    }
  }
  ll ans = fac[n], c = -1;
  for (register int i = 1; i <= n; i++) {
    ans = (ans + (f[tot][i][0] + f[tot][i][1]) % mod * fac[n - i] % mod * c) %
          mod;
    c = -c;
  }
  printf("%lld\n", (ans + mod) % mod);
  return 0;
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
long long int mod = 924844033;
long long int dp[2003][2002][2];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int num, kai;
  cin >> num >> kai;
  dp[0][0][0] = 1;
  int pt = 0;
  for (int i = 0; i < kai * 2; i++) {
    int t = 0;
    int nx, ny;
    if (i < kai)
      nx = i, ny = i + kai;
    else
      nx = i, ny = i - kai;
    while (true) {
      if (nx >= num || ny >= num) {
        break;
      }
      t++;
      if (nx < ny) {
        nx += kai * 2;
      } else {
        ny += kai * 2;
      }
    }
    for (int j = 0; j < t; j++) {
      for (int k = 0; k <= num; k++) {
        dp[pt + 1][k][0] += dp[pt][k][0] + dp[pt][k][1];
        dp[pt + 1][k + 1][1] += dp[pt][k][0];
        dp[pt + 1][k][0] %= mod;
        dp[pt + 1][k + 1][1] %= mod;
      }
      pt++;
    }
    for (int k = 0; k <= num; k++) {
      dp[pt][k][0] += dp[pt][k][1];
      dp[pt][k][0] %= mod;
      dp[pt][k][1] = 0;
    }
  }
  long long int ans = 0;
  long long int now = 1;
  for (int i = num; i >= 0; i--) {
    if (i % 2 == 0) {
      ans += dp[pt][i][0] * now;
    } else {
      ans += mod - dp[pt][i][0] * now % mod;
    }
    ans %= mod;
    now *= (num - i + 1);
    now %= mod;
  }
  cout << ans;
}


Fixed submission:
#include <iostream>
using namespace std;
long long int mod = 924844033;
long long int dp[4003][2002][2];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int num, kai;
  cin >> num >> kai;
  dp[0][0][0] = 1;
  int pt = 0;
  for (int i = 0; i < kai * 2; i++) {
    int t = 0;
    int nx, ny;
    if (i < kai)
      nx = i, ny = i + kai;
    else
      nx = i, ny = i - kai;
    while (true) {
      if (nx >= num || ny >= num) {
        break;
      }
      t++;
      if (nx < ny) {
        nx += kai * 2;
      } else {
        ny += kai * 2;
      }
    }
    for (int j = 0; j < t; j++) {
      for (int k = 0; k <= num; k++) {
        dp[pt + 1][k][0] += dp[pt][k][0] + dp[pt][k][1];
        dp[pt + 1][k + 1][1] += dp[pt][k][0];
        dp[pt + 1][k][0] %= mod;
        dp[pt + 1][k + 1][1] %= mod;
      }
      pt++;
    }
    for (int k = 0; k <= num; k++) {
      dp[pt][k][0] += dp[pt][k][1];
      dp[pt][k][0] %= mod;
      dp[pt][k][1] = 0;
    }
  }
  long long int ans = 0;
  long long int now = 1;
  for (int i = num; i >= 0; i--) {
    if (i % 2 == 0) {
      ans += dp[pt][i][0] * now;
    } else {
      ans += mod - dp[pt][i][0] * now % mod;
    }
    ans %= mod;
    now *= (num - i + 1);
    now %= mod;
  }
  cout << ans;
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
typedef long long LL;
const LL mod = 924844033;
// head
const int maxn = 2005;
int n, k, cnt;
LL fac[maxn], f[maxn][maxn][2], g[maxn], ans;
bool vis[maxn];

int main() {
  cin >> n >> k;
  rep(i, 1, k) {
    for (int j = i; j <= n; j += k)
      vis[++cnt] = (j == i);
    for (int j = i; j <= n; j += k)
      vis[++cnt] = (j == i);
  }
  f[0][0][0] = fac[0] = 1;
  rep(i, 1, n) fac[i] = i * fac[i - 1] % mod;
  rep(i, 1, n << 1) rep(j, 0, min(i, n)) {
    f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % mod;
    if (!vis[i])
      f[i][j][1] = f[i - 1][j - 1][0];
  }
  rep(i, 0, n) {
    int op = (i & 1) ? -1 : 1;
    g[i] = (f[n << 1][i][0] + f[n << 1][i][1]) % mod;
    ans = (ans + op * g[i] * fac[n - i] % mod + mod) % mod;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
typedef long long LL;
const LL mod = 924844033;
// head
const int maxn = 2005;
int n, k, cnt;
LL fac[maxn], f[maxn << 1][maxn][2], g[maxn], ans;
bool vis[maxn << 1];

int main() {
  cin >> n >> k;
  rep(i, 1, k) {
    for (int j = i; j <= n; j += k)
      vis[++cnt] = (j == i);
    for (int j = i; j <= n; j += k)
      vis[++cnt] = (j == i);
  }
  f[0][0][0] = fac[0] = 1;
  rep(i, 1, n) fac[i] = i * fac[i - 1] % mod;
  rep(i, 1, n << 1) rep(j, 0, min(i, n)) {
    f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % mod;
    if (!vis[i])
      f[i][j][1] = f[i - 1][j - 1][0];
  }
  rep(i, 0, n) {
    int op = (i & 1) ? -1 : 1;
    g[i] = (f[n << 1][i][0] + f[n << 1][i][1]) % mod;
    ans = (ans + op * g[i] * fac[n - i] % mod + mod) % mod;
  }
  cout << ans << endl;
}
Problem p03990 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 100005
const int INF = 0x3f3f3f3f;
struct edge {
  int t, next;
} e[N * 4];
int ecnt, head[N * 2]; // 1~n blue n+1~n+n red
void addedge(int f, int t) {
  e[++ecnt] = (edge){t, head[f]};
  head[f] = ecnt;
  e[++ecnt] = (edge){f, head[t]};
  head[t] = ecnt;
}
int fa[N], dis[N];
void DFS(int u, int fr) {
  fa[u] = fr;
  dis[u] = dis[fr] + 1;
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].t != fr)
      DFS(e[i].t, u);
}
bool judge(int f,
           int t) { // is the distance between f and t is smaller or equal to 2
  if (dis[f] == dis[t])
    return fa[f] == fa[t];
  if (dis[f] > dis[t])
    swap(f, t);
  return fa[t] == f || fa[fa[t]] == f;
}
int n, x, y, ans;
void DFS2(int u, int fr, int d) {
  if (dis[u - n] <= d)
    return;
  ans = max(ans, dis[u - n]);
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].t != fr) {
      DFS2(e[i].t, u, d + 1);
      if (judge(u - n, e[i].t - n) == false)
        ans = INF;
    }
}
int main() {
  scanf("%d%d%d", &n, &x, &y);
  for (int i = 1, f, t; i < n; i++)
    scanf("%d%d", &f, &t), addedge(f + n, t + n);
  for (int i = 1, f, t; i < n; i++)
    scanf("%d%d", &f, &t), addedge(f, t);
  dis[0] = -1;
  DFS(y, 0);
  DFS2(x + n, 0, 0);
  if (ans == INF)
    ans = -1;
  else
    ans *= 2;
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 200005
const int INF = 0x3f3f3f3f;
struct edge {
  int t, next;
} e[N * 4];
int ecnt, head[N * 2]; // 1~n blue n+1~n+n red
void addedge(int f, int t) {
  e[++ecnt] = (edge){t, head[f]};
  head[f] = ecnt;
  e[++ecnt] = (edge){f, head[t]};
  head[t] = ecnt;
}
int fa[N], dis[N];
void DFS(int u, int fr) {
  fa[u] = fr;
  dis[u] = dis[fr] + 1;
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].t != fr)
      DFS(e[i].t, u);
}
bool judge(int f,
           int t) { // is the distance between f and t is smaller or equal to 2
  if (dis[f] == dis[t])
    return fa[f] == fa[t];
  if (dis[f] > dis[t])
    swap(f, t);
  return fa[t] == f || fa[fa[t]] == f;
}
int n, x, y, ans;
void DFS2(int u, int fr, int d) {
  if (dis[u - n] <= d)
    return;
  ans = max(ans, dis[u - n]);
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].t != fr) {
      DFS2(e[i].t, u, d + 1);
      if (judge(u - n, e[i].t - n) == false)
        ans = INF;
    }
}
int main() {
  scanf("%d%d%d", &n, &x, &y);
  for (int i = 1, f, t; i < n; i++)
    scanf("%d%d", &f, &t), addedge(f + n, t + n);
  for (int i = 1, f, t; i < n; i++)
    scanf("%d%d", &f, &t), addedge(f, t);
  dis[0] = -1;
  DFS(y, 0);
  DFS2(x + n, 0, 0);
  if (ans == INF)
    ans = -1;
  else
    ans *= 2;
  printf("%d\n", ans);
  return 0;
}
Problem p03990 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define Rep(i, a) for (int i = 0; i < a; i++)
using namespace std;
const int N = 1e5 + 10;

struct edge {
  int to, pre;
} e[N << 1];
int l = 0, u[N];
void ins(int a, int b) { e[++l] = (edge){b, u[a]}, u[a] = l; }
#define v e[i].to
#define reg(i, a) for (int i = u[a]; i; i = e[i].pre)

int n, X, Y, a[N], b[N], dep[N], fa[N];

void dfs(int x, int f) {
  reg(i, x) if (v != f) dep[v] = dep[x] + 1, fa[v] = x, dfs(v, x);
}

bool jump(int a, int b) {
  if (dep[a] < dep[b])
    swap(a, b);
  if (fa[a] == b || fa[fa[a]] == b || fa[a] == fa[b])
    return false;
  else
    return true;
}

vector<int> adj[N];
#define pb(a) push_back(a)

bool inf = false, win[N];
int ans = 0;
void chk(int x, int f, int d) {
  if (d >= dep[x])
    return;
  else
    ans = max(ans, dep[x]);
  inf |= win[x];
  int l = adj[x].size();
  Rep(j, l) if (adj[x][j] != f) chk(adj[x][j], x, d + 1);
}

int main() {
  scanf("%d%d%d", &n, &X, &Y);
  rep(i, 1, n - 1) scanf("%d%d", a + i, b + i);
  rep(i, 1, n - 1) {
    int a, b;
    scanf("%d%d", &a, &b);
    ins(a, b), ins(b, a);
  }
  dfs(Y, 0);
  rep(i, 1, n - 1) if (!jump(a[i], b[i])) adj[a[i]].pb(b[i]),
      adj[b[i]].pb(a[i]);
  else win[a[i]] = win[b[i]] = true;
  chk(X, 0, 0);
  if (inf)
    printf("-1\n");
  else
    printf("%d\n", ans * 2);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define Rep(i, a) for (int i = 0; i < a; i++)
using namespace std;
const int N = 2e5 + 10;

struct edge {
  int to, pre;
} e[N << 1];
int l = 0, u[N];
void ins(int a, int b) { e[++l] = (edge){b, u[a]}, u[a] = l; }
#define v e[i].to
#define reg(i, a) for (int i = u[a]; i; i = e[i].pre)

int n, X, Y, a[N], b[N], dep[N], fa[N];

void dfs(int x, int f) {
  reg(i, x) if (v != f) dep[v] = dep[x] + 1, fa[v] = x, dfs(v, x);
}

bool jump(int a, int b) {
  if (dep[a] < dep[b])
    swap(a, b);
  if (fa[a] == b || fa[fa[a]] == b || fa[a] == fa[b])
    return false;
  else
    return true;
}

vector<int> adj[N];
#define pb(a) push_back(a)

bool inf = false, win[N];
int ans = 0;
void chk(int x, int f, int d) {
  if (d >= dep[x])
    return;
  else
    ans = max(ans, dep[x]);
  inf |= win[x];
  int l = adj[x].size();
  Rep(j, l) if (adj[x][j] != f) chk(adj[x][j], x, d + 1);
}

int main() {
  scanf("%d%d%d", &n, &X, &Y);
  rep(i, 1, n - 1) scanf("%d%d", a + i, b + i);
  rep(i, 1, n - 1) {
    int a, b;
    scanf("%d%d", &a, &b);
    ins(a, b), ins(b, a);
  }
  dfs(Y, 0);
  rep(i, 1, n - 1) if (!jump(a[i], b[i])) adj[a[i]].pb(b[i]),
      adj[b[i]].pb(a[i]);
  else win[a[i]] = win[b[i]] = true;
  chk(X, 0, 0);
  if (inf)
    printf("-1\n");
  else
    printf("%d\n", ans * 2);
  return 0;
}
Problem p03990 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 100005;
int deep[N], dfn[N], ed[N], fa[N], dis[N], Time, ans;
struct Tree {
  int Head[N], Next[N << 1], Adj[N << 1], tot;
  inline void addedge(int u, int v) {
    Next[++tot] = Head[u], Head[u] = tot, Adj[tot] = v;
    Next[++tot] = Head[v], Head[v] = tot, Adj[tot] = u;
  }
  inline void dfs(int x, int f) {
    dfn[x] = ++Time, fa[x] = f;
    for (int e = Head[x]; e; e = Next[e])
      if (Adj[e] != f) {
        dis[Adj[e]] = dis[x] + 1;
        dfs(Adj[e], x);
      }
    ed[x] = Time;
  }
  inline bool check(int x, int y) {
    if (dfn[x] > dfn[y])
      swap(x, y);
    if (dfn[y] >= dfn[x] && dfn[y] <= ed[x])
      return dis[y] - dis[x] > 2;
    return fa[x] != fa[y];
  }
  inline void dfs2(int x, int f) {
    if (deep[x] >= dis[x])
      return;
    ans = max(ans, dis[x] * 2);
    for (int e = Head[x]; e; e = Next[e])
      if (Adj[e] != f) {
        deep[Adj[e]] = deep[x] + 1;
        dfs2(Adj[e], x);
        if (check(x, Adj[e])) {
          puts("-1");
          exit(0);
        }
      }
  }
} T1, T2;
int main() {
  int n, x, y;
  scanf("%d%d%d", &n, &x, &y);
  if (x == y) {
    puts("0");
    return 0;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    T1.addedge(u, v);
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    T2.addedge(u, v);
  }
  T2.dfs(y, 0);
  T1.dfs2(x, 0);
  printf("%d", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 200005;
int deep[N], dfn[N], ed[N], fa[N], dis[N], Time, ans;
struct Tree {
  int Head[N], Next[N << 1], Adj[N << 1], tot;
  inline void addedge(int u, int v) {
    Next[++tot] = Head[u], Head[u] = tot, Adj[tot] = v;
    Next[++tot] = Head[v], Head[v] = tot, Adj[tot] = u;
  }
  inline void dfs(int x, int f) {
    dfn[x] = ++Time, fa[x] = f;
    for (int e = Head[x]; e; e = Next[e])
      if (Adj[e] != f) {
        dis[Adj[e]] = dis[x] + 1;
        dfs(Adj[e], x);
      }
    ed[x] = Time;
  }
  inline bool check(int x, int y) {
    if (dfn[x] > dfn[y])
      swap(x, y);
    if (dfn[y] >= dfn[x] && dfn[y] <= ed[x])
      return dis[y] - dis[x] > 2;
    return fa[x] != fa[y];
  }
  inline void dfs2(int x, int f) {
    if (deep[x] >= dis[x])
      return;
    ans = max(ans, dis[x] * 2);
    for (int e = Head[x]; e; e = Next[e])
      if (Adj[e] != f) {
        deep[Adj[e]] = deep[x] + 1;
        dfs2(Adj[e], x);
        if (check(x, Adj[e])) {
          puts("-1");
          exit(0);
        }
      }
  }
} T1, T2;
int main() {
  int n, x, y;
  scanf("%d%d%d", &n, &x, &y);
  if (x == y) {
    puts("0");
    return 0;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    T1.addedge(u, v);
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    T2.addedge(u, v);
  }
  T2.dfs(y, 0);
  T1.dfs2(x, 0);
  printf("%d", ans);
  return 0;
}
Problem p03990 (C++)

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

const int N = 200005;
int n, x, y;

int a[N], b[N], c[N], d[N];
struct tree {
  int head[N], tot;
  struct edge {
    int to, nxt;
  } e[N << 1];
  void link(int x, int y) {
    e[++tot] = (edge){y, head[x]}, head[x] = tot;
    e[++tot] = (edge){x, head[y]}, head[y] = tot;
  }
} R, B;
bool mark[N];

int depB[N], fB[N];
void dfs0(int x) {
  static auto head = B.head;
  static auto e = B.e;
  for (int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != fB[x])
      fB[e[i].to] = x, depB[e[i].to] = depB[x] + 1, dfs0(e[i].to);
}
int ans = 0;
void dfs1(int x, int f = 0, int dep = 0) {
  if (dep >= depB[x])
    return;
  if (mark[x])
    std::cout << -1 << '\n', std::exit(0);
  ans = std::max(ans, depB[x]);
  static auto head = R.head;
  static auto e = R.e;
  for (int i = head[x]; i; i = e[i].nxt)
    if (depB[e[i].to] > dep)
      dfs1(e[i].to, x, dep + 1);
}

bool qaq(int x, int y) {
  if (depB[x] > depB[y])
    std::swap(x, y);
  auto f = fB;
  return f[y] == f[x] || f[y] == x || f[f[y]] == x;
}

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> x >> y;
  for (int i = 1; i < n; ++i)
    std::cin >> a[i] >> b[i], R.link(a[i], b[i]);
  for (int i = 1; i < n; ++i)
    std::cin >> c[i] >> d[i], B.link(c[i], d[i]);
  dfs0(y);
  for (int i = 1; i < n; ++i)
    if (!qaq(a[i], b[i]))
      mark[a[i]] = mark[b[i]] = 1;
  dfs1(x), std::cout << ans * 2 << '\n';
  return 0;
}

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

const int N = 200005;
int n, x, y;

int a[N], b[N], c[N], d[N];
struct tree {
  int head[N], tot;
  struct edge {
    int to, nxt;
  } e[N << 1];
  void link(int x, int y) {
    e[++tot] = (edge){y, head[x]}, head[x] = tot;
    e[++tot] = (edge){x, head[y]}, head[y] = tot;
  }
} R, B;
bool mark[N];

int depB[N], fB[N];
void dfs0(int x) {
  static auto head = B.head;
  static auto e = B.e;
  for (int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != fB[x])
      fB[e[i].to] = x, depB[e[i].to] = depB[x] + 1, dfs0(e[i].to);
}
int ans = 0;
void dfs1(int x, int f = 0, int dep = 0) {
  if (dep >= depB[x])
    return;
  if (mark[x])
    std::cout << -1 << '\n', std::exit(0);
  ans = std::max(ans, depB[x]);
  static auto head = R.head;
  static auto e = R.e;
  for (int i = head[x]; i; i = e[i].nxt)
    if (depB[e[i].to] > dep && e[i].to != f)
      dfs1(e[i].to, x, dep + 1);
}

bool qaq(int x, int y) {
  if (depB[x] > depB[y])
    std::swap(x, y);
  auto f = fB;
  return f[y] == f[x] || f[y] == x || f[f[y]] == x;
}

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> x >> y;
  for (int i = 1; i < n; ++i)
    std::cin >> a[i] >> b[i], R.link(a[i], b[i]);
  for (int i = 1; i < n; ++i)
    std::cin >> c[i] >> d[i], B.link(c[i], d[i]);
  dfs0(y);
  for (int i = 1; i < n; ++i)
    if (!qaq(a[i], b[i]))
      mark[a[i]] = mark[b[i]] = 1;
  dfs1(x), std::cout << ans * 2 << '\n';
  return 0;
}
Problem p03990 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define N (400000 + 1000)
using namespace std;

struct edge {
  int to, next;
} edge[N << 1];
int Father[N], Depth[N], Dfn[N], Low[N];
int n, x, y, S1, S2, cnt, ans, u[N], v[N];
int head[N], num_edge;

void add(int u, int v) {
  edge[++num_edge].to = v;
  edge[num_edge].next = head[u];
  head[u] = num_edge;
}

bool check(int x, int y) {
  if (Dfn[x] > Dfn[y])
    swap(x, y);
  if (Low[x] >= Low[y])
    return (Depth[y] - Depth[x]) > 2;
  return Father[x] != Father[y];
}

void Dfs1(int x) {
  Dfn[x] = ++cnt;
  Depth[x] = Depth[Father[x]] + 1;
  for (int i = head[x]; i; i = edge[i].next)
    if (edge[i].to != Father[x]) {
      Father[edge[i].to] = x;
      Dfs1(edge[i].to);
    }
  Low[x] = ++cnt;
}

void Dfs2(int x, int fa, int k) {
  ans = max(ans, Depth[x] - 1);
  if (Depth[x] <= k)
    return;
  for (int i = head[x]; i; i = edge[i].next) {
    if (check(x, edge[i].to))
      puts("-1"), exit(0);
    Dfs2(edge[i].to, x, k + 1);
  }
}

int main() {
  scanf("%d%d%d", &n, &S1, &S2);
  for (int i = 1; i <= n - 1; ++i)
    scanf("%d%d", &u[i], &v[i]);
  for (int i = 1; i <= n - 1; ++i)
    scanf("%d%d", &x, &y), add(x, y), add(y, x);
  Dfs1(S2);
  memset(head, 0, sizeof(head));
  num_edge = 0;
  for (int i = 1; i <= n - 1; ++i)
    add(u[i], v[i]), add(v[i], u[i]);
  Dfs2(S1, -1, 1);
  printf("%d\n", ans << 1);
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define N (400000 + 1000)
using namespace std;

struct edge {
  int to, next;
} edge[N << 1];
int Father[N], Depth[N], Dfn[N], Low[N];
int n, x, y, S1, S2, cnt, ans, u[N], v[N];
int head[N], num_edge;

void add(int u, int v) {
  edge[++num_edge].to = v;
  edge[num_edge].next = head[u];
  head[u] = num_edge;
}

bool check(int x, int y) {
  if (Dfn[x] > Dfn[y])
    swap(x, y);
  if (Low[x] >= Low[y])
    return (Depth[y] - Depth[x]) > 2;
  return Father[x] != Father[y];
}

void Dfs1(int x) {
  Dfn[x] = ++cnt;
  Depth[x] = Depth[Father[x]] + 1;
  for (int i = head[x]; i; i = edge[i].next)
    if (edge[i].to != Father[x]) {
      Father[edge[i].to] = x;
      Dfs1(edge[i].to);
    }
  Low[x] = ++cnt;
}

void Dfs2(int x, int fa, int k) {
  ans = max(ans, Depth[x] - 1);
  if (Depth[x] <= k)
    return;
  for (int i = head[x]; i; i = edge[i].next)
    if (edge[i].to != fa) {
      if (check(x, edge[i].to))
        puts("-1"), exit(0);
      Dfs2(edge[i].to, x, k + 1);
    }
}

int main() {
  scanf("%d%d%d", &n, &S1, &S2);
  for (int i = 1; i <= n - 1; ++i)
    scanf("%d%d", &u[i], &v[i]);
  for (int i = 1; i <= n - 1; ++i)
    scanf("%d%d", &x, &y), add(x, y), add(y, x);
  Dfs1(S2);
  memset(head, 0, sizeof(head));
  num_edge = 0;
  for (int i = 1; i <= n - 1; ++i)
    add(u[i], v[i]), add(v[i], u[i]);
  Dfs2(S1, -1, 1);
  printf("%d\n", ans << 1);
}
Problem p03990 (C++)

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

#define PB push_back
#define SZ(c) int((c).size())
#define ALL(c) (c).begin(), (c).end()
#define CONTAINS(c, x) ((c).find(x) != (c).end())
#define REP(i, n) for (int i = 1; i <= n; i++)
#define WAIT cout << flush, system("PAUSE");
using namespace std;
const int MAX = 100100;

int N, A, B, u, v;
set<int> E[MAX], F[MAX];
int D[MAX], P[MAX], sol;
bool ok[MAX], used[MAX], escape;

void DFS(int n) {
  for (auto x : F[n])
    if (x != P[n]) {
      P[x] = n;
      D[x] = D[n] + 1;
      DFS(x);
    }
}

void DFS2(int n, int d) {
  if (D[n] <= d)
    return;
  used[n] = true;
  sol = max(sol, D[n]);
  if (ok[n])
    escape = true;

  for (auto x : E[n])
    if (!used[x]) {
      DFS2(x, d + 1);
    }
}

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

  cin >> N >> A >> B;
  REP(I, N - 1) {
    cin >> u >> v;
    E[u].insert(v);
    E[v].insert(u);
  }

  REP(I, N - 1) {
    cin >> u >> v;
    F[u].insert(v);
    F[v].insert(u);
  }

  P[B] = D[B] = 0;
  DFS(B);

  REP(u, N)
  for (auto v : E[u]) if (CONTAINS(F[u], v) || CONTAINS(F[v], u) ||
                          CONTAINS(F[P[u]], v) || CONTAINS(F[P[v]], u));
  else ok[u] = ok[v] = true;

  // REP(I, N) if (ok[I]) cout << I << " ";
  // cout << endl;
  // REP(I, N) cout << D[I] << " ";
  // cout << endl;

  DFS2(A, 0);
  if (escape)
    sol = -1;
  else
    sol *= 2;
  cout << sol << endl;
}


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

#define PB push_back
#define SZ(c) int((c).size())
#define ALL(c) (c).begin(), (c).end()
#define CONTAINS(c, x) ((c).find(x) != (c).end())
#define REP(i, n) for (int i = 1; i <= n; i++)
#define WAIT cout << flush, system("PAUSE");
using namespace std;
const int MAX = 200100;

int N, A, B, u, v;
set<int> E[MAX], F[MAX];
int D[MAX], P[MAX], sol;
bool ok[MAX], used[MAX], escape;

void DFS(int n) {
  for (auto x : F[n])
    if (x != P[n]) {
      P[x] = n;
      D[x] = D[n] + 1;
      DFS(x);
    }
}

void DFS2(int n, int d) {
  if (D[n] <= d)
    return;
  used[n] = true;
  sol = max(sol, D[n]);
  if (ok[n])
    escape = true;

  for (auto x : E[n])
    if (!used[x]) {
      DFS2(x, d + 1);
    }
}

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

  cin >> N >> A >> B;
  REP(I, N - 1) {
    cin >> u >> v;
    E[u].insert(v);
    E[v].insert(u);
  }

  REP(I, N - 1) {
    cin >> u >> v;
    F[u].insert(v);
    F[v].insert(u);
  }

  P[B] = D[B] = 0;
  DFS(B);

  REP(u, N)
  for (auto v : E[u]) if (CONTAINS(F[u], v) || CONTAINS(F[v], u) ||
                          CONTAINS(F[P[u]], v) || CONTAINS(F[P[v]], u));
  else ok[u] = ok[v] = true;

  // REP(I, N) if (ok[I]) cout << I << " ";
  // cout << endl;
  // REP(I, N) cout << D[I] << " ";
  // cout << endl;

  DFS2(A, 0);
  if (escape)
    sol = -1;
  else
    sol *= 2;
  cout << sol << endl;
}
Problem p03992 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
  char s[14];
  int i;
  for (int i = 0; i < 14; i++) {
    s[i] = 0;
  }
  scanf("%s", s);
  for (int i = 100; i > 3; i--) {
    s[i + 1] = s[i];
  }
  s[4] = ' ';
  printf("%s\n", s);
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
  char s[14];
  int i;
  for (int i = 0; i < 14; i++) {
    s[i] = 0;
  }
  scanf("%s", s);
  for (int i = 12; i > 3; i--) {
    s[i + 1] = s[i];
  }
  s[4] = ' ';
  printf("%s\n", s);
  return 0;
}
Problem p03992 (C++)

Buggy submission (status: Runtime Error):
// string::substr
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[]) {
  string str = argv[1];
  // (quoting Alfred N. Whitehead)

  string str2 = str.substr(0, 4); // "think"
  string str3 = str.substr(4);    // get from "live" to the end

  cout << str2 << ' ' << str3 << '\n';

  return 0;
}

Fixed submission:
// string::substr
#include <iostream>
#include <string>
using namespace std;

int main() {
  string str;

  cin >> str;
  // (quoting Alfred N. Whitehead)

  string str2 = str.substr(0, 4); // "think"
  string str3 = str.substr(4);    // get from "live" to the end

  cout << str2 << ' ' << str3 << '\n';

  return 0;
}