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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;

ll dp[50][50][50 * 50 + 1]; // dp[i][j][k] :=
// i番目まで見て、jこずつ添え字と数字を保留してる（互いに同じ個数となるようにしか保留ができない）とき、
// 確定してる奇妙さがkのときの場合の数
int N, K;
const ll MOD = 1000000007;

int main() {
  cin >> N >> K;
  dp[0][0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k <= N * N; k++) {
        dp[i][j][k] %= MOD;

        // 今見てる数字と添え字同士がそのままマッチングされるとき
        if (k + 2 * j <= N * N)
          dp[i + 1][j][k + 2 * j] += dp[i][j][k];

        // 今見てる数字と添え字どちらも保留とする。つまり、0~i(0-idx)番目までにマッチングさせないとき
        if (k + 2 * (j + 1) <= N * N && j <= N - 2)
          dp[i + 1][j + 1][k + 2 * (j + 1)] += dp[i][j][k];

        // 今見てる数字と添え字で、数字は保留された添え字とマッチングし、添え字はそのまま保留するとき
        // 今見てる数字と添え字で、添え字は保留された数字とマッチングし、数字はそのまま保留するとき
        // この二つは数字と添え字の保留数はかならず同じになる性質上、同じ式でまとめることができる。
        // お互い、保留された数字(or添え字)とマッチングする際に、j通りある相手のうちどれを選んでもいい。
        if (k + 2 * j <= N * N)
          dp[i + 1][j][k + 2 * j] += dp[i][j][k] * 2 * j;

        // 今見てる数字と添え字で、数字は保留された添え字とマッチング、添え字は保留された数字とマッチングするとき
        // 保留されてる数字と添え字は一つずつ減る。また、上のパターンと同様に、お互い保留されたものとはj通りの選び方があるので、
        // j*j倍される。
        if (k + 2 * (j - 1) <= N * N && j - 1 >= 0)
          dp[i + 1][j - 1][k + 2 * (j - 1)] += dp[i][j][k] * j * j;
      }
    }
  }

  cout << dp[N][0][K] % MOD << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;

ll dp[51][51][50 * 50 + 1]; // dp[i][j][k] :=
// i番目まで見て、jこずつ添え字と数字を保留してる（互いに同じ個数となるようにしか保留ができない）とき、
// 確定してる奇妙さがkのときの場合の数
int N, K;
const ll MOD = 1000000007;

int main() {
  cin >> N >> K;
  dp[0][0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k <= N * N; k++) {
        dp[i][j][k] %= MOD;

        // 今見てる数字と添え字同士がそのままマッチングされるとき
        if (k + 2 * j <= N * N)
          dp[i + 1][j][k + 2 * j] += dp[i][j][k];

        // 今見てる数字と添え字どちらも保留とする。つまり、0~i(0-idx)番目までにマッチングさせないとき
        if (k + 2 * (j + 1) <= N * N && j <= N - 2)
          dp[i + 1][j + 1][k + 2 * (j + 1)] += dp[i][j][k];

        // 今見てる数字と添え字で、数字は保留された添え字とマッチングし、添え字はそのまま保留するとき
        // 今見てる数字と添え字で、添え字は保留された数字とマッチングし、数字はそのまま保留するとき
        // この二つは数字と添え字の保留数はかならず同じになる性質上、同じ式でまとめることができる。
        // お互い、保留された数字(or添え字)とマッチングする際に、j通りある相手のうちどれを選んでもいい。
        if (k + 2 * j <= N * N)
          dp[i + 1][j][k + 2 * j] += dp[i][j][k] * 2 * j;

        // 今見てる数字と添え字で、数字は保留された添え字とマッチング、添え字は保留された数字とマッチングするとき
        // 保留されてる数字と添え字は一つずつ減る。また、上のパターンと同様に、お互い保留されたものとはj通りの選び方があるので、
        // j*j倍される。
        if (k + 2 * (j - 1) <= N * N && j - 1 >= 0)
          dp[i + 1][j - 1][k + 2 * (j - 1)] += dp[i][j][k] * j * j;
      }
    }
  }

  cout << dp[N][0][K] % MOD << endl;

  return 0;
}
Problem p02974 (C++)

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

using namespace std;
int n, kk;
long long mod = 1e9 + 7;
long long d[51][51][2800];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> kk;
  d[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= kk + 1; k++) {
        d[i + 1][j][k + j * 2] += d[i][j][k];
        d[i + 1][j][k + j * 2] %= mod;

        if (j - 1 >= 0) {
          d[i + 1][j - 1][k + (j - 1) * 2] += (d[i][j][k] * j * j);
          d[i + 1][j - 1][k + (j - 1) * 2] %= mod;
        }

        d[i + 1][j][k + j * 2] += d[i][j][k] * (2 * j);
        d[i + 1][j][k + j * 2] %= mod;

        d[i + 1][j + 1][k + (j + 1) * 2] += d[i][j][k];
        d[i + 1][j + 1][k + (j + 1) * 2] %= mod;
      }
    }
  }

  cout << d[n][0][kk] % mod << '\n';
}

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

using namespace std;
int n, kk;
long long mod = 1e9 + 7;
long long d[52][52][5000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> kk;
  d[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= kk + 1; k++) {
        d[i + 1][j][k + j * 2] += d[i][j][k];
        d[i + 1][j][k + j * 2] %= mod;

        if (j - 1 >= 0) {
          d[i + 1][j - 1][k + (j - 1) * 2] += (d[i][j][k] * j * j);
          d[i + 1][j - 1][k + (j - 1) * 2] %= mod;
        }

        d[i + 1][j][k + j * 2] += d[i][j][k] * (2 * j);
        d[i + 1][j][k + j * 2] %= mod;

        d[i + 1][j + 1][k + (j + 1) * 2] += d[i][j][k];
        d[i + 1][j + 1][k + (j + 1) * 2] %= mod;
      }
    }
  }

  cout << d[n][0][kk] % mod << '\n';
}
Problem p02974 (C++)

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

