Problem p03097 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
#ifdef wxh010910
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, a, b;
  cin >> n >> a >> b;
  int k = __builtin_popcount(a ^ b);
  if (k % 2 == 0) {
    cout << "NO"
         << "\n";
    return 0;
  }
  cout << "YES"
       << "\n";
  vector<vector<int>> to_one(n + 1);
  to_one[1] = {0, 1};
  for (int i = 2; i <= n; ++i) {
    for (auto x : to_one[i - 1]) {
      to_one[i].push_back(x << 1);
    }
    reverse(to_one[i - 1].begin(), to_one[i - 1].end());
    for (auto x : to_one[i - 1]) {
      to_one[i].push_back(x << 1 | 1);
    }
    reverse(to_one[i - 1].begin(), to_one[i - 1].end());
  }
  vector<vector<int>> to_all(k + 1);
  to_all[1] = {0, 1};
  for (int i = 3; i <= k; i += 2) {
    for (auto x : to_all[i - 2]) {
      to_all[i].push_back(x);
    }
    for (auto x : to_one[i - 2]) {
      to_all[i].push_back(x ^ ((1 << (i - 1)) - 1));
    }
    for (auto x : to_one[i - 1]) {
      to_all[i].push_back(x ^ ((1 << i) - 2));
    }
  }
  vector<int> ans;
  for (auto x : to_all[k]) {
    for (auto y : to_one[n - k]) {
      ans.push_back(y << k | x);
    }
    reverse(to_one[n - k].begin(), to_one[n - k].end());
  }
  vector<int> to_bit(n);
  int ptr = 0;
  for (int i = 0; i < n; ++i) {
    if ((a ^ b) >> i & 1) {
      to_bit[ptr++] = i;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!((a ^ b) >> i & 1)) {
      to_bit[ptr++] = i;
    }
  }
  for (int i = 0; i < 1 << n; ++i) {
    int real_ans = 0;
    for (int j = 0; j < n; ++j) {
      if (ans[i] >> j & 1) {
        real_ans |= 1 << to_bit[j];
      }
    }
    ans[i] = real_ans;
  }
  for (int i = 0; i < 1 << n; ++i) {
    if (i) {
      cout << " ";
    }
    cout << (ans[i] ^ a);
  }
  cout << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
#ifdef wxh010910
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, a, b;
  cin >> n >> a >> b;
  int k = __builtin_popcount(a ^ b);
  if (k % 2 == 0) {
    cout << "NO"
         << "\n";
    return 0;
  }
  cout << "YES"
       << "\n";
  vector<vector<int>> to_one(n + 1);
  to_one[0] = {0};
  to_one[1] = {0, 1};
  for (int i = 2; i <= n; ++i) {
    for (auto x : to_one[i - 1]) {
      to_one[i].push_back(x << 1);
    }
    reverse(to_one[i - 1].begin(), to_one[i - 1].end());
    for (auto x : to_one[i - 1]) {
      to_one[i].push_back(x << 1 | 1);
    }
    reverse(to_one[i - 1].begin(), to_one[i - 1].end());
  }
  vector<vector<int>> to_all(k + 1);
  to_all[1] = {0, 1};
  for (int i = 3; i <= k; i += 2) {
    for (auto x : to_all[i - 2]) {
      to_all[i].push_back(x);
    }
    for (auto x : to_one[i - 2]) {
      to_all[i].push_back(x ^ ((1 << (i - 1)) - 1));
    }
    for (auto x : to_one[i - 1]) {
      to_all[i].push_back(x ^ ((1 << i) - 2));
    }
  }
  vector<int> ans;
  for (auto x : to_all[k]) {
    for (auto y : to_one[n - k]) {
      ans.push_back(y << k | x);
    }
    reverse(to_one[n - k].begin(), to_one[n - k].end());
  }
  vector<int> to_bit(n);
  int ptr = 0;
  for (int i = 0; i < n; ++i) {
    if ((a ^ b) >> i & 1) {
      to_bit[ptr++] = i;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!((a ^ b) >> i & 1)) {
      to_bit[ptr++] = i;
    }
  }
  for (int i = 0; i < 1 << n; ++i) {
    int real_ans = 0;
    for (int j = 0; j < n; ++j) {
      if (ans[i] >> j & 1) {
        real_ans |= 1 << to_bit[j];
      }
    }
    ans[i] = real_ans;
  }
  for (int i = 0; i < 1 << n; ++i) {
    if (i) {
      cout << " ";
    }
    cout << (ans[i] ^ a);
  }
  cout << "\n";
  return 0;
}