Problem p03097 (C++)

Buggy submission (status: Runtime Error):
/**
 *    author:  tourist
 *    created: 16.03.2019 15:29:11
 **/
#include <bits/stdc++.h>

using namespace std;

int remove_bit(int x, int j) {
  int low = x & ((1 << j) - 1);
  return ((x >> (j + 1)) << j) + low;
}

int insert_bit(int x, int j, int v) {
  int low = x & ((1 << j) - 1);
  return ((x >> j) << (j + 1)) + (v << j) + low;
}

vector<int> solve(int n, int a, int b) {
  if (n == 1) {
    return {a, b};
  }
  for (int j = 0; j < n; j++) {
    if ((a ^ b) & (1 << j)) {
      int aa = remove_bit(a, j);
      int bb = remove_bit(b, j);
      for (int u = 0; u < (1 << n); u++) {
        int uu = remove_bit(u, j);
        if (uu != aa && uu != bb) {
          vector<int> p1 = solve(n - 1, aa, uu);
          vector<int> p2 = solve(n - 1, uu, bb);
          for (int &x : p1) {
            x = insert_bit(x, j, (a >> j) & 1);
          }
          for (int &x : p2) {
            x = insert_bit(x, j, (b >> j) & 1);
          }
          p1.insert(p1.end(), p2.begin(), p2.end());
          return p1;
        }
      }
    }
  }
  assert(false);
  return {};
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, a, b;
  cin >> n >> a >> b;
  if (__builtin_popcount(a ^ b) % 2 == 0) {
    cout << "NO" << '\n';
    return 0;
  }
  cout << "YES" << '\n';
  vector<int> p = solve(n, a, b);
  set<int> val(p.begin(), p.end());
  assert((int)val.size() == (1 << n));
  for (int i = 0; i < (1 << n); i++) {
    assert(0 <= p[i] && p[i] < (1 << n));
  }
  for (int i = 0; i < (1 << n) - 1; i++) {
    assert(__builtin_popcount(p[i] ^ p[i + 1]) == 1);
  }
  for (int i = 0; i < (1 << n); i++) {
    if (i > 0) {
      cout << " ";
    }
    cout << p[i];
  }
  cout << '\n';
  return 0;
}


Fixed submission:
/**
 *    author:  tourist
 *    created: 16.03.2019 15:29:11
 **/
#include <bits/stdc++.h>

using namespace std;

int remove_bit(int x, int j) {
  int low = x & ((1 << j) - 1);
  return ((x >> (j + 1)) << j) + low;
}

int insert_bit(int x, int j, int v) {
  int low = x & ((1 << j) - 1);
  return ((x >> j) << (j + 1)) + (v << j) + low;
}

vector<int> solve(int n, int a, int b) {
  if (n == 1) {
    return {a, b};
  }
  for (int j = 0; j < n; j++) {
    if ((a ^ b) & (1 << j)) {
      int aa = remove_bit(a, j);
      int bb = remove_bit(b, j);
      for (int u = 0; u < (1 << n); u++) {
        int uu = remove_bit(u, j);
        if (uu != aa && uu != bb) {
          if (__builtin_popcount(aa ^ uu) % 2 == 0) {
            continue;
          }
          if (__builtin_popcount(bb ^ uu) % 2 == 0) {
            continue;
          }
          vector<int> p1 = solve(n - 1, aa, uu);
          vector<int> p2 = solve(n - 1, uu, bb);
          for (int &x : p1) {
            x = insert_bit(x, j, (a >> j) & 1);
          }
          for (int &x : p2) {
            x = insert_bit(x, j, (b >> j) & 1);
          }
          p1.insert(p1.end(), p2.begin(), p2.end());
          return p1;
        }
      }
    }
  }
  assert(false);
  return {};
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, a, b;
  cin >> n >> a >> b;
  if (__builtin_popcount(a ^ b) % 2 == 0) {
    cout << "NO" << '\n';
    return 0;
  }
  cout << "YES" << '\n';
  vector<int> p = solve(n, a, b);
  set<int> val(p.begin(), p.end());
  assert((int)val.size() == (1 << n));
  for (int i = 0; i < (1 << n); i++) {
    assert(0 <= p[i] && p[i] < (1 << n));
  }
  for (int i = 0; i < (1 << n) - 1; i++) {
    assert(__builtin_popcount(p[i] ^ p[i + 1]) == 1);
  }
  for (int i = 0; i < (1 << n); i++) {
    if (i > 0) {
      cout << " ";
    }
    cout << p[i];
  }
  cout << '\n';
  return 0;
}