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 <vector>
#include <iostream>
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	int N, A, B;
	cin >> N >> A >> B;
	int diff = 0;
	for (int i = 0; i < N; ++i) {
		if (((A >> i) & 1) != ((B >> i) & 1)) {
			++diff;
		}
	}
	if (diff % 2 == 0) {
		cout << "NO" << '\n';
	}
	else {
		vector<int> way, cnt(N);
		cnt[0] = 1;
		for (int i = 0; i < 2 * N - 2; ++i) {
			if (i % 2 == 0) {
				way.push_back(i / 2 + 1);
				cnt[i / 2 + 1] ^= 1;
			}
			else {
				int pos = -1;
				for (int j = 0; j <= i / 2; ++j) {
					if ((j >= N - diff && cnt[j] == 0) || (j < N - diff && cnt[j] == 1)) {
						pos = j;
						break;
					}
				}
				if (pos != -1) {
					way.push_back(pos);
					cnt[pos] ^= 1;
				}
				else {
					way.push_back(0);
					cnt[0] ^= 1;
				}
			}
		}
		vector<int> seq = { 0 }, burger = { 0 };
		for (int i = 0; i < 2 * N - 2; ++i) {
			if (i % 2 == 0) {
				seq.push_back(i / 2 + 1);
				burger.push_back(i / 2 + 1);
				int sz = int(burger.size());
				for (int j = 0; j < sz - 1; ++j) {
					burger.push_back(burger[j]);
				}
			}
			else {
				int sz = int(seq.size());
				for (int j = 0; j < sz - 1; ++j) {
					seq.push_back((burger[j] + way[i] + 1) % (i / 2 + 1));
				}
			}
		}
		vector<int> perm;
		for (int i = 0; i < N; ++i) {
			if (((A ^ B) >> i) & 1) {
				perm.insert(perm.end(), i);
			}
			else {
				perm.insert(perm.begin(), i);
			}
		}
		vector<int> ans = { A };
		for (int i = 0; i < int(seq.size()); ++i) {
			ans.push_back(ans.back() ^ (1 << perm[seq[i]]));
		}
		cout << "YES" << '\n';
		for (int i : ans) {
			cout << i << ' ';
		}
	}
	return 0;
}