alexjercan/bugnet huggingface dataset:
Problem p02625 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n > 0) {
    if (n & 1) {
      ans = ans * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return ans;
}

vec tfact(1e7 + 1);
void fact(ll n) {
  for (ll i = 0; i <= n; i++) {
    if (i == 0) {
      tfact[i] = 1;
    } else {
      tfact[i] = tfact[i - 1] * i % mod;
    }
  }
}

ll comb(ll n, ll k) {
  return tfact.at(n) * mpow(tfact.at(k), mod - 2) % mod *
         mpow(tfact.at(n - k), mod - 2) % mod;
}

int main() {
  ll n, m;
  cin >> n >> m;
  fact(m);
  ll tans = tfact[n] * comb(m, n) % mod;
  ll ans = tans * tans % mod;
  for (ll i = 1; i <= n; i++) {
    ll tmp =
        tans * tfact[n - i] % mod * comb(m - i, n - i) % mod * comb(n, i) % mod;
    if (i % 2) {
      ans = (ans - tmp + mod) % mod;
    } else {
      ans = (ans + tmp) % mod;
    }
  }
  cout << ans << endl;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n > 0) {
    if (n & 1) {
      ans = ans * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return ans;
}

vec tfact(1e7 + 1);
void fact(ll n) {
  for (ll i = 0; i <= n; i++) {
    if (i == 0) {
      tfact[i] = 1;
    } else {
      tfact[i] = tfact[i - 1] * i % mod;
    }
  }
}

ll comb(ll n, ll k) {
  return tfact.at(n) * mpow(tfact.at(k), mod - 2) % mod *
         mpow(tfact.at(n - k), mod - 2) % mod;
}

vec tpf(1e7 + 1);
ll pf(ll n) {
  if (n < 1)
    return 1;
  if (n == 1)
    return 0;
  if (tpf[n])
    return tpf[n];
  ll res = (n - 1) * (pf(n - 1) + pf(n - 2)) % mod;
  return tpf[n] = res;
}

int main() {
  ll n, m;
  cin >> n >> m;
  fact(m);
  ll tans = tfact[n] * comb(m, n) % mod;
  ll ans = 0;
  for (ll i = n; n - i <= m - n; i--) {
    ans += tans * pf(i) % mod;
    ll nk = n - i;
    if (nk) {
      ans += tans * comb(n, nk) % mod;
    }
    ans %= mod;
  }
  cout << ans << endl;
}
Problem p02625 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)

using namespace std;

typedef long long ll;

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

ll MOD = 1e9 + 7;

const ll max_n = 505050;
ll fact[max_n];
bool fact_inited = false;

ll powmod(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 1)
    return a * powmod(a, b - 1) % MOD;

  ll d = powmod(a, b / 2) % MOD;
  return d * d % MOD;
}

void init_fact() {
  fact[0] = 1;
  fact[1] = 1;

  for (ll i = 2; i < max_n; i++) {
    fact[i] = fact[i - 1] * i % MOD;
  }

  fact_inited = true;
}

ll comb(ll n, ll k) {
  if (!fact_inited)
    init_fact();

  ll ret = fact[n];
  ret *= powmod(fact[k], MOD - 2);
  ret %= MOD;
  ret *= powmod(fact[n - k], MOD - 2);
  ret %= MOD;
  return ret;

  // X^(-1) = X^(p-2) (mod p) (Fermat's little theorem)
}

ll perm(ll n, ll k) {
  if (!fact_inited)
    init_fact();

  ll ret = fact[n];
  ret *= powmod(fact[n - k], MOD - 2);
  ret %= MOD;
  return ret;
}

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

  ll n, m;
  cin >> n >> m;

  ll ans = perm(m, n) * perm(m, n);
  ans %= MOD;

  ll s = -1;
  for (int x = 1; x <= n; x++) {
    ll t = perm(m, n) * comb(n, x) % MOD * perm(m - x, n - x) % MOD;
    t %= MOD;
    t *= s;
    ans += MOD + t;
    ans %= MOD;

    s *= -1;
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)

using namespace std;

typedef long long ll;

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

ll MOD = 1e9 + 7;

const ll max_n = 505050;
ll fact[max_n];
bool fact_inited = false;