#define MIN_INT -2147483648
#define MAX_INT 2147483647
#define MIN_LONG -9223372036854775808L
#define MAX_LONG 9223372036854775807L

#define long long long int

using namespace std;

// @author: pashka

const long MOD = 1000000007;

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

  int n, k;
  cin >> n >> k;
  vector<vector<long>> d(2 * n * n, vector<long>(n + 1));
  vector<vector<long>> dd(2 * n * n, vector<long>(n + 1));
  d[n * n][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int q = 0; q < 2 * n * n; q++) {
      for (int c = 0; c <= n; c++) {
        dd[q][c] = 0;
      }
    }
    //        cout << i << "\n";
    for (int q = 0; q < 2 * n * n; q++) {
      for (int c = 0; c <= n; c++) {
        if (d[q][c] == 0)
          continue;
        //                cout << q << " " << c << " " << d[q][c] << "\n";
        dd[q - i - i][c + 1] += d[q][c];
        dd[q - i - i][c + 1] %= MOD;
        dd[q][c] += d[q][c];
        dd[q][c] %= MOD;
        if (c > 0) {
          dd[q + i + i][c - 1] += d[q][c] * c * c;
          dd[q + i + i][c - 1] %= MOD;
          dd[q][c] += d[q][c] * c * 2;
          dd[q][c] %= MOD;
        }
      }
    }
    swap(d, dd);
  }
  //    cout << n << "\n";
  //    for (int q = 0; q < 2 * n * n; q++) {
  //        for (int c = 0; c <= n; c++) {
  //            if (d[q][c] == 0) continue;
  //            cout << q << " " << c << " " << d[q][c] << "\n";
  //        }
  //    }
  cout << d[n * n + k][0] << "\n";

  return 0;
}

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

#define MIN_INT -2147483648
#define MAX_INT 2147483647
#define MIN_LONG -9223372036854775808L
#define MAX_LONG 9223372036854775807L

#define long long long int

using namespace std;

// @author: pashka

const long MOD = 1000000007;

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

  int n, k;
  cin >> n >> k;
  vector<vector<long>> d(2 * n * n + 1, vector<long>(n + 1));
  vector<vector<long>> dd(2 * n * n + 1, vector<long>(n + 1));
  d[n * n][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int q = 0; q < 2 * n * n; q++) {
      for (int c = 0; c <= n; c++) {
        dd[q][c] = 0;
      }
    }
    //        cout << i << "\n";
    for (int q = 0; q < 2 * n * n; q++) {
      for (int c = 0; c <= n; c++) {
        if (d[q][c] == 0)
          continue;
        //                cout << q << " " << c << " " << d[q][c] << "\n";
        dd[q - i - i][c + 1] += d[q][c];
        dd[q - i - i][c + 1] %= MOD;
        dd[q][c] += d[q][c];
        dd[q][c] %= MOD;
        if (c > 0) {
          dd[q + i + i][c - 1] += d[q][c] * c * c;
          dd[q + i + i][c - 1] %= MOD;
          dd[q][c] += d[q][c] * c * 2;
          dd[q][c] %= MOD;
        }
      }
    }
    swap(d, dd);
  }
  //    cout << n << "\n";
  //    for (int q = 0; q < 2 * n * n; q++) {
  //        for (int c = 0; c <= n; c++) {
  //            if (d[q][c] == 0) continue;
  //            cout << q << " " << c << " " << d[q][c] << "\n";
  //        }
  //    }
  cout << d[n * n + k][0] << "\n";

  return 0;
}
Problem p02974 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll << 50;
template <typename T> void printv(const vector<T> &s) {
  for (int i = 0; i < (int)(s.size()); ++i) {
    cout << s[i];
    if (i == (int)(s.size()) - 1)
      cout << endl;
    else
      cout << " ";
  }
}
int main() {
  cin.tie(0);
  cout << fixed << setprecision(10);
  ll n, m;
  cin >> n >> m;
  vector<vvll> dp(n + 1, vvll(n + 1, vll(m + 1, 0)));
  dp[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= n; ++j) {
      for (int k = 0; k <= m; ++k) {
        int tmpk = k + 2 * j;
        if (0 <= tmpk && tmpk <= m) {
          dp[i + 1][j][tmpk] += dp[i][j][k];
          dp[i + 1][j][tmpk] %= MOD;
          dp[i + 1][j + 1][tmpk] += dp[i][j][k];
          dp[i + 1][j + 1][tmpk] %= MOD;
          dp[i + 1][j][tmpk] += j * dp[i][j][k] % MOD;
          dp[i + 1][j][tmpk] %= MOD;
          if (j > 0)
            dp[i + 1][j - 1][tmpk] += j * j % MOD * dp[i][j][k] % MOD;
          if (j > 0)
            dp[i + 1][j - 1][tmpk] %= MOD;
          dp[i + 1][j][tmpk] += j * dp[i][j][k] % MOD;
          dp[i + 1][j][tmpk] %= MOD;
        }
      }
    }
  }
  cout << dp[n][0][m] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll << 50;
