Problem p03097 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
#include <vector>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE, LIE)                                                     \
  for (auto LIE = CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0) ? -(x) : (x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
// palindromic tree is better than splay tree!
#define lld I64d
#endif

int popcnt(int x) {
  int ret = 0;
  for (int i = 0; i < 17; i++)
    ret += (x >> i) & 1;
  return ret;
}

void build(int mask, int A, int B, int mask_val, int l, int r,
           vector<int> &ans) {
  if (l + 1 == r) { // A == B
    ans[l] = mask_val;
    return;
  }
  int dif = A ^ B, c = (l + r) / 2;
  for (int i = 0; i < 17; i++)
    if ((dif >> i) & 1) {
      if (mask == (1 << i)) {
        build(0, 0, 0, A | mask_val, l, c, ans);
        build(0, 0, 0, B | mask_val, c, r, ans);
        break;
      }
      for (int j = 0; j < 17; j++)
        if (j != i)
          if ((mask >> j) & 1) {
            int C = (A - (A & (1 << i))) ^ (1 << j);
            build(mask - (1 << i), A - (A & (1 << i)), C,
                  mask_val | (A & (1 << i)), l, c, ans);
            build(mask - (1 << i), C, B - (B & (1 << i)),
                  mask_val | (B & (1 << i)), c, r, ans);
          }
      break;
    }
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  int N, A, B;
  cin >> N >> A >> B;
  if (popcnt(A ^ B) % 2 == 0) {
    cout << "NO\n";
    return 0;
  }
  vector<int> ans(1 << N);
  build((1 << N) - 1, A, B, 0, 0, 1 << N, ans);
  cout << "YES\n";
  for (int i = 0; i < (1 << N); i++)
    cout << ans[i] << ((i == (1 << N) - 1) ? "\n" : " ");
  return 0;
}

// look at my code
// my code is amazing


Fixed submission:
#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
#include <vector>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE, LIE)                                                     \
  for (auto LIE = CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0) ? -(x) : (x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
// palindromic tree is better than splay tree!
#define lld I64d
#endif

int popcnt(int x) {
  int ret = 0;
  for (int i = 0; i < 17; i++)
    ret += (x >> i) & 1;
  return ret;
}

void build(int mask, int A, int B, int mask_val, int l, int r,
           vector<int> &ans) {
  if (l + 1 == r) { // A == B
    ans[l] = mask_val;
    return;
  }
  int dif = A ^ B, c = (l + r) / 2;
  for (int i = 0; i < 17; i++)
    if ((dif >> i) & 1) {
      if (mask == (1 << i)) {
        build(0, 0, 0, A | mask_val, l, c, ans);
        build(0, 0, 0, B | mask_val, c, r, ans);
        break;
      }
      for (int j = 0; j < 17; j++)
        if (j != i)
          if ((mask >> j) & 1) {
            int C = (A - (A & (1 << i))) ^ (1 << j);
            build(mask - (1 << i), A - (A & (1 << i)), C,
                  mask_val | (A & (1 << i)), l, c, ans);
            build(mask - (1 << i), C, B - (B & (1 << i)),
                  mask_val | (B & (1 << i)), c, r, ans);
            break;
          }
      break;
    }
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  int N, A, B;
  cin >> N >> A >> B;
  if (popcnt(A ^ B) % 2 == 0) {
    cout << "NO\n";
    return 0;
  }
  vector<int> ans(1 << N);
  build((1 << N) - 1, A, B, 0, 0, 1 << N, ans);
  cout << "YES\n";
  for (int i = 0; i < (1 << N); i++)
    cout << ans[i] << ((i == (1 << N) - 1) ? "\n" : " ");
  return 0;
}

// look at my code
// my code is amazing