Problem p03097 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

int N, A, B;
vector<int> ans;

vector<int> dfs(int start, int goal, int len) {
  if (len == 1)
    return (vector<int>){start, goal};
  int x = 0;
  for (int i = 0; i < len; i++) {
    if ((start ^ goal) & (1 << i)) {
      x = i;
      break;
    }
  }
  int a = 0, b = 0;
  for (int i = 0; i < len; i++) {
    if (i < x) {
      if (start & (1 << i))
        a += (1 << i);
      if (goal & (1 << i))
        b += (1 << i);
    }
    if (i > x) {
      if (start & (1 << i))
        a += (1 << (i - 1));
      if (goal & (1 << i))
        b += (1 << (i - 1));
    }
  }
  int c = -1;
  for (int i = 0; i < len - 1; i++) {
    if ((a ^ (1 << i)) != b) {
      c = a ^ (1 << i);
      break;
    }
  }
  assert(c != -1);
  vector<int> q = dfs(a, c, len - 1), r = dfs(c, b, len - 1), res;
  for (auto n : q) {
    int now = (start & (1 << x) ? (1 << x) : 0);
    for (int i = 0; i < len - 1; i++) {
      if (i < x && (n & (1 << i)))
        now += (1 << i);
      if (i >= x && (n & (1 << i)))
        now += (1 << (i + 1));
    }
    res.push_back(now);
  }
  for (auto n : r) {
    int now = (goal & (1 << x) ? (1 << x) : 0);
    for (int i = 0; i < len - 1; i++) {
      if (i < x && (n & (1 << i)))
        now += (1 << i);
      if (i >= x && (n & (1 << i)))
        now += (1 << (i + 1));
    }
    res.push_back(now);
  }
  return res;
}

int main() {
  cin >> N >> A >> B;
  if (__builtin_popcount(A) % 2 == __builtin_popcount(B)) {
    cout << "NO" << endl;
    return 0;
  }
  vector<int> ans = dfs(A, B, N);
  cout << "YES" << endl;
  for (auto x : ans)
    cout << x << " ";
  cout << endl;
}

Fixed submission:
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

int N, A, B;
vector<int> ans;

vector<int> dfs(int start, int goal, int len) {
  if (len == 1)
    return (vector<int>){start, goal};
  int x = 0;
  for (int i = 0; i < len; i++) {
    if ((start ^ goal) & (1 << i)) {
      x = i;
      break;
    }
  }
  int a = 0, b = 0;
  for (int i = 0; i < len; i++) {
    if (i < x) {
      if (start & (1 << i))
        a += (1 << i);
      if (goal & (1 << i))
        b += (1 << i);
    }
    if (i > x) {
      if (start & (1 << i))
        a += (1 << (i - 1));
      if (goal & (1 << i))
        b += (1 << (i - 1));
    }
  }
  int c = -1;
  for (int i = 0; i < len - 1; i++) {
    if ((a ^ (1 << i)) != b) {
      c = a ^ (1 << i);
      break;
    }
  }
  assert(c != -1);
  vector<int> q = dfs(a, c, len - 1), r = dfs(c, b, len - 1), res;
  for (auto n : q) {
    int now = (start & (1 << x) ? (1 << x) : 0);
    for (int i = 0; i < len - 1; i++) {
      if (i < x && (n & (1 << i)))
        now += (1 << i);
      if (i >= x && (n & (1 << i)))
        now += (1 << (i + 1));
    }
    res.push_back(now);
  }
  for (auto n : r) {
    int now = (goal & (1 << x) ? (1 << x) : 0);
    for (int i = 0; i < len - 1; i++) {
      if (i < x && (n & (1 << i)))
        now += (1 << i);
      if (i >= x && (n & (1 << i)))
        now += (1 << (i + 1));
    }
    res.push_back(now);
  }
  return res;
}

int main() {
  cin >> N >> A >> B;
  if (__builtin_popcount(A) % 2 == __builtin_popcount(B) % 2) {
    cout << "NO" << endl;
    return 0;
  }
  vector<int> ans = dfs(A, B, N);
  cout << "YES" << endl;
  for (auto x : ans)
    cout << x << " ";
  cout << endl;
}