ll powmod(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 1)
    return a * powmod(a, b - 1) % MOD;

  ll d = powmod(a, b / 2) % MOD;
  return d * d % MOD;
}

ll init_fact() {
  fact[0] = 1;
  fact[1] = 1;

  for (ll i = 2; i < max_n; i++) {
    fact[i] = fact[i - 1] * i % MOD;
  }

  fact_inited = true;
}

ll comb(ll n, ll k) {
  if (!fact_inited)
    init_fact();

  ll ret = fact[n];
  ret *= powmod(fact[k], MOD - 2);
  ret %= MOD;
  ret *= powmod(fact[n - k], MOD - 2);
  ret %= MOD;
  return ret;

  // X^(-1) = X^(p-2) (mod p) (Fermat's little theorem)
}

ll perm(ll n, ll k) {
  if (!fact_inited)
    init_fact();

  ll ret = fact[n];
  ret *= powmod(fact[n - k], MOD - 2);
  ret %= MOD;
  return ret;
}

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

  ll n, m;
  cin >> n >> m;

  ll ans = perm(m, n) * perm(m, n);
  ans %= MOD;

  ll s = -1;
  for (int x = 1; x <= n; x++) {
    ll t = perm(m, n) * comb(n, x) % MOD * perm(m - x, n - x) % MOD;
    t %= MOD;
    t *= s;
    ans += MOD + t;
    ans %= MOD;

    s *= -1;
  }

  cout << ans << endl;
}
Problem p02625 (C++) — buggy submission, verdict: Runtime Error

#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 500005, mod = 1e9 + 7;
int n, m, fact[N], infact[N];
int qmi(int a, int k, int p) {
  int res = 1;
  while (k) {
    if (k & 1)
      res = 1ll * res * a % p;
    k >>= 1;
    a = 1ll * a * a % p;
  }
  return res;
}
void init() {
  fact[0] = infact[0] = 1;
  for (int i = 1; i <= m; i++) {
    fact[i] = 1ll * fact[i - 1] * i % mod;
    infact[i] = qmi(fact[i], mod - 2, mod);
  }
}
int main() {
  IO;
  cin >> n >> m;
  init();
  ll res = 0;
  for (int i = 0; i <= n; i++) {
    if (i & 1)
      res = (res - 1ll * fact[n] * infact[i] % mod * infact[n - i] % mod *
                       fact[m - i] % mod * infact[m - n] % mod) %
            mod;
    else
      res = (res + 1ll * fact[n] * infact[i] % mod * infact[n - i] % mod *
                       fact[m - i] % mod * infact[m - n] % mod) %
            mod;
  }
  for (int i = m - n + 1; i <= m; i++)
    res = res * i % mod;
  res %= mod;
  cout << (res + mod) % mod << endl;
}

Error: 0

Fixed submission:

#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 500010, mod = 1e9 + 7;
int n, m, fact[N], infact[N];
int qmi(int a, int k, int p) {
  int res = 1;
  while (k) {
    if (k & 1)
      res = 1ll * res * a % p;
    k >>= 1;
    a = 1ll * a * a % p;
  }
  return res;
}
void init() {
  fact[0] = infact[0] = 1;
  for (int i = 1; i <= N; i++) {
    fact[i] = 1ll * fact[i - 1] * i % mod;
    infact[i] = 1ll * infact[i - 1] * qmi(i, mod - 2, mod) % mod;
  }
}

int main() {
  IO;
  cin >> n >> m;
  init();
  ll res = 0;
  for (int i = 0; i <= n; i++) {
    if (i & 1)
      res = (res - 1ll * fact[n] * infact[i] % mod * infact[n - i] % mod *
                       fact[m - i] % mod * infact[m - n] % mod) %
            mod;
    else
      res = (res + 1ll * fact[n] * infact[i] % mod * infact[n - i] % mod *
                       fact[m - i] % mod * infact[m - n] % mod) %
            mod;
  }
  for (int i = m - n + 1; i <= m; i++)
    res = res * i % mod;
  cout << res << endl;
}
Problem p02625 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

const int MOD = 1e9 + 7;
const long long MAX = 5 * 1e5 + 1;
long long fact[MAX], fact_inv[MAX], inv[MAX];

