Problem p02631 (C++) — buggy submission, verdict: Runtime Error

// Author : Naresh Bharasagar
// Instt. : Indian Institute of Technology Guwahati

//--------------------------------------------------------------------------------------------------------------------------------

#include <bits/stdc++.h>
using namespace std;
typedef long long int loo;
typedef long double ld;

#pragma GCC target("sse4.2")
#define loop(i, begin, end)                                                    \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define loopset(it, val, dtype)                                                \\
  for (set<dtype>::iterator it = val.begin(); it != val.end(); it++)
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define vloo vector<loo>
#define vi vector<int>
#define pi pair<int, int>
#define mp make_pair
#define ploo pair<loo, loo>
#define fillz(a) memset(a, 0, sizeof(a))
#define outa(a, n) loop(i, 0, n) cout << a[i] << " ";
#define pr(a) cout << a << "\\n";
#define pb(l) push_back(l)
#define ff first
#define ss second
#define input(a)                                                               \\
  for (auto &i : a)                                                            \\
    cin >> i;
#define deb0(i) cout << "[" << #i << "=" << i << "]" << endl;
#define nl cout << "\\n";
#define INF 1000000000
#define M 1000000007

string decToBin(loo n) {
  string s = "0";
  if (n == 0)
    return s;
  s = "";
  while (n > 0) {
    s += (n % 2) + '0';
    n = n / 2;
  }
  reverse(s.begin(), s.end());
  return s;
}

loo binToDec(string s) {
  loo ans = 0;
  reverse(s.begin(), s.end());
  loop(i, 0, s.size()) ans += pow(2, i) * (s[i] - '0');
  return ans;
}

// ----------------------------------------------------------------

void solve() {
  loo n;
  cin >> n;
  loo a[n];
  loo mx = 1;
  loop(i, 0, n) {
    cin >> a[i];
    if (a[i])
      mx = max(mx, loo(log2(a[i])) + 1);
  }
  loo allxor = 0;
  loop(i, 0, n) {
    allxor ^= (a[i] ^ a[i + 1]);
    i++;
  }
  if (allxor)
    mx = max(mx, loo(ceil(log2(allxor))));
  string allx = decToBin(allxor);
  allx = string(mx - allx.size(), '0') + allx;
  // deb0(allx);
  loop(i, 0, n) {
    // deb0(i);
    string d = decToBin(a[i]);
    d = string(mx - d.size(), '0') + d;
    string res = string(mx, '0');
    // deb0(d);
    loop(j, 0, mx) {
      if (allx[j] == '0') {
        res[j] = d[j];
      } else {
        if (d[j] == '1')
          res[j] = '0';
        else
          res[j] = '1';
      }
    }
    cout << binToDec(res) << " ";
  }
  nl;
}

int main() {
  fast_io

      // freopen("filename.in","r",stdin);
      // freopen("filename.out","w",stdout);

      loo q = 1;
  // cin >> q;
  while (q--) {
    solve();
  }

  return 0;
}

Error: WA

Fixed submission:

// Author : Naresh Bharasagar
// Instt. : Indian Institute of Technology Guwahati

//--------------------------------------------------------------------------------------------------------------------------------

#include <bits/stdc++.h>
using namespace std;
typedef long long int loo;
typedef long double ld;

#pragma GCC target("sse4.2")
#define loop(i, begin, end)                                                    \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define loopset(it, val, dtype)                                                \\
  for (set<dtype>::iterator it = val.begin(); it != val.end(); it++)
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define vloo vector<loo>
#define vi vector<int>
#define pi pair<int, int>
#define mp make_pair
#define ploo pair<loo, loo>
#define fillz(a) memset(a, 0, sizeof(a))
#define outa(a, n) loop(i, 0, n) cout << a[i] << " ";
#define pr(a) cout << a << "\\n";
#define pb(l) push_back(l)
#define ff first
#define ss second
#define input(a)                                                               \\
  for (auto &i : a)                                                            \\
    cin >> i;
#define deb0(i) cout << "[" << #i << "=" << i << "]" << endl;
#define nl cout << "\\n";
#define INF 1000000000
#define M 1000000007