template <typename T> void printv(const vector<T> &s) {
  for (int i = 0; i < (int)(s.size()); ++i) {
    cout << s[i];
    if (i == (int)(s.size()) - 1)
      cout << endl;
    else
      cout << " ";
  }
}
int main() {
  cin.tie(0);
  cout << fixed << setprecision(10);
  ll n, m;
  cin >> n >> m;
  vector<vvll> dp(n + 1, vvll(n + 2, vll(m + 1, 0)));
  dp[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= n; ++j) {
      for (int k = 0; k <= m; ++k) {
        int tmpk = k + 2 * j;
        if (0 <= tmpk && tmpk <= m) {
          dp[i + 1][j][tmpk] += dp[i][j][k];
          dp[i + 1][j][tmpk] %= MOD;
          dp[i + 1][j + 1][tmpk] += dp[i][j][k];
          dp[i + 1][j + 1][tmpk] %= MOD;
          dp[i + 1][j][tmpk] += j * dp[i][j][k] % MOD;
          dp[i + 1][j][tmpk] %= MOD;
          if (j > 0)
            dp[i + 1][j - 1][tmpk] += j * j % MOD * dp[i][j][k] % MOD;
          if (j > 0)
            dp[i + 1][j - 1][tmpk] %= MOD;
          dp[i + 1][j][tmpk] += j * dp[i][j][k] % MOD;
          dp[i + 1][j][tmpk] %= MOD;
        }
      }
    }
  }
  cout << dp[n][0][m] << endl;
}
Problem p02974 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

const int N = 51;
const int mod = 1e9 + 7;

ll dp[N][N][N * N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, k;
  cin >> n >> k;
  if (k & 1) {
    cout << 0;
    return 0;
  }
  k >>= 1;
  dp[0][0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      for (int k = 0; k <= n * n / 2; k++) {
        (dp[i][j][k + j] += dp[i - 1][j][k]) %= mod;
        (dp[i][j + 1][k + j + 1] += dp[i - 1][j][k]) %= mod;
        (dp[i][j][k + j] += dp[i - 1][j][k] * 2 * j) %= mod;
        if (j >= 1) {
          (dp[i][j - 1][k + j - 1] += dp[i - 1][j][k] * j * j) %= mod;
        }
      }
    }
  }
  cout << dp[n][0][k];
  return 0;
}

Fixed submission:
// #pragma GCC optimize("O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

const int N = 100;
const int mod = 1e9 + 7;

ll dp[N][N][N * N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, k;
  cin >> n >> k;
  if (k & 1) {
    cout << 0;
    return 0;
  }
  k >>= 1;
  dp[0][0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      for (int k = 0; k <= n * n / 2; k++) {
        (dp[i][j][k + j] += dp[i - 1][j][k]) %= mod;
        (dp[i][j + 1][k + j + 1] += dp[i - 1][j][k]) %= mod;
        (dp[i][j][k + j] += dp[i - 1][j][k] * 2 * j) %= mod;
        if (j >= 1) {
          (dp[i][j - 1][k + j - 1] += dp[i - 1][j][k] * j * j) %= mod;
        }
      }
    }
  }
  cout << dp[n][0][k];
  return 0;
}
Problem p02974 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