void init_nCk() {
  fact[0] = fact[1] = 1;
  fact_inv[0] = fact_inv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD;
  }
}

long long nCk(int n, int k) {
  if (n < k)
    return 0;
  else if (n < 0 || k < 0)
    return 0;
  else
    return fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD;
}
long long nPk(int n, int k) {
  if (n < k)
    return 0;
  else if (n < 0 || k < 0)
    return 0;
  else
    return fact[n] * (fact_inv[n - k] % MOD) % MOD;
}

int main() {
  long long temp, ans, M, N;
  temp = 0;
  std::cin >> N >> M;
  init_nCk();
  for (int i = 0; i <= N; i++) {
    temp += (1 - i % 2 * 2) *
            ((nPk(M, i) * (nPk(M - i, N - i)) % MOD) *
             ((nPk(M - i, N - i)) * nCk(N, i) % MOD)) %
            MOD;
    temp = (temp + MOD) % MOD;
  }
  ans = (nPk(M, N) * nPk(M, N) % MOD - temp % MOD) % MOD;
  std::cout << temp;
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <vector>

const int MOD = 1e9 + 7;

std::vector<long long> fact, fact_inv, inv;

void init_nCk(int SIZE) {
  fact.resize(SIZE + 5);
  fact_inv.resize(SIZE + 5);
  inv.resize(SIZE + 5);
  fact[0] = fact[1] = 1;
  fact_inv[0] = fact_inv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < SIZE + 5; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD;
  }
}

long long nCk(int n, int k) {
  if (n < k)
    return 0;
  else if (n < 0 || k < 0 || k > n)
    return 0;
  else
    return fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD;
}
long long nPk(int n, int k) {
  if (n < k)
    return 0;
  else if (n < 0 || k < 0 || k > n)
    return 0;
  else
    return fact[n] * (fact_inv[n - k] % MOD) % MOD;
}

int main() {
  int M, N;
  long long temp, ans;
  temp = 0;
  std::cin >> N >> M;
  init_nCk(N);
  for (int i = 0; i <= N; i++) {
    temp += (1 - i % 2 * 2) *
            ((nPk(M, i) * (nPk(M - i, N - i)) % MOD) *
             ((nPk(M - i, N - i)) * nCk(N, i) % MOD)) %
            MOD;
    temp = (temp + MOD) % MOD;
  }
  ans = (nPk(M, N) * nPk(M, N) % MOD - temp % MOD) % MOD;
  std::cout << (ans + MOD) % MOD;
  return 0;
}
Problem p02625 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int N = 5e5 + 10;
const int mod = 1e9 + 7;

ll fac[N];

ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}

void init() {
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = fac[i - 1] * i % mod;
}

ll inv(ll a) { return binpow(a, mod - 2); }

ll A(ll n, ll m) { return fac[n] * inv(fac[n - m]) % mod; }

ll C(ll n, ll m) { return fac[n] * inv(fac[m]) % mod * inv(fac[n - m]) % mod; }

