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

#define NDEBUG
NDEBUG

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FORA(i, a, n) for (int i = (a); i < (n); ++i)

typedef int64_t i64;

// TC_REMOVE_BEGIN
/// caide keep
bool __hack = std::ios::sync_with_stdio(false);
/// caide keep
auto __hack1 = cin.tie(nullptr);
// TC_REMOVE_END

// Section with adoption of array and vector algorithms.

// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
//
// template <class T> using StdTree = tree<T, null_type, less<T>,
// rb_tree_tag,tree_order_statistics_node_update>;

template <class T> inline T set_bit(T mask, int bit) {
  assert(0 <= bit && bit < numeric_limits<T>::digits);
  return mask | ((T)(1) << bit);
}

namespace template_util {

constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; }

template <int N> struct bytetype {};

/// caide keep
template <uint64_t N> struct minimal_uint : bytetype<bytecount(N)> {};
} // namespace template_util

template <class T> T next(istream &in) {
  T ret;
  in >> ret;
  return ret;
}

template <class T> vector<T> next_vec(istream &in, size_t n) {
  vector<T> ret(n);
  for (size_t i = 0; i < n; ++i) {
    ret[i] = next<T>(in);
  }
  return ret;
}

i64 X, S;
i64 res = -1;
i64 lim;

void rec(int b, i64 a) {
  if (b == 41) {
    return;
  }
  if ((a + (a ^ X)) == S) {
    //    i64 b = (a ^ X);
    //    if (b > lim) {
    //      if (a < res) {
    //        return;
    //      }
    //    }
    //    dbg(a, a ^ X, S);
    if (a < lim) {
      res = max(res, a);
    }
    if ((a ^ X) < lim) {
      res = max(res, (a ^ X));
    }
    //    return;
  }

  i64 A = set_bit(a, b);
  i64 M = (1LL << (b + 1)) - 1;
  i64 S2 = (S & M);

  if (((A + (A ^ X)) & M) == S2) {
    rec(b + 1, A);
  }
  if (((a + (a ^ X)) & M) == S2) {
    rec(b + 1, a);
  }
}

void solve(istream &in, ostream &out) {
  auto n = next<int>(in);
  auto a = next_vec<i64>(in, n);
  auto x = a[0];
  auto y = a[1];
  i64 z = 0;
  FORA(i, 2, n) { z ^= a[i]; }

  if ((x ^ y) == z) {
    out << 0 << "\\n";
    return;
  }

  lim = x;
  S = x + y;
  X = z;
  res = -1;
  rec(0, 0);

  if (res == 0 || res == -1) {
    out << "-1\\n";
    return;
  }
  out << x - res << "\\n";
}

// #include <fstream>

int main() {
  //  ifstream fin("bridges.in");
  //  ofstream fout("bridges.out");
  solve(cin, cout);
  //  solve(fin, fout);
  return 0;
}


Error: TLE

Fixed submission:

#define NDEBUG
NDEBUG

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FORA(i, a, n) for (int i = (a); i < (n); ++i)

typedef int64_t i64;

// TC_REMOVE_BEGIN
/// caide keep
bool __hack = std::ios::sync_with_stdio(false);
/// caide keep
auto __hack1 = cin.tie(nullptr);
// TC_REMOVE_END

// Section with adoption of array and vector algorithms.

// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
//
// template <class T> using StdTree = tree<T, null_type, less<T>,
// rb_tree_tag,tree_order_statistics_node_update>;

template <class T> inline T set_bit(T mask, int bit) {
  assert(0 <= bit && bit < numeric_limits<T>::digits);
  return mask | ((T)(1) << bit);
}

namespace template_util {

constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; }

template <int N> struct bytetype {};

/// caide keep
template <uint64_t N> struct minimal_uint : bytetype<bytecount(N)> {};
} // namespace template_util

template <class T> T next(istream &in) {
  T ret;
  in >> ret;
  return ret;
}

template <class T> vector<T> next_vec(istream &in, size_t n) {
  vector<T> ret(n);
  for (size_t i = 0; i < n; ++i) {
    ret[i] = next<T>(in);
  }
  return ret;
}

i64 X, S;
i64 res = -1;
i64 lim;

void rec(int b, i64 a) {
  if (b == 50) {
    return;
  }
  if ((a + (a ^ X)) == S) {

    //    dbg(a, a ^ X, S);
    if (a < lim)
      res = max(res, a);
    if ((a ^ X) < lim) {
      res = max(res, (a ^ X));
    }
    //    return;
  }

  i64 A = set_bit(a, b);
  i64 M = (1LL << (b + 1)) - 1;
  i64 S2 = (S & M);

  if (((A + (A ^ X)) & M) == S2) {
    rec(b + 1, A);
  }
  if (((a + (a ^ X)) & M) == S2) {
    rec(b + 1, a);
  }
}

void solve(istream &in, ostream &out) {
  auto n = next<int>(in);
  auto a = next_vec<i64>(in, n);
  auto x = a[0];
  auto y = a[1];
  i64 z = 0;
  FORA(i, 2, n) { z ^= a[i]; }

  if ((x ^ y) == z) {
    out << 0 << "\\n";
    return;
  }

  lim = x;
  S = x + y;
  X = z;
  res = -1;
  rec(0, 0);

  if (res == 0 || res == -1) {
    out << "-1\\n";
    return;
  }
  out << x - res << "\\n";
}

// #include <fstream>

int main() {
  //  ifstream fin("bridges.in");
  //  ofstream fout("bridges.out");
  solve(cin, cout);
  //  solve(fin, fout);
  return 0;
}