int main() {
  ll n, K;
  cin >> n >> K;
  vector<vector<vector<ll>>> dp(
      n * 3, vector<vector<ll>>(n * 3, vector<ll>(n * n * 3)));
  dp[0][0][0] = 1;
  REP(i, n) {
    REP(j, n) {
      REP(k, n * n + 1) {
        dp[i + 1][j][k + j * 2] += dp[i][j][k];
        dp[i + 1][j][k + j * 2] %= MOD;

        dp[i + 1][j + 1][k + (j + 1) * 2] += dp[i][j][k];
        dp[i + 1][j + 1][k + (j + 1) * 2] %= MOD;

        dp[i + 1][j][k + j * 2] += dp[i][j][k] * 2 * j;
        dp[i + 1][j][k + j * 2] %= MOD;

        if (j - 1 >= 0) {
          dp[i + 1][j - 1][k + (j - 1) * 2] += dp[i][j][k] * j * j;
          dp[i + 1][j - 1][k + (j - 1) * 2] %= MOD;
        }
      }
    }
  }
  cout << dp[n][0][K] << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

int main() {
  ll n, K;
  cin >> n >> K;
  vector<vector<vector<ll>>> dp(55, vector<vector<ll>>(55, vector<ll>(3000)));
  dp[0][0][0] = 1;
  REP(i, n) {
    REP(j, n) {
      REP(k, n * n + 1) {
        dp[i + 1][j][k + j * 2] += dp[i][j][k];
        dp[i + 1][j][k + j * 2] %= MOD;

        dp[i + 1][j + 1][k + (j + 1) * 2] += dp[i][j][k];
        dp[i + 1][j + 1][k + (j + 1) * 2] %= MOD;

        dp[i + 1][j][k + j * 2] += dp[i][j][k] * 2 * j;
        dp[i + 1][j][k + j * 2] %= MOD;

        if (j - 1 >= 0) {
          dp[i + 1][j - 1][k + (j - 1) * 2] += dp[i][j][k] * j * j;
          dp[i + 1][j - 1][k + (j - 1) * 2] %= MOD;
        }
      }
    }
  }
  cout << dp[n][0][K] << endl;
}
Problem p02974 (Python)

Buggy submission (status: Runtime Error):
import sys

input = sys.stdin.readline
sys.setrecursionlimit(100000)
mod = 10**9 + 7


def read_values():
    return map(int, input().split())


def read_index():
    return map(lambda x: int(x) - 1, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v

    def __str__(self):
        return str(self.v)

    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n)


def main():
    N, K = read_values()
    dp = [
        [[0 for i in range(N**2 + 1)] for ii in range(N + 1)] for iii in range(N + 1)
    ]
    dp[0][0][0] = 1

    for i in range(1, N + 1):
        for j in range(N + 1):
            for k in range(K + 1):
                if k - 2 * j < 0:
                    continue

                dp[i][j][k] = (
                    (2 * j + 1) * dp[i - 1][j][k - 2 * j]
                    + (j + 1) * (j + 1) * dp[i - 1][j + 1][k - 2 * j]
                    + dp[i - 1][j - 1][k - 2 * j]
                ) % mod

    print(dp[N][0][K])


if __name__ == "__main__":
    main()


Fixed submission:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(100000)
mod = 10**9 + 7


def read_values():
    return map(int, input().split())


def read_index():
    return map(lambda x: int(x) - 1, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v

    def __str__(self):
        return str(self.v)

    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n)


def main():
    N, K = read_values()
    dp = [
        [[0 for i in range(N**2 + 1)] for ii in range(N + 1)] for iii in range(N + 1)
    ]
    dp[0][0][0] = 1

    for i in range(1, N + 1):
        for j in range(N + 1):
            for k in range(K + 1):
                if k - 2 * j < 0:
                    continue

                dp[i][j][k] = (
                    (2 * j + 1) * dp[i - 1][j][k - 2 * j]
                    + (
                        (j + 1) * (j + 1) * dp[i - 1][j + 1][k - 2 * j]
                        if j + 1 <= N
                        else 0
                    )
                    + dp[i - 1][j - 1][k - 2 * j]
                ) % mod

    print(dp[N][0][K])


if __name__ == "__main__":
    main()
Problem p02975 (Python)

Buggy submission (status: Runtime Error):
import sys
from collections import Counter

N = int(input())
C = Counter(list(map(int, input().split())))
if len(C) == 1 and C[0] == N:
    print("Yes")
elif len(C) == 2 and N % 3 == 0 and C[0] == N // 3 and N - [0] == 2 * N // 3:
    print("Yes")
elif len(C) == 3 and N % 3 == 0:
    b = 0
    for c in C.most_common():
        if c[1] != N // 3:
            print("No")
            sys.exit()
        b ^= c[0]
    if b == 0:
        print("Yes")
    else:
        print("No")
else:
    print("No")


Fixed submission:
import sys
from collections import Counter

N = int(input())
C = Counter(list(map(int, input().split())))
if len(C) == 1 and C[0] == N:
    print("Yes")
elif len(C) == 2 and N % 3 == 0 and C[0] == N // 3 and N - C[0] == 2 * N // 3:
    print("Yes")
elif len(C) == 3 and N % 3 == 0:
    b = 0
    for c in C.most_common():
        if c[1] != N // 3:
            print("No")
            sys.exit()
        b ^= c[0]
    if b == 0:
        print("Yes")
    else:
        print("No")
else:
    print("No")
Problem p02975 (Python)

Buggy submission (status: Runtime Error):
from collections import defaultdict

n = int(input())
a = tuple(map(int, input().split()))

can = True
if n % 3 == 0:
    d = defaultdict(int)
    for aa in a:
        d[aa] += 1
    m = n // 3

    t = []
    for number, cnt in d.items():
        if cnt % m != 0:
            can = False
            break
        else:
            for _ in range(cnt // m):
                t.append(number)
    # print(t)
    if any(t[(0 + i) % 3] ^ t[(1 + i) % 3] != t[(2 + i) % 3] for i in range(3)):
        can = False

else:
    if any(aa != 0 for aa in a):
        can = False

print("Yes" if can else "No")


Fixed submission:
from collections import defaultdict

n = int(input())
a = tuple(map(int, input().split()))

can = True
if n % 3 == 0:
    d = defaultdict(int)
    for aa in a:
        d[aa] += 1
    m = n // 3

    t = []
    for number, cnt in d.items():
        if cnt % m != 0:
            can = False
            break
        else:
            for _ in range(cnt // m):
                t.append(number)
    else:
        if any(t[(0 + i) % 3] ^ t[(1 + i) % 3] != t[(2 + i) % 3] for i in range(3)):
            can = False

else:
    if any(aa != 0 for aa in a):
        can = False

print("Yes" if can else "No")
Problem p02975 (C++)

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

#define LL long long
using namespace std;
#define MAXN 200005
#define pp 1000000007

// set<int> s;
unordered_map<int, int> m;

int nums[MAXN];

int main() {
  int n;
  bool flag = true;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &nums[i]);
    if (m.find(nums[i]) == m.end())
      m.insert({nums[i], 1});
    else {
      m[nums[i]]++;
    }
    // s.insert(nums[i]);
    if (nums[i] != 0)
      flag = false;
  }
  if (flag) {
    printf("Yes");
    return 0;
  }
  int ans = 0;
  if (n > 3) {
    if (n % 3 == 0) {
      if (m.size() > 3) {
        printf("No");
        return 0;
      } else {
        for (auto i : m) {
          while (i.second != 0) {
            ans ^= i.first;
            i.second -= n / 3;
          }
        }
      }
      if (ans == 0) {
        printf("Yes");
        return 0;
      } else {
        printf("No");
        return 0;
      }
    } else {
      printf("No");
      return 0;
    }
  } else {
    if ((nums[0] ^ nums[1]) == nums[2]) {
      printf("Yes");
      return 0;
    } else {
      printf("No");
      return 0;
    }
  }
  return 0;
}

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

#define LL long long
using namespace std;
#define MAXN 200005
#define pp 1000000007

// set<int> s;
unordered_map<int, int> m;

int nums[MAXN];

int main() {
  int n;
  bool flag = true;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &nums[i]);
    if (m.find(nums[i]) == m.end())
      m.insert({nums[i], 1});
    else {
      m[nums[i]]++;
    }
    // s.insert(nums[i]);
    if (nums[i] != 0)
      flag = false;
  }
  if (flag) {
    printf("Yes");
    return 0;
  }
  int ans = 0;
  if (n > 3) {
    if (n % 3 == 0) {
      if (m.size() > 3) {
        printf("No");
        return 0;
      } else {
        for (auto i : m) {
          while (i.second != 0) {
            ans ^= i.first;
            i.second -= n / 3;
            if (i.second < 0) {
              printf("No");
              return 0;
            }
          }
        }
      }
      if (ans == 0) {
        printf("Yes");
        return 0;
      } else {
        printf("No");
        return 0;
      }
    } else {
      printf("No");
      return 0;
    }
  } else {
    if ((nums[0] ^ nums[1]) == nums[2]) {
      printf("Yes");
      return 0;
    } else {
      printf("No");
      return 0;
    }
  }
  return 0;
}
Problem p02975 (C++)

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

using namespace std;