int main() {
  init();
  int n, m;
  cin >> n >> m;
  ll sum = 0;
  for (int i = 0; i <= n; i++) {
    sum += binpow(-1, i) * C(n, i) * A(m - i, n - i) % mod;
    sum = (sum + mod) % mod;
  }
  cout << A(m, n) * sum % mod << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define mp make_pair
#define ld long double
typedef pair<int, int> pii;
typedef priority_queue<int, vector<int>, greater<int>> small_heap;
typedef priority_queue<int> big_heap;
const int N = 5e5 + 10;
int T;
const ll mod = 1e9 + 7;

ll ksc(ll a, ll b, ll c) {
  ll ans = 0;
  while (b) {
    if (b & 1)
      ans = (ans + a) % c;
    a = a * 2 % c;
    b >>= 1;
  }
  return ans % c;
}

ll ksm(ll a, ll b, ll c) {
  ll ans = 1 % c;
  a %= c;
  while (b) {
    if (b & 1)
      ans = ksc(ans, a, c);
    a = ksc(a, a, c);
    b >>= 1;
  }
  return ans % c;
}

struct zhtools {
  ll fac[N] = {1};

  void init() {
    for (int i = 1; i < N; i++)
      fac[i] = fac[i - 1] * i % mod;
  }

  ll inv(ll a) { return ksm(a, mod - 2, mod); }

  ll A(ll n, ll m) { return fac[n] * inv(fac[n - m]) % mod; }

  ll C(ll n, ll m) {
    return fac[n] * inv(fac[m]) % mod * inv(fac[n - m]) % mod;
  }
} zh;

int main() {
  zh.init();
  int n, m;
  scanf("%d%d", &n, &m);
  ll sum = 0;
  ll flag = 1ll;
  for (int i = 0; i <= n; i++) {
    sum += flag * zh.C(n, i) * zh.A(m - i, n - i) % mod;
    sum %= mod;
    flag = -flag;
  }
  printf("%lld\\n", zh.A(m, n) * sum % mod);
  return 0;
}
Problem p02625 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
/* run this program using the console pauser or add your own getch,
 * system("pause") or input loop */
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define ll long long
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
const ll mod = 1e9 + 7;
ll ad(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll jc[720005];
ll ap(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll ksm(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b % 2 == 1)
      res = ap(res, a);
    a = ap(a, a);
    b /= 2;
  }
  return res;
}
ll C(ll a, ll b) {
  return ap(jc[a], ap(ksm(jc[b], mod - 2), ksm(jc[a - b], mod - 2)));
}
ll f[720005];
ll g[720005];
int main(int argc, char **argv) {
  ll n, m;
  sl(n);
  sl(m);
  jc[0] = 1;
  rep(i, 1, m) { jc[i] = ap(jc[i - 1], i); }
  ll ans = 0;
  rep(i, 0, n) // 最少有i个相等
  {
    g[i] = ap(ap(ap(C(n, i), C(m, i)), ap(C(m - i, (n - i)), C(m - i, n - i))),
              ap(jc[i], ap(jc[n - i], jc[n - i])));
    //	cout<<g[i]<<" "<<ap(C(m,2*(n-i)),(jc[2*(n-i)]))<<endl;
    ans = ad(ans, ((i) % 2 == 0) ? g[i] : mod - g[i]);
  }

  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
/* run this program using the console pauser or add your own getch,
 * system("pause") or input loop */
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define ll long long
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
const ll mod = 1e9 + 7;
ll ad(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll jc[720005];
ll ap(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll ksm(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b % 2 == 1)
      res = ap(res, a);
    a = ap(a, a);
    b /= 2;
  }
  return res;
}
ll C(ll a, ll b) {
  return ap(jc[a], ap(ksm(jc[b], mod - 2), ksm(jc[a - b], mod - 2)));
}
ll f[720005];
ll g[720005];
int main(int argc, char **argv) {
  ll n, m;
  sl(n);
  sl(m);
  jc[0] = 1;
  rep(i, 1, m + m) { jc[i] = ap(jc[i - 1], i); }
  ll ans = 0;
  rep(i, 0, n) // 最少有i个相等
  {
    g[i] = ap(ap(ap(C(n, i), C(m, i)), ap(C(m - i, (n - i)), C(m - i, n - i))),
              ap(jc[i], ap(jc[n - i], jc[n - i])));
    //	cout<<g[i]<<" "<<ap(C(m,2*(n-i)),(jc[2*(n-i)]))<<endl;
    ans = ad(ans, ((i) % 2 == 0) ? g[i] : mod - g[i]);
  }

  cout << ans;
  return 0;
}
Problem p02625 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long

const int mod = 1e9 + 7, N = 2e6 + 9;
int fac[N], ifac[N];

int pw(int a, int t = mod - 2) {
  int x = 1;
  while (t > 0) {
    if (t & 1)
      x = x * a % mod;
    a = a * a % mod, t >>= 1;
  }
  return x;
}
int C(int n, int r) {
  if (r < 0 or r > n)
    return 0;
  return fac[n] * ifac[r] % mod * ifac[n - r] % mod;
}
int P(int n, int r) {
  if (r < 0 or r > n or n < 0)
    return 0;
  return fac[n] * ifac[n - r] % mod;
}
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  fac[0] = ifac[0] = 1;
  for (int i = 1; i < N; i++) {
    fac[i] = i * fac[i - 1] % mod;
    ifac[i] = pw(fac[i]);
  }
  int n, m;
  cin >> n >> m;
  int sum = 0, s = 1;
  for (int i = 0; i <= n; i++) {
    sum = (sum + s * P(m - i, n - i) * C(n, i) % mod + mod) % mod;
    s *= -1;
  }
  cout << sum * P(m, n) % mod;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long

const int mod = 1e9 + 7, N = 4e5 + 9;
int fac[N], ifac[N];

int pw(int a, int t = mod - 2) {
  int x = 1;
  while (t > 0) {
    if (t & 1)
      x = x * a % mod;
    a = a * a % mod, t >>= 1;
  }
  return x;
}
int C(int n, int r) {
  if (r < 0 or r > n)
    return 0;
  return fac[n] * ifac[r] % mod * ifac[n - r] % mod;
}
int P(int n, int r) {
  if (r < 0 or r > n)
    return 0;
  return fac[n] * ifac[n - r] % mod;
}
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  fac[0] = ifac[0] = 1;
  for (int i = 1; i < N; i++) {
    fac[i] = i * fac[i - 1] % mod;
    ifac[i] = pw(fac[i]);
  }
  int n, m;
  cin >> n >> m;
  int sum = 0, s = 1;
  for (int i = 0; i <= n; i++) {
    sum = (sum + s * P(m - i, n - i) * C(n, i) % mod + mod) % mod;
    s *= -1;
  }
  cout << sum * P(m, n) % mod;
}
Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
typedef long long LL;
LL a[100010];
int main() {
  int n;
  cin >> n;
  LL X = 0, S = a[1] + a[2];
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (i <= 2)
      S = S + a[i];
    else
      X = X ^ a[i];
  }
  if ((a[1] + a[2] - X) % 2 || (a[1] + a[2] - X) < 0) {
    puts("-1");
    return 0;
  }
  LL D = (a[1] + a[2] - X) / 2;
  if (D > a[1]) {
    puts("-1");
    return 0;
  }
  LL s1 = D, s2 = 0;
  for (int i = 45; i >= 0; --i) {
    int xi = (X >> i) & 1ll, di = (D >> i) & 1ll;
    if (xi == 0 && di == 0) {
      continue;
    } else if (xi == 1 && di == 0) {
      if (s1 + (1ll << i) < a[1]) {
        s1 = s1 + (1ll << i);
      } else
        s2 += (1ll << i);
    } else if (xi == 0 && di == 1) {
      continue;
    } else {
      puts("-1");
      return 0;
    }
  }
  if (s1 == 0)
    puts("-1");
  else
    cout << a[1] - s1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef long long LL;
LL sum, tXor, ans1, ans2, ans = 1e18;
LL a[310];
void dfs(int u, LL s1, LL s2, LL Xor) {
  if (ans == 1)
    return;
  if (s1 + s2 > sum)
    return;
  if (s1 > a[1])
    return;
  if (s2 + sum - s1 < a[2])
    return;
  if (s2 == sum)
    return;
  if (s1 + s2 == sum && Xor)
    return;
  if (s1 + sum - s2 <= a[1] - ans)
    return;
  if (s2 - a[2] >= ans ||
      abs(s1 % (1ll < (u - 1)) - a[1] % (1ll < (u - 1))) >= ans)
    return;
  if (u && (s1 + s2) % (1ll << (u - 1)) != (sum % (1ll << (u - 1))))
    return;
  if (Xor == 0 && s1 + s2 == sum && s1 <= a[1] && a[2] <= s2 && s1) {
    ans = min(ans, a[1] - s1);
    return;
  }
  if (u == 45)
    return;
  if (Xor & 1) {
    dfs(u + 1, s1 + (1ll << u), s2, Xor / 2);
    dfs(u + 1, s1, s2 + (1ll << u), Xor / 2);
  } else {
    dfs(u + 1, s1 + (1ll << u), s2 + (1ll << u), Xor / 2);
    dfs(u + 1, s1, s2, Xor / 2);
  }
}
int main() {
  int n;
  LL k = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    k = k ^ a[i];
  }
  if (k == 0) {
    cout << "0\\n";
    return 0;
  }
  sum = a[1] + a[2];
  ans1 = -1, ans2 = -1;
  tXor = (k ^ a[1] ^ a[2]);
  dfs(0, 0, 0, tXor);
  if (ans != 1e18) {
    cout << ans << endl;
  } else
    cout << "-1" << endl;
  return 0;
}
Problem p02626 (C++) — buggy submission, verdict: Runtime Error

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

