Problem:
Score : 800 points

Problem Statement

You are given integers N,\ A and B.
Determine if there exists a permutation (P_0,\ P_1,\ ...\ P_{2^N-1}) of (0,\ 1,\ ...\ 2^N-1) that satisfies all of the following conditions, and create one such permutation if it exists.

P_0=A

P_{2^N-1}=B

For all 0 \leq i < 2^N-1, the binary representations of P_i and P_{i+1} differ by exactly one bit.

Constraints

1 \leq N \leq 17

0 \leq A \leq 2^N-1

0 \leq B \leq 2^N-1

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

If there is no permutation that satisfies the conditions, print NO.

If there is such a permutation, print YES in the first line.
Then, print (P_0,\ P_1,\ ...\ P_{2^N-1}) in the second line, with spaces in between.
If there are multiple solutions, any of them is accepted.

Sample Input 1

2 1 3

Sample Output 1

YES
1 0 2 3

The binary representation of P=(1,0,2,3) is (01,00,10,11), where any two adjacent elements differ by exactly one bit.

Sample Input 2

3 2 1

Sample Output 2

NO

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>
#include<vector>
#include<math.h>
#include<string.h>
using namespace std;

vector<bool> perm[131075];
int N, A, B;
int len;
vector<bool> getbin(int x){
    vector<bool> buff;
    while(x!=0){
        buff.push_back(x%2);
        x /= 2;
    }
    for (int i=0;i<N-buff.size();i++){
        buff.push_back(0);
    }
    return buff;
}
int getval(vector<bool> buff){
    int t = 1, ret = 0;
    for (int i=0;i<buff.size();i++){
        ret += (int)buff[i] * t;
        t = t << 1;
    }
    return ret;
}

int find(int i, int j, int start){
    for (int k=start;k<N;k++){
        if (perm[i][k]!=perm[j][k]) return k;
    }
    return -1;
}

void fill(int i, int j, int start){
    if (start==N-1) return;
    if (i==j) return;
    if (i>j) return;
    if (start<0 || start>=N){
        cout << -1 << endl;
        exit(0);        
    }
    if (i<0 || i>=len) {
        cout << -1 << endl;
        exit(0);
    }
    if (j<0 || j>=len) {
        cout << -1 << endl;
        exit(0);
    }
    int pos = find(i, j, start);
    if (pos<0 || pos >= N){
        cout << -1 << endl;
        exit(0);
    }

    // cout << "pos: " << pos << endl;
    //swap pos row and start row
    bool temp = perm[i][start];
    perm[i][start] =  perm[i][pos];
    perm[i][pos] = temp;
    temp = perm[j][start];
    perm[j][start] =  perm[j][pos];
    perm[j][pos] = temp;
    // Now first row is different
    int mid = (i+j)/2;
    // cout << "mid: " << mid << endl;
    for(int k=i;k<=mid;k++){
        perm[k][start] = perm[i][start];
    }
    for(int k=j;k>mid;k--){
        perm[k][start] = perm[j][start];   
    }
    // Copy the rest and alter the next leading bit 
    if (start+1<N && mid+1<j){
        for (int k=start+1;k<N;k++){
            perm[mid][k] = perm[i][k];
            perm[mid+1][k] = perm[i][k];
        }
        perm[mid][start+1] = !perm[mid][start+1];
        perm[mid+1][start+1] = !perm[mid+1][start+1];        
    }
    fill(i, mid, start+1);
    fill(mid+1, j, start+1);
    // cout << "CHECK1" << endl;
    // Swap back
    for (int k=i;k<=j;k++){
        temp = perm[k][start];
        perm[k][start] =  perm[k][pos];
        perm[k][pos] = temp;
    }
}

int main(void){
    cin >> N;
    len = (int) pow(2, N);
    for (int i=0;i<len;i++){
        perm[i] = vector<bool>(N);
        for(int j=0;j<N;j++){
            perm[i][j] = 0;
        }
    }
    cin >> A >> B;
    perm[0] = getbin(A), perm[len-1] = getbin(B);  
    int cnt = 0;
    for (int i=0;i<N;i++){
        cnt += (perm[0][i]!=perm[len-1][i]);
    }
    if (cnt%2==0) {
        cout << "NO" << endl;
        return 0;
    }
    cout << "YES" << endl;
    if (N==1){
        cout << A << " " << B << endl;
        return 0;
    }
    // cout << -1 << endl;
    // exit(0);
    fill(0, len-1, 0);
    for (int i=0;i<len;i++){
        cout << getval(perm[i]) << " ";
    }
    cout << endl;
}