loo power(loo x, loo y, loo p) {
  loo res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

loo power(loo x, loo y) {
  loo temp;
  if (y == 0)
    return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else
    return x * temp * temp;
}

bool sortbysec(const pair<loo, loo> &a, const pair<loo, loo> &b) {
  return (a.second > b.second);
}

string decToBin(loo n) {
  string s = "0";
  if (n == 0)
    return s;
  s = "";
  while (n > 0) {
    s += (n % 2) + '0';
    n = n / 2;
  }
  reverse(s.begin(), s.end());
  return s;
}

loo binToDec(string s) {
  loo ans = 0;
  reverse(s.begin(), s.end());
  loop(i, 0, s.size()) ans += pow(2, i) * (s[i] - '0');
  return ans;
}

loo andOperator(loo a, loo b) {
  string sa = decToBin(a);
  string sb = decToBin(b);
  loo sza = sa.size(), szb = sb.size();
  if (sza > szb)
    loop(i, 0, sza - szb) sb = "0" + sb;
  else if (szb > sza)
    loop(i, 0, szb - sza) sa = "0" + sa;
  string res = "";
  loop(i, 0, sa.size()) {
    if (sa[i] == '1' && sb[i] == '1')
      res += "1";
    else
      res += "0";
  }
  return binToDec(res);
}

loo orOperator(loo a, loo b) {
  string sa = decToBin(a);
  string sb = decToBin(b);
  loo sza = sa.size(), szb = sb.size();
  if (sza > szb) {
    loop(i, 0, sza - szb) sb = "0" + sb;
  } else if (szb > sza) {
    loop(i, 0, szb - sza) sa = "0" + sa;
  }
  string res = "";
  loop(i, 0, sa.size()) {
    if (sa[i] == '0' && sb[i] == '0')
      res += "0";
    else
      res += "1";
  }
  return binToDec(res);
}

loo nCr(loo n, loo r) {
  loo p = 1, k = 1;
  if (n - r < r)
    r = n - r;
  if (r != 0) {
    while (r) {
      p *= n;
      k *= r;
      loo m = __gcd(p, k);
      p /= m;
      k /= m;
      n--;
      r--;
    }
  } else
    p = 1;
  return p;
}

// vector<int> dist;
vector<bool> vis(100001, false);
void dfs(vector<loo> adj[], loo src) {
  vis[src] = true;
  loop(i, 0, adj[src].size()) {
    loo x = adj[src][i];
    if (vis[x] == false) {
      vis[x] = true;
      // dist[x] = dist[src]+1;
      dfs(adj, x);
    }
  }
}

// vector<loo> sg(100001,-1); //prev
void bfs(vector<loo> adj[], loo src) {
  list<loo> ql;
  vis[src] = true;
  ql.pb(src);
  while (!(ql.empty())) {
    src = ql.front();
    // cout << src << " ";
    ql.pop_front();

    loop(i, 0, adj[src].size()) {
      if (!vis[adj[src][i]]) {
        vis[adj[src][i]] = true;
        // sg[adj[src][i]]=src;
        ql.pb(adj[src][i]);
      }
    }
  }
}

// ----------------------------------------------------------------

void solve() {
  loo n;
  cin >> n;
  loo a[n];
  loo mx = 0;
  loop(i, 0, n) {
    cin >> a[i];
    mx = max(mx, loo(ceil(log2(a[i]))));
  }
  loo allxor = 0;
  loop(i, 0, n) {
    allxor ^= (a[i] ^ a[i + 1]);
    i++;
  }
  mx = max(mx, loo(ceil(log2(allxor))));
  string allx = decToBin(allxor);
  allx = string(mx - allx.size(), '0') + allx;
  loop(i, 0, n) {
    string d = decToBin(a[i]);
    d = string(mx - d.size(), '0') + d;
    string res = string(mx, '0');
    loop(j, 0, mx) {
      if (allx[j] == '0') {
        res[j] = d[j];
      } else {
        if (d[j] == '1')
          res[j] = '0';
        else
          res[j] = '1';
      }
    }
    cout << binToDec(res) << " ";
  }
  nl;
}

int main() {
  fast_io

      // freopen("filename.in","r",stdin);
      // freopen("filename.out","w",stdout);

      loo q = 1;
  // cin >> q;
  while (q--) {
    solve();
  }

  return 0;
}