long n;
long a, b, c;
long sub() {
  long x = a + b;
  if (x < c)
    return -1;
  if (x % 2 != c % 2)
    return -1;
  long cap = (x - c) / 2;
  if (cap & c)
    return -1;
  long rem = a - cap;
  if (rem < 0)
    return -1;
  long ans = cap;
  for (int k = 50; k >= 0; --k) {
    if ((1ll << k) > rem)
      continue;
    if (c & (1ll << k)) {
      ans += 1ll << k;
      rem -= 1ll << k;
    }
  }
  cerr << ans << endl;
  return ans;
}

int main() {
  cin >> n;
  cin >> a >> b;
  for (int i = 2; i < n; ++i) {
    long p;
    cin >> p;
    c ^= p;
  }

  long ans = sub();
  if (ans <= 0) {
    cout << -1 << endl;
  } else {
    cout << a - ans << endl;
  }
}

Error: 0

Fixed submission:

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

long n;
long a, b, c;
long sub() {
  long x = a + b;
  if (x < c)
    return -1;
  if (x % 2 != c % 2)
    return -1;
  long cap = (x - c) / 2;
  if (cap & c)
    return -1;
  long rem = a - cap;
  long ans = cap;
  for (int k = 50; k >= 0; --k) {
    if ((1ll << k) > rem)
      continue;
    if (c & (1ll << k)) {
      ans += 1ll << k;
      rem -= 1ll << k;
    }
  }
  assert(rem >= 0);
  return ans;
}

