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 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <stdio.h>

int isOneBitDiff(int a, int b)
{
	int xorAB = a^b;
	
	if (xorAB & (xorAB - 1)) {
		return 1;
	}

	return 0;
}

int bitCount(unsigned int bits) {
	bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
	bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
	bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
	bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);   
	return (bits & 0x0000ffff) + (bits >> 16);
}

int gray(int n)
{
	return n ^ (n>>1);
}

int getbit(int i)
{
	return i & -i;
}

int main(int argc, char *argv[])
{
	int n, a, b;
	
	scanf("%d %d %d", &n, &a, &b);

	if (isOneBitDiff(a, b)) {
		printf("NO\n");
		return 0;
	}

	printf("YES\n");

	int a_bits = bitCount(a);
	int b_bits = bitCount(b);

	int range = 1<<n;

	int end = range - 1;
	int last_bits = bitCount(gray(end));
	int now_bits;
	int start = 0;

	for (int i = 0; i < range; i++) {
		now_bits = bitCount(gray(i));
		if (last_bits == b_bits && now_bits == a_bits) {
			start = i;
			break;
		}
		last_bits = now_bits;
		end = i;
	}
	
	int gstart = gray(start);
	int gend = gray(end);

	int from_both_one = a & b;
	int from_both_zero = ~(a | b);
	int from_change_bit = a^b;

	int to_both_one = gstart & gend;
	int to_both_zero = ~(gstart | gend);
	int to_change_bit = gstart^gend;

	int map[18];
	
	for (int i = 0; i < n; i++) {
		int bits = 1<<i;
		if (from_change_bit & bits) {
			map[i] = to_change_bit;
		}
		if (from_both_one & bits) {
			int b = getbit(to_both_one);
			map[i] = b;
			to_both_one ^= b;
		}
		if (from_both_zero & bits) {
			int b = getbit(to_both_zero);
			map[i] = b;
			to_both_zero ^= b;
		}
	}


	int j = start;
	for (int i = 0; i < range; i++) {
		int g = gray(j);
		int rg = 0;
		for (int k = 0; k < n; k++) {
			if ((1<<k) & g) {
				rg += map[k];
			}
		}
		printf("%d", rg);
		if (i < range - 1) {putchar(' ');}
		j = (j + 1) % range;
	}
	
	printf("\n");
	return 0;
}