int n;
int a[300001];
int b[100001];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int k = 1;
  sort(a + 1, a + 1 + n);
  a[n + 1] = -2;
  for (int i = 1; a[i + 1] != -2 && i < 300001; i++)
    if (a[i] == a[i + 1]) {
      a[n + k] = a[i];
      a[i] = -1;
      a[n + k + 1] = -2;
      k++;
    }
  for (int i = 1, j = 1; i <= n; i++) {
    while (a[j] == -1)
      j++;
    b[i] = a[j];
    j++;
  }
  int flag = 0;
  for (int i = 1; i + 2 <= n; i++)
    if ((b[i] ^ b[i + 1]) != b[i + 2]) {
      flag = 1;
      break;
    }
  if ((b[n - 1] ^ b[n]) != b[1]) {
    flag = 1;
  }
  if (flag)
    printf("No\n");
  else
    printf("Yes\n");
  return 0;
}


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

using namespace std;

int n;
int a[300001];
int b[100001];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int s = 0;
  for (int i = 1; i <= n; i++)
    s = s ^ a[i];
  if (s)
    printf("No\n");
  else
    printf("Yes\n");
  return 0;
}
Problem p02975 (C++)

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

using namespace std;
typedef long long ll;

int main() {
  // freopen("t.txt","r",stdin);

  int n;
  cin >> n;
  int a[10002];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int t = a[1], kq;
  for (int i = 2; i <= n; i++) {
    t = t ^ a[i];
  }
  if (t == 0) {
    cout << "Yes";
    return 0;
  }
  cout << "No";
  return 0;
}

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

using namespace std;
typedef long long ll;

int main() {
  // freopen("t.txt","r",stdin);

  int n;
  cin >> n;
  int a[100002];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int t = a[1], kq;
  for (int i = 2; i <= n; i++) {
    t = t ^ a[i];
  }
  if (t == 0) {
    cout << "Yes";
    return 0;
  }
  cout << "No";
  return 0;
}
Problem p02975 (C++)

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

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

const llong inf = 1100100100100100ll;
const llong mod = 1e9 + 7;

int main() {
  llong N;
  vector<llong> a;
  map<llong, llong> m;

  cin >> N;
  a.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    m[a[i]]++;
  }

  if (m.size() == 1 && m[0] > 0) {
    cout << "Yes" << endl;
  } else if (N % 3 != 0) {
    cout << "No" << endl;
  } else {
    if (m.size() != 3) {
      return 1;
      cout << "No" << endl;
    } else {
      map<llong, llong> mm;
      vector<llong> v;
      for (auto it = m.begin(); it != m.end(); ++it) {
        v.push_back(it->first);
        mm[it->second]++;
      }
      if (mm.size() != 1) {
        cout << "No" << endl;
      } else {
        if ((v[0] ^ v[2]) == v[1] && (v[1] ^ v[0]) == v[2] &&
            (v[2] ^ v[1]) == v[0]) {
          cout << "Yes" << endl;
        } else {
          cout << "No" << endl;
        }
      }
    }
  }

  return 0;
}

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

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

const llong inf = 1100100100100100ll;
const llong mod = 1e9 + 7;

int main() {
  llong N;
  vector<llong> a;
  map<llong, llong> m;

  cin >> N;
  a.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    m[a[i]]++;
  }

  if (m.size() == 1 && m[0] > 0) {
    cout << "Yes" << endl;
  } else if (N % 3 != 0) {
    cout << "No" << endl;
  } else {
    if (m.size() != 3) {
      if (m[0] == N / 3) {
        cout << "Yes" << endl;
      } else {
        cout << "No" << endl;
      }
    } else {
      map<llong, llong> mm;
      vector<llong> v;
      for (auto it = m.begin(); it != m.end(); ++it) {
        v.push_back(it->first);
        mm[it->second]++;
      }
      if (mm.size() != 1) {
        cout << "No" << endl;
      } else {
        if ((v[0] ^ v[2]) == v[1] && (v[1] ^ v[0]) == v[2] &&
            (v[2] ^ v[1]) == v[0]) {
          cout << "Yes" << endl;
        } else {
          cout << "No" << endl;
        }
      }
    }
  }

  return 0;
}
Problem p02975 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

void fail() {
  cout << "No" << endl;
  exit(0);
}

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }

  if (mp[0] == n) {
    cout << "Yes" << endl;
    return 0;
  }
  if (n % 3 != 0)
    fail();

  if (mp.size() == 2) {
    if (mp[0] != n / 3)
      fail();
  } else {
    vector<int> vec;
    for (auto p : mp) {
      if (p.first != 0 && p.second == n / 3)
        vec.push_back(p.first);
    }
    if (vec[0] ^ vec[1] ^ vec[2] != 0)
      fail();
  }
  cout << "Yes" << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

void fail() {
  cout << "No" << endl;
  exit(0);
}

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }

  if (mp[0] == n) {
    cout << "Yes" << endl;
    return 0;
  }
  if (n % 3 != 0)
    fail();

  if (mp.size() == 2) {
    if (mp[0] != n / 3)
      fail();
  } else {
    vector<int> vec;
    for (auto p : mp) {
      if (p.first != 0 && p.second == n / 3)
        vec.push_back(p.first);
    }
    if (vec.size() != 3 || (vec[0] ^ vec[1] ^ vec[2]) != 0)
      fail();
  }
  cout << "Yes" << endl;

  return 0;
}
Problem p02975 (C++)

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

int main() {
  using namespace std;
  size_t N;
  cin >> N;
  unordered_map<unsigned long, size_t> mp;
  for (unsigned long i = 0, a; i < N; ++i) {
    cin >> a;
    ++mp[a];
  }
  if (mp.size() > 3)
    return 0 & puts("No");
  if (mp.size() == 1)
    return 0 & puts(mp[0] == N ? "Yes" : "No");
  if (mp.size() == 2)
    return 0 & puts(mp[0] * 3 == N ? "Yes" : "No");
  if (mp.size() == 3) {
    vector<unsigned long> a;
    for (const auto &i : mp) {
      if (i.second * 3 != N)
        return 0 & puts("No");
      a.push_back(i.first);
    }
    assert(0);
    puts(a[0] ^ a[1] == a[2] ? "Yes" : "No");
  }
  return 0;
}

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

