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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, 1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

int main() {
  ll n;
  cin >> n;
  ll a, b, sum = 0;
  cin >> a >> b;
  for (int i = 2; i < n; i++) {
    ll d;
    cin >> d;
    sum ^= d;
  }
  if (sum > a + b) {
    // cout << "a\\n" ;
    cout << -1 << endl;
    return 0;
  }
  ll num = 0;
  ll sub = (a + b - sum);
  if (sub & 1) {
    // cout << "b\\n" ;
    cout << -1 << endl;
    return 0;
  }
  sub /= 2;
  for (int i = 62; i >= 0; i--) {
    if (sum >> i & 1)
      continue;
    if (sub >= (1LL << i)) {
      num |= (1LL << i);
      sub -= (1LL << i);
    }
  }
  if (sub != 0) {
    // cout << "c\\n" ;
    cout << -1 << endl;
    // cout << sub << endl ;
    return 0;
  }
  for (int i = 62; i >= 0; i--) {
    if (sum >> i & 1) {
      if (num + (1LL << i) <= a) {
        num |= (1LL << i);
      }
    }
  }
  if (num == 0 || num > a) {
    // cout << "d\\n" ;
    cout << -1 << endl;
    return 0;
  }
  cout << a - num << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, -1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

vll ans(0);
ll g = 0;
ll mn, mx;

void DFS(ll a, ll b, ll d, ll rem) {
  if (rem < 0)
    return;
  if (rem % (1LL << d) != 0)
    return;
  if (a > mn)
    return;
  if (a + rem <= mx)
    return;
  if (rem == 0) {
    if ((a ^ b) == g) {
      // cout << a << " " << b << endl ;
      ans.emplace_back(a);
      chmax(mx, a);
    }
    return;
  }
  if (g >> d & 1) {
    DFS(a + (1LL << d), b, d + 1, rem - (1LL << d));
    DFS(a, b + (1LL << d), d + 1, rem - (1LL << d));
  } else {
    DFS(a + (1LL << d), b + (1LL << d), d + 1, rem - 2 * (1LL << d));
    DFS(a, b, d + 1, rem);
  }
}

int main() {
  ll n;
  cin >> n;
  vll a(n);
  for (auto &i : a)
    cin >> i;
  for (int i = 2; i < n; i++)
    g ^= a.at(i);
  ll sum = a.at(0) + a.at(1);
  mn = a.at(0);
  if (g & 1) {
    DFS(1, 0, 1, sum - 1);
    DFS(0, 1, 1, sum - 1);
  } else {
    DFS(0, 0, 1, sum);
    DFS(1, 1, 1, sum - 2);
  }
  ll res = a.at(0) + 1;
  sort(all(ans));
  for (int i = 0; i < ans.size(); i++) {
    if (ans.at(i) > a.at(0))
      break;
    if (ans.at(i) == 0)
      continue;
    res = ans.at(i);
  }
  cout << a.at(0) - res << endl;
  // for(auto i:ans) cout << i << " " ; cout << endl ;
}