int main() {
  cin >> n;
  cin >> a >> b;
  for (int i = 2; i < n; ++i) {
    long p;
    cin >> p;
    c ^= p;
  }

  long ans = sub();
  if (ans <= 0) {
    cout << -1 << endl;
  } else {
    cout << a - ans << endl;
  }
}
Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
#define rep(j, n) for (int j = 0; j < (n); j++)

using ll = long long;
using P = pair<int, int>;

int main(int, char **) {
  int n;
  cin >> n;

  ll a1, a2, b = 0, tmp;
  cin >> a1 >> a2;
  rep(i, n - 2) {
    cin >> tmp;
    b ^= tmp;
  }

  ll msk = 1, mskb = 0;
  ll ans = 0;
  for (; b ^ a1 ^ a2; msk <<= 1) {
    if ((b & msk) != ((a1 ^ a2) & msk)) {
      mskb = msk >> 1;
      if (!mskb || a1 <= mskb || (a1 & mskb) != (a2 & mskb)) {
        cout << -1 << endl;
        return 0;
      }
      a1 -= mskb;
      a2 += mskb;
      ans += mskb;
    }
  }
  for (msk = mskb; msk; msk >>= 1) {
    if (!(a1 & msk) && (a2 & msk))
      ans -= msk;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define rep(j, n) for (int j = 0; j < (n); j++)

using ll = long long;
using P = pair<int, int>;

int main(int, char **) {
  int n;
  cin >> n;

  ll a1, a2, b = 0, tmp;
  cin >> a1 >> a2;
  rep(i, n - 2) {
    cin >> tmp;
    b ^= tmp;
  }

  ll msk = 1, mskb = 0;
  ll ans = 0;
  for (; b ^ a1 ^ a2; msk <<= 1) {
    if ((b & msk) != ((a1 ^ a2) & msk)) {
      if (!mskb || a1 <= mskb) {
        cout << -1 << endl;
        return 0;
      }
      a1 -= mskb;
      a2 += mskb;
      ans += mskb;
    }
    if ((a1 & msk) == (a2 & msk))
      mskb = msk;
  }
  for (msk = mskb; msk; msk >>= 1) {
    if (!(a1 & msk) && (a2 & msk))
      ans -= msk;
  }
  cout << ans << endl;

  return 0;
}