int main() {
  using namespace std;
  size_t N;
  cin >> N;
  unordered_map<unsigned long, size_t> mp;
  for (unsigned long i = 0, a; i < N; ++i) {
    cin >> a;
    ++mp[a];
  }
  if (mp.size() > 3)
    return 0 & puts("No");
  if (mp.size() == 1)
    return 0 & puts(mp[0] == N ? "Yes" : "No");
  if (mp.size() == 2)
    return 0 & puts(mp[0] * 3 == N ? "Yes" : "No");
  if (mp.size() == 3) {
    vector<unsigned long> a;
    for (const auto &i : mp) {
      if (i.second * 3 != N)
        return 0 & puts("No");
      a.push_back(i.first);
    }
    puts((a[0] ^ a[1]) == a[2] ? "Yes" : "No");
  }
  return 0;
}
Problem p02975 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 100005;
const long long mod = 1.0e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ll n, i;
  cin >> n;
  ll arr[n], x = 0;
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    x = x ^ arr[i];
  }
  if (x == 0) {
    cout << "Yes" << endl;

  } else
    cout << "No" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 100005;
const long long mod = 1.0e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, i;
  cin >> n;
  ll arr[n], x = 0;
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    x = x ^ arr[i];
  }
  if (x == 0) {
    cout << "Yes" << endl;

  } else
    cout << "No" << endl;
  return 0;
}
Problem p02975 (C++)

Buggy submission (status: Time Limit Exceeded):
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

ll askx(ll a, ll b) {
  ll ans = 0;
  for (int i = 0; i < 30; i++) {
    if (b & (1 << i)) {
      if (a & (1 << i)) {
      } else {
        ans |= (1 << i);
      }
    } else {
      if (a & (1 << i)) {
        ans |= (1 << i);
      } else {
      }
    }
  }
  return ans;
}
ll n;
bool saiki(ll now, ll a, ll b, map<ll, ll> c) {
  if (now == n) {
    return true;
  }
  bool ok = false;

  ll x = askx(a, b);
  if (c[x] > 0) {
    c[x]--;
    if (saiki(now + 1, b, x, c)) {
      ok = true;
    } else {
      c[x]++;
    }
  }
  return ok;
}

int main() {
  ll a[100010];
  std::cin >> n;

  std::map<ll, ll> cou;

  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    cou[a[i]]++;
  }

  for (int i = 1; i < n; i++) {
    if (cou[askx(a[0], a[i])] > 0 && cou[askx(a[i], a[0])] > 0) {
      cou[a[0]]--;
      cou[a[i]]--;
      if (saiki(2, a[0], a[i], cou)) {
        std::cout << "Yes" << std::endl;
        return 0;
      }
      cou[a[0]]++;
      cou[a[i]]++;
    }
  }
  std::cout << "No" << std::endl;
}

Fixed submission:
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

ll askx(ll a, ll b) {
  ll ans = 0;
  for (int i = 0; i < 30; i++) {
    if (b & (1 << i)) {
      if (a & (1 << i)) {
      } else {
        ans |= (1 << i);
      }
    } else {
      if (a & (1 << i)) {
        ans |= (1 << i);
      } else {
      }
    }
  }
  return ans;
}
ll n;
bool saiki(ll now, ll a, ll b, map<ll, ll> c) {
  if (now == n) {
    return true;
  }
  bool ok = false;

  ll x = askx(a, b);
  if (c[x] > 0) {
    c[x]--;
    if (saiki(now + 1, b, x, c)) {
      ok = true;
    } else {
      c[x]++;
    }
  }
  return ok;
}

