Problem p03097 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
constexpr char ln = '\n';
constexpr long long MOD = 1000000007LL;
// constexpr long long MOD = 998244353LL;
template <class T, class U> inline bool chmax(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void print() { cout << "\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////

using bit = bitset<7>;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, A, B;
  cin >> N >> A >> B;

  if (__builtin_popcount(A) % 2 == __builtin_popcount(B) % 2) {
    cout << "NO" << ln;
    return 0;
  }
  cout << "YES" << ln;

  auto dfs = [](auto &&self, int n, bit a, bit b) -> vector<bit> {
    if (!n) {
      vector<bit> ret = {a, b};
      return ret;
    }
    if (a.test(n) ^ b.test(n)) {
      auto c = a;
      c.flip(0);
      auto P = self(self, n - 1, a, c);
      c.flip(n);
      auto Q = self(self, n - 1, c, b);
      vector<bit> ret;
      int L = P.size();
      rep(i, L) ret.emplace_back(P[i]);
      rep(i, L) ret.emplace_back(Q[i]);
      return ret;
    } else {
      auto P = self(self, n - 1, a, b);
      a.flip(n);
      auto c = P[1];
      c.flip(n);
      auto Q = self(self, n - 1, a, c);
      vector<bit> ret;
      int L = P.size();
      ret.emplace_back(P[0]);
      rep(i, L) ret.emplace_back(Q[i]);
      for (int i = 1; i < L; i++)
        ret.emplace_back(P[i]);
      return ret;
    }
  };
  bit x(A);
  bit y(B);
  auto ans = dfs(dfs, N - 1, x, y);
  for (auto i : ans)
    cout << i.to_ullong() << " ";
  cout << ln;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
constexpr char ln = '\n';
constexpr long long MOD = 1000000007LL;
// constexpr long long MOD = 998244353LL;
template <class T, class U> inline bool chmax(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void print() { cout << "\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////

using bit = bitset<17>;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, A, B;
  cin >> N >> A >> B;

  if (__builtin_popcount(A) % 2 == __builtin_popcount(B) % 2) {
    cout << "NO" << ln;
    return 0;
  }
  cout << "YES" << ln;

  auto dfs = [](auto &&self, int n, bit a, bit b) -> vector<bit> {
    if (!n) {
      vector<bit> ret = {a, b};
      return ret;
    }
    if (a.test(n) ^ b.test(n)) {
      auto c = a;
      c.flip(0);
      auto P = self(self, n - 1, a, c);
      c.flip(n);
      auto Q = self(self, n - 1, c, b);
      vector<bit> ret;
      int L = P.size();
      rep(i, L) ret.emplace_back(P[i]);
      rep(i, L) ret.emplace_back(Q[i]);
      return ret;
    } else {
      auto P = self(self, n - 1, a, b);
      a.flip(n);
      auto c = P[1];
      c.flip(n);
      auto Q = self(self, n - 1, a, c);
      vector<bit> ret;
      int L = P.size();
      ret.emplace_back(P[0]);
      rep(i, L) ret.emplace_back(Q[i]);
      for (int i = 1; i < L; i++)
        ret.emplace_back(P[i]);
      return ret;
    }
  };
  bit x(A);
  bit y(B);
  auto ans = dfs(dfs, N - 1, x, y);
  for (auto i : ans)
    cout << i.to_ullong() << " ";
  cout << ln;
}