Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (300006)
int main() {
  FAST ll n, x, y, val = 0;
  cin >> n >> x >> y;
  n -= 2;
  FOR(i, 1, n) {
    ll a;
    cin >> a;
    val ^= a;
  }
  ll sum = x + y;
  sum -= val;
  if (sum < 0 || sum % 2) {
    cout << "-1\\n";
    return 0;
  }
  // can for now
  ll res = 0;
  FOR(i, 1, 40) {
    if (1ll << i & sum) {
      if (1ll << (i - 1) & val) {
        cout << "-1\\n";
        return 0;
      } else
        res += 1ll << (i - 1);
    }
  }
  if (res > x) {
    cout << "-1\\n";
    return 0;
  }
  DEC(i, 39, 0) {
    if ((1ll << i & val) && res + (1ll << i) <= x)
      res += 1ll << i;
  }
  if (res == 0) {
    cout << "-1\\n";
    return 0;
  }
  cerr << res << ' ' << x << '\\n';
  cout << x - res << '\\n';
}


Error: TLE

Fixed submission:

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (300006)
int main() {
  FAST ll n;
  cin >> n;
  n -= 2;
  vector<ll> A(n);
  ll x, y;
  cin >> x >> y;
  FOR(i, 0, n - 1) cin >> A[i];
  ll val = 0;
  for (auto i : A)
    val ^= i;
  ll moves = 0;
  FOR(i, 0, 2e8) {
    if ((val ^ x ^ y) == 0) {
      cout << moves << '\\n';
      return 0;
    }
    if (i > ll(2e7)) {
      ll bruh = rand(0, x - 1);
      x -= bruh, y += bruh;
      moves += bruh;
    } else {
      --x, ++y;
      ++moves;
    }
    if (x < 1 || y < 1)
      break;
  }
  cout << "-1\\n";
}