int main() {
  ll a[100010];
  std::cin >> n;

  std::map<ll, ll> cou;

  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    cou[a[i]]++;
  }

  for (int i = 0; i < 30; i++) {
    ll c1, c0;
    c1 = 0;
    c0 = 0;
    for (int j = 0; j < n; j++) {
      if (a[j] & (1 << i)) {
        c1++;
      } else {
        c0++;
      }
    }
    if (c1 == 2 * c0 || c0 == n) {
      continue;
    } else {
      std::cout << "No" << std::endl;
      return 0;
    }
  }

  for (int i = 1; i < n; i++) {
    if (cou[askx(a[0], a[i])] > 0 && cou[askx(a[i], a[0])] > 0) {
      cou[a[0]]--;
      cou[a[i]]--;
      if (saiki(2, a[0], a[i], cou)) {
        std::cout << "Yes" << std::endl;
        return 0;
      }
      cou[a[0]]++;
      cou[a[i]]++;
    }
  }
  std::cout << "No" << std::endl;
}
Problem p02975 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  map<int, int> cnt;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    cnt[A[i]] += 1;
  }

  if (cnt[0] == n) {
    cout << "Yes" << endl;
    return 0;
  }
  if (n % 3 != 0) {
    cout << "No" << endl;
    return 0;
  }
  vector<int> all;
  for (auto &p : cnt) {
    for (int i = 0; i < p.second / (n / 3); i++) {
      all.push_back(p.first);
    }
  }
  if (all.size() == 3 & (all[0] ^ all[1] ^ all[2]) == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

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

int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  map<int, int> cnt;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    cnt[A[i]] += 1;
  }

  if (cnt[0] == n) {
    cout << "Yes" << endl;
    return 0;
  }
  if (n % 3 != 0) {
    cout << "No" << endl;
    return 0;
  }
  vector<int> all;
  for (auto &p : cnt) {
    for (int i = 0; i < p.second / (n / 3); i++) {
      all.push_back(p.first);
    }
  }
  if (all.size() == 3 && (all[0] ^ all[1] ^ all[2]) == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
Problem p02975 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int No() {
  cout << "No";
  return 0;
}
int Yes() {
  cout << "Yes";
  return 0;
}
int main() {
  int a[3][2] = {}, n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    for (int j = 0; j <= 3; j++) {
      if (j == 3) {
        return No();
      }
      if (a[j][1] == 0) {
        a[j][0] = tmp;
        a[j][1]++;
        break;
      } else if (a[j][0] == tmp) {
        a[j][1]++;
        break;
      }
    }
  }
  if (a[2][1] != 0) { //
    if (a[0][1] != a[1][1])
      return No();
    if (a[1][1] != a[2][1])
      return No();
    a[0][0] = 1 / 0;
    if (a[0][0] ^ a[1][0] != a[2][0])
      return No();
    return Yes();
  } else if (a[1][1] != 0) {
    if (a[0][0] == 0) {
      if (a[0][1] * 2 != a[1][1])
        return No();
      return Yes();
    }
    if (a[1][0] == 0) {
      if (a[1][1] * 2 != a[0][1])
        return No();
      return Yes();
    }
    return No();
  } else if (a[0][1] != 0) { //
    if (a[0][0] != 0)
      return No();
    return Yes();
  }
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int No() {
  cout << "No";
  return 0;
}
int Yes() {
  cout << "Yes";
  return 0;
}
int main() {
  int a[3][2] = {}, n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    for (int j = 0; j <= 3; j++) {
      if (j == 3) {
        return No();
      }
      if (a[j][1] == 0) {
        a[j][0] = tmp;
        a[j][1]++;
        break;
      } else if (a[j][0] == tmp) {
        a[j][1]++;
        break;
      }
    }
  }
  if (a[2][1] != 0) { //
    if (a[0][1] != a[1][1])
      return No();
    if (a[1][1] != a[2][1])
      return No();
    if ((a[0][0] ^ a[1][0]) != a[2][0])
      return No();
    return Yes();
  } else if (a[1][1] != 0) {
    if (a[0][0] == 0) {
      if (a[0][1] * 2 != a[1][1])
        return No();
      return Yes();
    }
    if (a[1][0] == 0) {
      if (a[1][1] * 2 != a[0][1])
        return No();
      return Yes();
    }
    return No();
  } else if (a[0][1] != 0) { //
    if (a[0][0] != 0)
      return No();
    return Yes();
  }
}
Problem p02975 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int t, n, a[N], b[N], num[N], tot, c;
map<int, int> cnt, tmp;
void solve() {
  if (tot > 3) {
    puts("No");
    return;
  } else if (tot == 1) {
    if (num[0] == 0) {
      puts("Yes");
      return;
    } else {
      puts("No");
      return;
    }
  } else {
    bool ok = 1;
    do {
      ok = 1;
      tmp = cnt;
      b[1] = num[0];
      tmp[num[0]]--;
      b[2] = num[1];
      tmp[num[1]]--;
      for (int i = 3; i <= n && ok; ++i) {
        bool upd = 0;
        for (int j = 0; j < 3 && !upd; ++j) {
          if (tmp[num[j]] > 0 && (num[j] ^ b[i - 2]) == b[i - 1]) {
            b[i] = num[j];
            tmp[num[j]]--;
            upd = 1;
          }
        }
        if (!upd)
          ok = 0;
      }
      if (ok && (b[n] ^ b[n - 1]) != b[1])
        ok = 0;
      if (ok)
        break;
    } while (num, num + tot);
    if (ok)
      puts("Yes");
    else
      puts("No");
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    if (!cnt.count(a[i]))
      num[tot++] = a[i];
    cnt[a[i]]++;
  }
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int t, n, a[N], b[N], num[N], tot, c;
map<int, int> cnt, tmp;
void solve() {
  if (tot > 3) {
    puts("No");
    return;
  } else if (tot == 1) {
    if (num[0] == 0) {
      puts("Yes");
      return;
    } else {
      puts("No");
      return;
    }
  } else {
    bool ok = 1;
    do {
      ok = 1;
      tmp = cnt;
      b[1] = num[0];
      tmp[num[0]]--;
      b[2] = num[1];
      tmp[num[1]]--;
      for (int i = 3; i <= n && ok; ++i) {
        bool upd = 0;
        for (int j = 0; j < 3 && !upd; ++j) {
          if (tmp[num[j]] > 0 && (num[j] ^ b[i - 2]) == b[i - 1]) {
            b[i] = num[j];
            tmp[num[j]]--;
            upd = 1;
          }
        }
        if (!upd)
          ok = 0;
      }
      if (ok && (b[n] ^ b[n - 1]) != b[1])
        ok = 0;
      if (ok)
        break;
    } while (next_permutation(num, num + tot));
    if (ok)
      puts("Yes");
    else
      puts("No");
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    if (!cnt.count(a[i]))
      num[tot++] = a[i];
    cnt[a[i]]++;
  }
  solve();
  return 0;
}
Problem p02975 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
#define N 80005
#define ll long long
int t, n;
ll a[N];
int main() {
  // freopen("circle.in","r",stdin);
  // freopen("circle.out","w",stdout);
  // scanf("%d",&t);
  // while(t--)
  //{
  int k = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), k ^= a[i];
  if (k != 0) {
    printf("No\n");
    // continue;
  }
  // sort(a+1,a+n+1);
  else {
    printf("Yes\n");
  }
  //}
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
#define N 100005
#define ll long long
int t, n;
ll a[N];
int main() {
  // freopen("circle.in","r",stdin);
  // freopen("circle.out","w",stdout);
  // scanf("%d",&t);
  // while(t--)
  //{
  int k = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), k ^= a[i];
  if (k != 0) {
    printf("No\n");
    // continue;
  }
  // sort(a+1,a+n+1);
  else {
    printf("Yes\n");
  }
  //}
}
Problem p02975 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

