Problem p03097 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n)                                                       \
  cerr << __LINE__ << ": " << #x << " = {";                                    \
  for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge))                  \
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \
  cerr << "}" << '\n'
#define debugMatrix(x, h, w)                                                   \
  cerr << __LINE__ << ": " << #x << " =\n";                                    \
  for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) {                \
    cerr << ((hoge ? " {" : "{{"));                                            \
    for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga))                \
      cerr << ((fuga ? ", " : "")) << x[hoge][fuga];                           \
    cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n';           \
  }
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N, A, B;
  cin >> N >> A >> B;
  if (!(__builtin_popcount(A ^ B) & 1)) {
    cout << "NO" << '\n';
    return 0;
  }
  auto f = [](int x, int i) {
    return (x >> (i + 1)) * (1 << i) ^ (x % (1 << i));
  };
  auto g = [](int x, int i, bool b) {
    return (x >> (i)) * (1 << (i + 1)) ^ (b << i) ^ (x % (1 << i));
  };
  function<vector<int>(int, int, int)> rec = [&](int n, int a, int b) {
    if (n == 1)
      return vector<int>({a, b});
    vector<int> ret(1 << n);
    for (int i = 0; i < n; i++) {
      if (((a ^ b) >> i) & 1) {
        int aa = f(a, i);
        int bb = f(b, i);
        auto l = rec(n - 1, aa, aa ^ 1);
        auto r = rec(n - 1, aa ^ 1, bb);
        for (int j = 0; j < (1 << (n - 1)); j++) {
          ret[j] = g(l[j], i, (a >> i) & 1);
          ret[j + (1 << (n - 1))] = g(r[j], i, (b >> i) & 1);
        }
      }
    }
    return ret;
  };
  auto ans = rec(N, A, B);
  cout << "YES" << '\n';
  for (int i = 0; i < (1 << N); i++)
    cout << (i ? " " : "") << ans[i];
  cout << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n)                                                       \
  cerr << __LINE__ << ": " << #x << " = {";                                    \
  for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge))                  \
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \
  cerr << "}" << '\n'
#define debugMatrix(x, h, w)                                                   \
  cerr << __LINE__ << ": " << #x << " =\n";                                    \
  for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) {                \
    cerr << ((hoge ? " {" : "{{"));                                            \
    for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga))                \
      cerr << ((fuga ? ", " : "")) << x[hoge][fuga];                           \
    cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n';           \
  }
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N, A, B;
  cin >> N >> A >> B;
  if (!(__builtin_popcount(A ^ B) & 1)) {
    cout << "NO" << '\n';
    return 0;
  }
  auto f = [](int x, int i) {
    return (x >> (i + 1)) * (1 << i) ^ (x % (1 << i));
  };
  auto g = [](int x, int i, bool b) {
    return (x >> (i)) * (1 << (i + 1)) ^ (b << i) ^ (x % (1 << i));
  };
  function<vector<int>(int, int, int)> rec = [&](int n, int a, int b) {
    if (n == 1)
      return vector<int>({a, b});
    vector<int> ret(1 << n);
    for (int i = 0; i < n; i++) {
      if (((a ^ b) >> i) & 1) {
        int aa = f(a, i);
        int bb = f(b, i);
        auto l = rec(n - 1, aa, aa ^ 1);
        auto r = rec(n - 1, aa ^ 1, bb);
        for (int j = 0; j < (1 << (n - 1)); j++) {
          ret[j] = g(l[j], i, (a >> i) & 1);
          ret[j + (1 << (n - 1))] = g(r[j], i, (b >> i) & 1);
        }
        break;
      }
    }
    return ret;
  };
  auto ans = rec(N, A, B);
  cout << "YES" << '\n';
  for (int i = 0; i < (1 << N); i++)
    cout << (i ? " " : "") << ans[i];
  cout << '\n';
  return 0;
}