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

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

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

#define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i)
#define REP(i, y) FOR(i, 0, y)
#define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i)
#define RREP(i, x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...)                                                      \\
  cerr << "line " << __LINE__ << " : ";                                        \\
  debug_print_in(x);

template <typename First> void debug_print_in(First first) {
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest) {
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

template <typename First> void OUT(First first) {
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
};
template <class t, class u> void chmin(t &a, u b) {
  if (a > b)
    a = b;
};
int popcount(int t) { return __builtin_popcount(t); }  // GCC
int popcount(ll t) { return __builtin_popcountll(t); } // GCC

template <typename T> void vec_print(vector<T> VEC) {
  REP(i, VEC.size()) { cerr << VEC[i] << " "; }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    REP(i, MAT.size()){REP(j, MAT[i].size()){cerr << MAT[i][j] << " ";
}
cerr << endl;
}
}
;

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9) + 7);

ll calc(ll A1, ll A2, ll A3) {
  if (A3 == 0) {
    if (A1 < A2)
      return INFLL / 4;
    return (A1 - A2) / 2;
  }
  if ((A1 + A2 + A3) % 2 == 1) {
    return INFLL / 4;
  }
  ll x = calc(A1 / 2, A2 / 2, A3 / 2) * 2;
  ll y = calc((A1 - 1) / 2, (A2 + 1) / 2, A3 / 2) * 2 + 1;
  ll tmp = min(x, y);
  if (tmp > INFLL / 4) {
    tmp = INFLL / 4;
  }

  return min(x, y);
}

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\\n";)

  ll N;
  IN(N);
  vll A(N);
  REP(i, N) IN(A[i]);

  ll A1 = A[0];
  ll A2 = A[1];
  ll A3 = 0;
  FOR(i, 2, N) A3 ^= A[i];

  ll ans = calc(A1, A2, A3);

  if (ans >= INFLL / 4 || ans >= A1) {
    OUT(-1);
  } else if (((A1 - ans) ^ (A2 + ans)) != A3) {
    OUT(-1);
  } else {
    OUT(ans);
  }

  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

#define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i)
#define REP(i, y) FOR(i, 0, y)
#define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i)
#define RREP(i, x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...)                                                      \\
  cerr << "line " << __LINE__ << " : ";                                        \\
  debug_print_in(x);

template <typename First> void debug_print_in(First first) {
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest) {
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

template <typename First> void OUT(First first) {
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
};
template <class t, class u> void chmin(t &a, u b) {
  if (a > b)
    a = b;
};
int popcount(int t) { return __builtin_popcount(t); }  // GCC
int popcount(ll t) { return __builtin_popcountll(t); } // GCC

template <typename T> void vec_print(vector<T> VEC) {
  REP(i, VEC.size()) { cerr << VEC[i] << " "; }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    REP(i, MAT.size()){REP(j, MAT[i].size()){cerr << MAT[i][j] << " ";
}
cerr << endl;
}
}
;

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9) + 7);

vll calc_digit(ll x) {
  vll ans(50, 0);
  REP(j, 50) {
    ans[j] = x % 2;
    x /= 2;
  }
  return ans;
}

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\\n";)

  ll N;
  IN(N);
  vll A(N);
  REP(i, N) IN(A[i]);

  vvll pile(300, vll(50, 0));
  FOR(i, 2, N) {
    ll A_tmp = A[i];
    REP(j, 50) {
      pile[i][j] = A_tmp % 2;
      A_tmp /= 2;
    }
  }

  vll digitsum(50, 0);
  for (ll i = 2; i < N; ++i) {
    for (ll j = 0; j < 50; ++j) {
      digitsum[j] += pile[i][j];
      digitsum[j] %= 2;
    }
  }

  ll target = 0;
  REP(j, 50) { target += digitsum[j] << j; }

  REP(x, A[0]) {
    ll A1 = A[0] - x;
    ll A2 = A[1] + x;

    if ((A1 ^ A2) == target) {
      OUT(x);
      /*
      vll A1_digit = calc_digit(A1);
      vll A2_digit = calc_digit(A2);

  vec_print(digitsum);
  vec_print(A1_digit);
  vec_print(A2_digit);
  */

      return 0;
    }
  }

  OUT(-1);

  return 0;

  ll shift = 0;
  ll A1 = A[0];
  ll A2 = A[1];

  vll A1_digit = calc_digit(A1);
  vll A2_digit = calc_digit(A2);

  /*
  vec_print(digitsum);
  vec_print(A1_digit);
  vec_print(A2_digit);
  */
  RREP(j, 50 - 1) {
    if ((A1_digit[j] + A2_digit[j] + digitsum[j]) % 2 != 0) {
      if (j > 0) {
        // shift += (1LL<<(j-1));
        ll tmp1 = (1LL << j) - A2;
        ll tmp2 = A1 - (1LL << j) + 1;
        if (tmp1 <= 0 || tmp1 > A1) {
          shift += tmp2;
        } else if (tmp2 <= 0 || tmp2 > A2) {
          shift += tmp1;
        } else {
          shift += min(tmp1, tmp2);
        }
        // debug_print(j, A1, A2, A1_digit[j], A2_digit[j], shift);
        A1 = A[0] - shift;
        A2 = A[1] + shift;
        for (ll k = 49; k >= j; --k) {
          if (A1 >= (1LL << k)) {
            A1 -= (1LL << k);
          }
          if (A2 >= (1LL << k)) {
            A2 -= (1LL << k);
          }
        }
        A1_digit = calc_digit(A1);
        A2_digit = calc_digit(A2);
      }
    }
    if (A1 >= (1LL << j)) {
      A1 -= (1LL << j);
    }
    if (A2 >= (1LL << j)) {
      A2 -= (1LL << j);
    }
  }

  A1_digit = calc_digit(A[0] - shift);
  A2_digit = calc_digit(A[1] + shift);

  bool flag = true;
  REP(j, 50) {
    if ((A1_digit[j] + A2_digit[j] + digitsum[j]) % 2 == 0)
      continue;
    flag = false;
  }

  if (flag && 0 <= shift && shift < A[0]) {
    OUT(shift);
  } else {
    OUT(-1);
  }

  return 0;
}