bool solve(int N, vector<int> &a) {
  map<int, int> s;
  for (int i = 0; i < N; i++) {
    s[a[i]]++;
  }
  pair<int, int> v[3];
  int j = 0;
  for (auto it = s.begin(); it != s.end(); ++it, j++) {
    v[j] = *it;
  }
  if (s.size() == 3) {
    if (v[0].second == v[1].second && v[1].second == v[2].second) {
      if ((v[0].first ^ v[1].first ^ v[2].first) == 0) {
        return true;
      }
    }
    return false;
  } else if (s.size() == 2) {
    if (v[0].second < v[1].second) {
      swap(v[0], v[1]);
    }
    if (v[0].second == v[1].second * 2) {
      if ((v[0].first ^ v[0].first ^ v[1].first) == 0) {
        return true;
      }
    }
    return false;
  } else {
    if (v[0].first == 0) {
      return true;
    }
    return false;
  }
}

int main() {
  int N;
  scanf("%d", &N);
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }
  printf("%s\n", solve(N, a) ? "Yes" : "No");
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

bool solve(int N, vector<int> &a) {
  map<int, int> s;
  for (int i = 0; i < N; i++) {
    s[a[i]]++;
  }
  if (s.size() > 3) {
    return false;
  }
  pair<int, int> v[3];
  int j = 0;
  for (auto it = s.begin(); it != s.end(); ++it, j++) {
    v[j] = *it;
  }
  if (s.size() == 3) {
    if (v[0].second == v[1].second && v[1].second == v[2].second) {
      if ((v[0].first ^ v[1].first ^ v[2].first) == 0) {
        return true;
      }
    }
    return false;
  } else if (s.size() == 2) {
    if (v[0].second < v[1].second) {
      swap(v[0], v[1]);
    }
    if (v[0].second == v[1].second * 2) {
      if ((v[0].first ^ v[0].first ^ v[1].first) == 0) {
        return true;
      }
    }
    return false;
  } else {
    if (v[0].first == 0) {
      return true;
    }
    return false;
  }
}

int main() {
  int N;
  scanf("%d", &N);
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }
  printf("%s\n", solve(N, a) ? "Yes" : "No");
}
Problem p02975 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> num;
  map<int, int> nums;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    num.push_back(t);
    if (nums.find(t) != nums.end()) {
      nums[t]++;
    } else {
      nums[t] = 1;
    }
  }
  sort(num.begin(), num.end());
  num.erase(unique(num.begin(), num.end()), num.end());
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (i == j)
        continue;
      int a = num[i];
      int b = num[j];
      map<int, int> nums2 = nums;
      nums2[a]--;
      nums2[b]--;
      int first = a;
      int prev = a;
      int cur = b;
      for (int k = 2; k < n + 1; k++) {
        int next = prev ^ cur;
        if (k != n) {
          if (nums2.find(next) == nums2.end() || nums2[next] == 0) {
            break;
          }
          prev = cur;
          cur = next;
          nums2[next]--;
        } else {
          if (next == first) {
            cout << "Yes" << endl;
            return 0;
          }
        }
      }
    }
  }
  cout << "No" << endl;
  return 0;
}


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

int main() {
  int n;
  cin >> n;
  vector<int> num;
  map<int, int> nums;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    num.push_back(t);
    if (nums.find(t) != nums.end()) {
      nums[t]++;
    } else {
      nums[t] = 1;
    }
  }
  int a = 0;
  for (int i = 0; i < n; i++) {
    a ^= num[i];
  }
  if (a != 0) {
    cout << "No" << endl;
    return 0;
  }
  sort(num.begin(), num.end());
  num.erase(unique(num.begin(), num.end()), num.end());
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (i == j)
        continue;
      int a = num[i];
      int b = num[j];
      map<int, int> nums2 = nums;
      nums2[a]--;
      nums2[b]--;
      int first = a;
      int prev = a;
      int cur = b;
      for (int k = 2; k < n + 1; k++) {
        int next = prev ^ cur;
        if (k != n) {
          if (nums2.find(next) == nums2.end() || nums2[next] == 0) {
            break;
          }
          prev = cur;
          cur = next;
          nums2[next]--;
        } else {
          if (next == first) {
            cout << "Yes" << endl;
            return 0;
          }
        }
      }
    }
  }
  cout << "No" << endl;
  return 0;
}
Problem p02975 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n;
vector<int> a;

vector<int> calc(int x, int y) {
  vector<int> ret = {x, y};
  for (int i = 0; i < n - 2; i++) {
    ret.push_back(ret[(int)ret.size() - 2] ^ ret.back());
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  set<int> s(a.begin(), a.end());
  if (s.size() > 3)
    finish("No");
  for (auto &i : a) {
    for (auto &j : a) {
      vector<int> c = calc(i, j);
      map<int, int> cnt;
      for (auto &i : a)
        cnt[i]++;
      bool ok = 1;
      for (auto &i : c) {
        if (cnt[i] == 0)
          ok = 0;
        cnt[i]--;
      }
      if (ok)
        finish("Yes");
    }
  }
  cout << "No" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n;
vector<int> a;

vector<int> calc(int x, int y) {
  vector<int> ret = {x, y};
  for (int i = 0; i < n - 2; i++) {
    ret.push_back(ret[(int)ret.size() - 2] ^ ret.back());
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  set<int> s(a.begin(), a.end());
  if (s.size() > 3)
    finish("No");
  for (auto &i : s) {
    for (auto &j : s) {
      vector<int> c = calc(i, j);
      map<int, int> cnt;
      for (auto &i : a)
        cnt[i]++;
      bool ok = 1;
      for (auto &i : c) {
        if (cnt[i] == 0)
          ok = 0;
        cnt[i]--;
      }
      if (ok)
        finish("Yes");
    }
  }
  cout << "No" << endl;
}