Problem p03097 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define int long long
#define ll long long
#define P pair<int, int>
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define all(V) V.begin(), V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 10;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = 1e-9;
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
using namespace std;
inline ll gcd(ll a, ll b) {
  while (b) {
    ll c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
bool isprime(ll n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
ll mypow(ll a, ll b) {
  if (!b)
    return 1;
  if (b & 1)
    return mypow(a, b - 1) * a;
  int memo = mypow(a, b >> 1);
  return memo * memo;
}
int n, a, b;
int bit[1 << 16];
void func(int m, int e, int c, int d, int l, int r) {
  bit[l] = c;
  bit[r - 1] = d;
  if (m == 1)
    return;
  int dbit;
  rep(i, n) {
    if ((e & (1 << i)) && (c & (1 << i)) != (d & (1 << i))) {
      dbit = i;
      break;
    }
  }
  int md = c;
  rep(i, n) {
    if ((e & (1 << i)) && i != dbit) {
      md ^= (1 << i);
      break;
    }
  }
  func(m - 1, e - (1 << dbit), c, md, l, (l + r) / 2);
  func(m - 1, e - (1 << dbit), md, d, (l + r) / 2, r);
  for (int i = l; i < (l + r) / 2; i++) {
    if (bit[i] & (1 << dbit))
      bit[i] -= 1 << dbit;
    bit[i] += c & (1 << dbit);
  }
  for (int i = (l + r) / 2; i < r; i++) {
    if (bit[i] & (1 << dbit))
      bit[i] -= 1 << dbit;
    bit[i] += d & (1 << dbit);
  }
}
signed main() {
  cin >> n >> a >> b;
  int cnt = 0;
  if ((bitset<17>(a).count() & 1) == (bitset<17>(b).count() & 1)) {
    puts("NO");
    return 0;
  }
  puts("YES");
  func(n, (1 << n) - 1, a, b, 0, (1 << n));
  rep(i, (1 << n) - 1) cout << bit[i] << " ";
  cout << bit[(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define int long long
#define ll long long
#define P pair<int, int>
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define all(V) V.begin(), V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 10;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = 1e-9;
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
using namespace std;
inline ll gcd(ll a, ll b) {
  while (b) {
    ll c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
bool isprime(ll n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
ll mypow(ll a, ll b) {
  if (!b)
    return 1;
  if (b & 1)
    return mypow(a, b - 1) * a;
  int memo = mypow(a, b >> 1);
  return memo * memo;
}
int n, a, b;
int bit[1 << 17];
void func(int m, int e, int c, int d, int l, int r) {
  bit[l] = c;
  bit[r - 1] = d;
  if (m == 1)
    return;
  int dbit;
  rep(i, n) {
    if ((e & (1 << i)) && (c & (1 << i)) != (d & (1 << i))) {
      dbit = i;
      break;
    }
  }
  int md = c;
  rep(i, n) {
    if ((e & (1 << i)) && i != dbit) {
      md ^= (1 << i);
      break;
    }
  }
  func(m - 1, e - (1 << dbit), c, md, l, (l + r) / 2);
  func(m - 1, e - (1 << dbit), md, d, (l + r) / 2, r);
  for (int i = l; i < (l + r) / 2; i++) {
    if (bit[i] & (1 << dbit))
      bit[i] -= 1 << dbit;
    bit[i] += c & (1 << dbit);
  }
  for (int i = (l + r) / 2; i < r; i++) {
    if (bit[i] & (1 << dbit))
      bit[i] -= 1 << dbit;
    bit[i] += d & (1 << dbit);
  }
}
signed main() {
  cin >> n >> a >> b;
  int cnt = 0;
  if ((bitset<17>(a).count() & 1) == (bitset<17>(b).count() & 1)) {
    puts("NO");
    return 0;
  }
  puts("YES");
  func(n, (1 << n) - 1, a, b, 0, (1 << n));
  rep(i, (1 << n) - 1) cout << bit[i] << " ";
  cout << bit[(1 << n) - 1] << endl;
  return 0;
}