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

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define loop(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dump(a) cerr << #a << " = " << (a) << "(L:" << __LINE__ << ")" << endl

const double PI = acos(-1.0);

bool bit(long long n, int k) { return (n >> k) & 1LL; }

long long a, b, c;
long long dfs(int i, long long x) {
  long long m = 1LL << 62;
  if (a <= x)
    return m;
  if (i == 41) {
    if ((a - x) ^ (b + x) == c, i) {
      return x;
    } else {
      return m;
    }
  }

  auto y = x;
  if (bit((a - x) ^ (b + x), i) == bit(c, i)) {
    m = min(m, dfs(i + 1, x));
  }
  y = x | (1LL << i);
  if (bit((a - y) ^ (b + y), i) == bit(c, i)) {
    m = min(m, dfs(i + 1, y));
  }
  return m;
}

void solve(long long N, std::vector<long long> A) {
  a = A[0];
  b = A[1];
  c = 0;
  loop(i, 2, N) c ^= A[i];
  // t == 0 となる x を見つけたい. 最小のもの.

  long long x = dfs(0, 0);

  if (x < a && (a - x) ^ (b + x) == c) {
    cout << x << endl;
  } else {
    cout << -1 << endl;
  }
  return;

  for (long long k = 0; k < A[0];) {
    auto t = (A[0] - k) ^ (A[1] + k) ^ x;
    if (t == 0) {
      cout << k << endl;
      return;
    }

    int s = 0;
    while (((t >> s) & 1) == 0)
      s++;
    k += 1 << s;
  }
  cout << -1 << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define loop(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dump(a) cerr << #a << " = " << (a) << "(L:" << __LINE__ << ")" << endl

const double PI = acos(-1.0);

bool bit(long long n, int k) { return (n >> k) & 1LL; }

long long dfs(int i, long long a, long long b, long long c, long long x) {
  long long m = 1LL << 62;
  if (a <= x)
    return m;
  if (i == 63) {
    if ((a - x) ^ (b + x) == c, i) {
      return x;
    } else {
      return m;
    }
  }
  if (bit((a - x) ^ (b + x), i) == bit(c, i)) {
    m = min(m, dfs(i + 1, a, b, c, x));
  }
  x |= 1LL << i;
  if (bit((a - x) ^ (b + x), i) == bit(c, i)) {
    m = min(m, dfs(i + 1, a, b, c, x));
  }
  return m;
}

void solve(long long N, std::vector<long long> A) {
  long long a = A[0];
  long long b = A[1];
  long long c = 0;
  loop(i, 2, N) c ^= A[i];
  // t == 0 となる x を見つけたい. 最小のもの.

  long long x = dfs(0, a, b, c, 0);

  if (x < a && (a - x) ^ (b + x) == c) {
    cout << x << endl;
  } else {
    cout << -1 << endl;
  }
  return;

  for (long long k = 0; k < A[0];) {
    auto t = (A[0] - k) ^ (A[1] + k) ^ x;
    if (t == 0) {
      cout << k << endl;
      return;
    }

    int s = 0;
    while (((t >> s) & 1) == 0)
      s++;
    k += 1 << s;
  }
  cout << -1 << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}