Problem p03097 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long LL;
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \
  int(X);                                                                      \
  scanf("%d", &X)
#define DRII(X, Y)                                                             \
  int X, Y;                                                                    \
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \
  int X, Y, Z;                                                                 \
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int, int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int, int>>
#define PLL pair<long long, long long>
#define VPLL vector<pair<long long, long long>>
#define F first
#define S second

using namespace std;

VI difs(0);
VI same(0);

VI gendiff(int n) {
  if (n == 1) {
    VI v(2, 0);
    v[1] = 1 << difs[0];
    return v;
  }
  VI base = gendiff(n - 2);
  int sb = base.size();
  int st = 0;
  VI res(0);
  int j = 0;
  int nn = 1 << difs[n - 1];
  int nn2 = 1 << difs[n - 2];
  REP(i, sb) { res.PB(base[i]); }
  REP(i, sb) { res.PB(base[sb - 1 - i] + nn); }
  VI t = {nn + nn2, nn2};
  int last = 0;
  while (j < sb) {
    res.PB(base[j] + t[st]);
    if ((st + 1) % 2 == last) {
      j++;
      last = st;
    } else {
      st++;
      st %= 2;
    }
  }
  return res;
}

VI gensame(int n) {
  if (n == 1) {
    VI v(2, 0);
    v[1] = (1 << same[0]);
    return v;
  }
  VI base = gensame(n - 1);
  int sb = base.size();
  int st = 0;
  VI res(0);
  int j = 0;
  int nn = 1 << same[n - 1];
  REP(i, sb) { res.PB(base[i]); }
  REP(i, sb) { res.PB(base[sb - 1 - i] + nn); }
  return res;
}

int main() {
  DRIII(n, a, b);
  int na = a;
  int nb = b;
  int kk = 0;
  while (kk < n) {
    if (na % 2 != nb % 2) {
      difs.PB(kk);
    } else {
      same.PB(kk);
    }
    na >>= 1;
    nb >>= 1;
    kk++;
  }
  /*
  for (auto i:difs) {cout<<i<<" ";} cout<<endl;
  for (auto i:same) {cout<<i<<" ";} cout<<endl;
  for (auto i:gensame(same.size())) {cout<<i<<" ";} cout<<endl;
  for (auto i:gendiff(difs.size())) {cout<<i<<" ";} cout<<endl;*/
  if (difs.size() % 2) {
    printf("YES\n");
    VI ss = gensame(same.size());
    VI sd = gendiff(difs.size());
    VI res;
    REP(i, sd.size()) {
      if (i % 2) {
        for (int j = ss.size() - 1; j >= 0; j--) {
          res.PB((ss[j] + sd[i]) ^ a);
        }
      } else {
        REP(j, ss.size()) { res.PB((ss[j] + sd[i]) ^ a); }
      }
    }
    REP(i, res.size()) {
      printf("%d%c", res[i], i == res.size() - 1 ? '\n' : ' ');
    }
  } else {
    printf("NO\n");
  }
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long LL;
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \
  int(X);                                                                      \
  scanf("%d", &X)
#define DRII(X, Y)                                                             \
  int X, Y;                                                                    \
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \
  int X, Y, Z;                                                                 \
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int, int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int, int>>
#define PLL pair<long long, long long>
#define VPLL vector<pair<long long, long long>>
#define F first
#define S second

using namespace std;

VI difs(0);
VI same(0);

VI gendiff(int n) {
  if (n == 1) {
    VI v(2, 0);
    v[1] = 1 << difs[0];
    return v;
  }
  VI base = gendiff(n - 2);
  int sb = base.size();
  int st = 0;
  VI res(0);
  int j = 0;
  int nn = 1 << difs[n - 1];
  int nn2 = 1 << difs[n - 2];
  REP(i, sb) { res.PB(base[i]); }
  REP(i, sb) { res.PB(base[sb - 1 - i] + nn); }
  VI t = {nn + nn2, nn2};
  int last = 0;
  while (j < sb) {
    res.PB(base[j] + t[st]);
    if ((st + 1) % 2 == last) {
      j++;
      last = st;
    } else {
      st++;
      st %= 2;
    }
  }
  return res;
}

VI gensame(int n) {
  if (n == 0) {
    VI v(1, 0);
    return v;
  }
  if (n == 1) {
    VI v(2, 0);
    v[1] = (1 << same[0]);
    return v;
  }
  VI base = gensame(n - 1);
  int sb = base.size();
  int st = 0;
  VI res(0);
  int j = 0;
  int nn = 1 << same[n - 1];
  REP(i, sb) { res.PB(base[i]); }
  REP(i, sb) { res.PB(base[sb - 1 - i] + nn); }
  return res;
}

int main() {
  DRIII(n, a, b);
  int na = a;
  int nb = b;
  int kk = 0;
  while (kk < n) {
    if (na % 2 != nb % 2) {
      difs.PB(kk);
    } else {
      same.PB(kk);
    }
    na >>= 1;
    nb >>= 1;
    kk++;
  }
  /*
  for (auto i:difs) {cout<<i<<" ";} cout<<endl;
  for (auto i:same) {cout<<i<<" ";} cout<<endl;
  for (auto i:gensame(same.size())) {cout<<i<<" ";} cout<<endl;
  for (auto i:gendiff(difs.size())) {cout<<i<<" ";} cout<<endl;*/
  if (difs.size() % 2) {
    printf("YES\n");
    VI ss = gensame(same.size());
    VI sd = gendiff(difs.size());
    VI res;
    REP(i, sd.size()) {
      if (i % 2) {
        for (int j = ss.size() - 1; j >= 0; j--) {
          res.PB((ss[j] + sd[i]) ^ a);
        }
      } else {
        REP(j, ss.size()) { res.PB((ss[j] + sd[i]) ^ a); }
      }
    }
    REP(i, res.size()) {
      printf("%d%c", res[i], i == res.size() - 1 ? '\n' : ' ');
    }
  } else {
    printf("NO\n");
  }
}