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 (Python):




def trace(bits, idx, history):
  if idx == len(bits):
    history.append(bits[:])
  elif idx == len(bits) - 1:
    history.append(bits[:])
    bits[idx] = 1 - bits[idx]
    history.append(bits[:])
  else:
    trace(bits, idx + 1, history)
    bits[idx] = 1 - bits[idx]
    trace(bits, idx + 1, history)


def down(bits, idx, count, history):
  if idx == len(bits):
    history.append(bits[:])
  elif idx == len(bits) - 1:
    history.append(bits[:])
    bits[idx] = 1 - bits[idx]
    history.append(bits[:])
  elif count > 0:
    trace(bits, idx + 2, history)
    bits[idx] = 1 - bits[idx]
    trace(bits, idx + 2, history)
    bits[idx + 1] = 1 - bits[idx + 1]
    trace(bits, idx + 2, history)
    bits[idx] = 1 - bits[idx]
    down(bits, idx + 2, count - 2, history)
  else:
    trace(bits, idx + 2, history)
    bits[idx + 1] = 1 - bits[idx + 1]
    trace(bits, idx + 2, history)
    bits[idx ] = 1 - bits[idx ]
    trace(bits, idx + 2, history)
    bits[idx + 1] = 1 - bits[idx + 1]
    down(bits, idx + 2, count - 2, history)


def main():
  n, a, b = map(int, input().split())
  x = a ^ b
  count = 0
  remain = 0
  mapper = [-1] * n

  for i in range(n):
    if (x >> i) & 0x1 != 0:
      mapper[count] = i
      count += 1
    else:
      remain += 1
      mapper[-remain] = i

  if count % 2 == 0:
    print('NO', flush=True)
    return

  bits = [0] * n
  history = []
  trace(bits, 1, history)
  bits[0] = 1
  down(bits, 1, count - 1, history)

  for i in range(len(history)):
    v = history[i][:]

    for j, m in enumerate(mapper):
      history[i][n - m - 1] = v[j]

  history = [str(a ^ int(''.join(str(v) for v in h), 2)) for h in history]

  print('YES', flush=True)
  print(' '.join(history), flush=True)


if __name__ == '__main__':
  main()

Solution (Python):


import sys

def sum_digits(n,m):
   r = 0
   while n:
       r, n = r + n % m, n // m
   return r


n,a,b = [int(i) for i in input().split()]

c=a^b
if sum_digits(c,2)%2 == 0:
   print('NO') 
   sys.exit()

def grey(n):
    if n == 1: return [0,1]
    a0 = grey(n-1)
    a2= [(2**(n-2))^i^(2**(n-1)) for i in grey(n-1)] 
    return a0 + a2
    

def newgrey(a,b,move):
    if len(move) == 1:
        return [a,b]
    diff = 0
    for j in move:
        if a&(2**j) != b&(2**j):
            diff=j
            break
    ac = a
    bc = b
    for j in move:
        if j == diff: continue
        ac = a^(2**j)
        bc = ac ^ (2**diff)
        if bc != b: break
    return newgrey(a,ac, move-set({diff})) + newgrey(bc,b, move-set({diff}))
    
print('YES')
ans =newgrey(a,b,set(range(n)))
print(*ans)

Solution (Python):
N,A,B = map(int,input().split())
M = 2**(N-1)

if N==1:
    print('YES')
    print(A,B)
    exit()

gray = [0]
for i in range(1,M):
    gray.append(gray[-1] ^ (i&(-i)))
grid = [gray, [a+M for a in gray]]

for i,row in enumerate(grid):
    if A in row:
        sx,sy = row.index(A),i
    if B in row:
        gx,gy = row.index(B),i

if (abs(sx-gx) + abs(sy-gy)) % 2:
    print('YES')
else:
    print('NO')
    exit()

ans = [A]

x,y = sx,sy
while (gx-x)%M != 1:
    x += 1
    x %= M
    ans.append(grid[y][x])
y = 1-y
ans.append(grid[y][x])
while x != sx:
    x -= 1
    x %= M
    ans.append(grid[y][x])

if x != gx:
    while 1:
        x -= 1
        x %= M
        ans.append(grid[y][x])
        y = 1-y
        ans.append(grid[y][x])
        if x==gx: break

print(*ans)

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def func(A, B, bit_len):
    if bit_len == 1:
        return [A, B]
    X = A ^ B
    for i in range(bit_len):
        if (X >> i) & 0x01 >= 1:
            break
    del_bit = i
    A_prime = ((A >> (del_bit + 1)) << del_bit) + (A & ((2 ** del_bit) - 1))
    B_prime = ((B >> (del_bit + 1)) << del_bit) + (B & ((2 ** del_bit) - 1))
    C = A_prime ^ 0x01
    retA = func(A_prime, C, bit_len - 1)
    retB = func(C, B_prime, bit_len - 1)
    ret = [0 for i in range(len(retA) + len(retB))]
    for i in range(len(retA)):
        ret[i] = (retA[i] & ((2 ** del_bit) - 1)) + (A & (2 ** del_bit)) + ((retA[i] & (~((2 ** del_bit) - 1))) << 1)
    for i in range(len(retB)):
        ret[len(retA) + i] = (retB[i] & ((2 ** del_bit) - 1)) + (B & (2 ** del_bit)) + ((retB[i] & (~((2 ** del_bit) - 1))) << 1)

    return ret




def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, A, B] = [int(input_raw[0]), long(input_raw[1]), long(input_raw[2])]
    countA = 0
    countB = 0
    for i in range(N):
        countA += (A >> i) & 0x01
    for i in range(N):
        countB += (B >> i) & 0x01
    if countA % 2 == countB % 2:
        print "NO"
        return
    ret = func(A, B, N)
    print "YES"
    for i in range(len(ret)):
        print ret[i],
    print













































if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def func(A, B, bit_len):
    if bit_len == 1:
        return [A, B]
    X = A ^ B
    for i in range(bit_len):
        if (X >> i) & 0x01 >= 1:
            break
    del_bit = i
    A_prime = ((A >> (del_bit + 1)) << del_bit) + (A & ((2 ** del_bit) - 1))
    B_prime = ((B >> (del_bit + 1)) << del_bit) + (B & ((2 ** del_bit) - 1))
    C = A_prime ^ 0x01
    retA = func(A_prime, C, bit_len - 1)
    retB = func(C, B_prime, bit_len - 1)
    ret = [0 for i in range(len(retA) + len(retB))]
    for i in range(len(retA)):
        ret[i] = (retA[i] & ((2 ** del_bit) - 1)) + (A & (2 ** del_bit)) + ((retA[i] & (~((2 ** del_bit) - 1))) << 1)
    for i in range(len(retB)):
        ret[len(retA) + i] = (retB[i] & ((2 ** del_bit) - 1)) + (B & (2 ** del_bit)) + ((retB[i] & (~((2 ** del_bit) - 1))) << 1)

    return ret




def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, A, B] = [int(input_raw[0]), long(input_raw[1]), long(input_raw[2])]
    countA = 0
    countB = 0
    for i in range(N):
        countA += (A >> i) & 0x01
    for i in range(N):
        countB += (B >> i) & 0x01
    if countA % 2 == countB % 2:
        print "NO"
        return
    ret = func(A, B, N)
    print "YES"
    for i in range(len(ret)):
        print ret[i],
    print













































if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

n, a, b = map(int, input().split())
def pop_count(x):
	res = 0
	for i in range(17):
		res += ((x>>i)%2 == 1)
	return res
if (pop_count(a^b)) % 2 == 0:
	print("NO")
else:
	print("YES")
	def f(m, x, y):
		if m == 1:
			if x == 0:
				return [0, 1]
			else:
				return [1, 0]

		res = [x]
		for i in range(m):
			if (x>>i) % 2  != (y>>i) % 2:
				diff = i
				break
		s = (x % (2**diff)) + (((x // (2**(diff+1))) * 2**(diff+1)) >> 1)
		t = (y % (2**diff)) + (((y // (2**(diff+1))) * 2**(diff+1)) >> 1)
		
		u = s^1
		left = f(m-1, s, u)
		right = f(m-1, u, t)
		
		x_bit, y_bit = (x>>diff) % 2, (y>>diff) % 2
		left = [l % (2**diff) + (2**diff * x_bit) + (((l // (2**diff)) * 2**diff) << 1) for l in left]
		right = [r % (2**diff) + (2**diff * y_bit) + (((r // (2**diff)) * 2**diff) << 1) for r in right]
		return left + right
	print(*f(n, a, b))

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

n, a, b = map(int, input().split())
def pop_count(x):
	res = 0
	for i in range(17):
		res += ((x>>i)%2 == 1)
	return res
if (pop_count(a^b)) % 2 == 0:
	print("NO")
else:
	print("YES")
	def f(m, x, y):
		if m == 1:
			if x == 0:
				return [0, 1]
			else:
				return [1, 0]

		res = [x]
		for i in range(m):
			if (x>>i) % 2  != (y>>i) % 2:
				diff = i
				break
		s = (x % (2**diff)) + (((x // (2**(diff+1))) * 2**(diff+1)) >> 1)
		t = (y % (2**diff)) + (((y // (2**(diff+1))) * 2**(diff+1)) >> 1)
		
		u = s^1
		left = f(m-1, s, u)
		right = f(m-1, u, t)
		
		x_bit, y_bit = (x>>diff) % 2, (y>>diff) % 2
		left = [l % (2**diff) + (2**diff * x_bit) + (((l // (2**diff)) * 2**diff) << 1) for l in left]
		right = [r % (2**diff) + (2**diff * y_bit) + (((r // (2**diff)) * 2**diff) << 1) for r in right]
		return left + right
	print(*f(n, a, b))

Solution (Python):
import sys
DBG = not True
DBG2 = not True
n,a,b =      map(int, input().split())
MAXM =  18
r = [0] * MAXM
s = [0] * MAXM
r[0] = [0]
r[1] = [0,1]
s[1] = [0,1]
p1a = [0,1,3,2]
p2a = [3,1,0,2,2,0,1,3]

def swiz(n,z,d):
    xpos = n - len(d)
    ypos = 0
    t = 0
    for i in range(n):
        if DBG2:
            print("sw lp i {} xp {} yp {} t {}".\
               format(i,xpos,ypos,t))
        if i in d:
            t += ((z >> xpos) & 1) << i
            xpos += 1
        else:
            t += ((z >> ypos) & 1) << i
            ypos += 1
    if  DBG2:
        print("sw z {} d {} ret {}".format(z,d,t))
    return t

for m in range(3,MAXM,2):
    s[m] = [0] * (2**m)
    for i in range(2**(m-2)):
        hi = s[m-2][i] << 2
        for j in range(4):
          s[m][4*i+j] = hi + \
            (p1a[j] if i == 0 else p2a[4*(i%2)+j])
          if DBG2:
              print("set s m {} i {} j {} - {}".\
                 format(m,i,j,s[m][4*i+j]))

for m in range(2,MAXM):
    r[m] = [0] * (2**m)
    for i in range(2**(m-1)):
        r[m][i  ] = r[m-1][i]
        r[m][2**m-1-i] = 2**(m-1) + r[m-1][i]

if DBG:
    for m in range(1,6):
        print(s[m])
    print("")
    for m in range(1,6):
        print(r[m])

z = a ^ b
cnt = 0
d = []
for i in range(MAXM):
    if z & (1<<i) != 0:
        cnt += 1
        d.append(i)
if cnt%2 == 0:
    print('NO')
    sys.exit(0)
else:
    print('YES')

sz = len(d)
if DBG:
    print("n {} a {} b {} sz {} d:".format(n,a,b,sz))
    print(d)

for i in range(2**sz):
    for j in range(2**(n-sz)):
        hi = (s[sz][i] << (n-sz))
        lo =  r[n-sz][j] if i%2 == 0 \
                         else r[n-sz][2**(n-sz)-1-j]
        z =  hi + lo
        sw = swiz(n,z,d)
        if DBG:
          print("i {} j {} hi {} lo {} z {} sw {}".\
               format(i,j,hi,lo,z,sw))
        sys.stdout.write(str(a^sw) + ' ')
print('')

Solution (Python):
import sys

N,A,B=map(int,input().split())

BIT=0
for i in range(N):
    if (1<<i) & A!=(1<<i) & B:
        BIT+=1

if BIT%2==1:
    print("YES")
else:
    print("NO")
    sys.exit()

check=[0]*(1<<N)

ANS=[]
check[A]=1
check[B]=1

def route(x,i,j):
    NOW=x
    ANS.append(NOW)
    check[NOW]=1

    for k in range(1<<j):
        for l in range(N):
            if check[NOW ^ (1<<l)]==0 and l!=i:
                NOW=NOW ^ (1<<l)
                check[NOW]=1
                ANS.append(NOW)
                break

    return NOW^(1<<i)

checkbit=[0]*N     
for i in range(N-1,-1,-1):
    for j in range(N):
        if (1<<j)&A!=(1<<j)&B and checkbit[j]==0:
            break
    A=route(A,j,i)
    checkbit[j]=1

ANS.append(B)
print(*ANS)

Solution (Python):
import sys

N,A,B=map(int,input().split())

BIT=0
for i in range(N):
    if (1<<i) & A!=(1<<i) & B:
        BIT+=1

if BIT%2==1:
    print("YES")
else:
    print("NO")
    sys.exit()

check=[0]*(1<<N)

ANS=[]
check[A]=1
check[B]=1

def route(x,i,j):
    NOW=x
    ANS.append(NOW)
    check[NOW]=1

    for k in range(1<<j):
        for l in range(N):
            if check[NOW ^ (1<<l)]==0 and l!=i:
                NOW=NOW ^ (1<<l)
                check[NOW]=1
                ANS.append(NOW)
                break

    return NOW^(1<<i)

checkbit=[0]*N     
for i in range(N-1,-1,-1):
    for j in range(N):
        if (1<<j)&A!=(1<<j)&B and checkbit[j]==0:
            break
    A=route(A,j,i)
    checkbit[j]=1

ANS.append(B)
print(*ANS)

Solution (Python):
import sys

def revBit(Ai):
    return '1' if Ai == '0' else '0'

def rec(binA, binB):
    if len(binA) == 1:
        return [binA, binB]

    for i, (Ai, Bi) in enumerate(zip(binA, binB)):
        if Ai != Bi:
            x = i
            Ax, Bx = Ai, Bi
            break

    binA2 = binA[:x] + binA[x+1:]
    binB2 = binB[:x] + binB[x+1:]
    binC = binA2[:-1] + revBit(binA2[-1])

    ptn1s = rec(binA2, binC)
    ptn2s = rec(binC, binB2)

    ptns = []
    for ptn1 in ptn1s:
        ptns.append(ptn1[:x] + Ax + ptn1[x:])
    for ptn2 in ptn2s:
        ptns.append(ptn2[:x] + Bx + ptn2[x:])

    return ptns


N, A, B = map(int, input().split())

binA, binB = bin(A)[2:].zfill(N), bin(B)[2:].zfill(N)
numDiff = sum([Ai != Bi for Ai, Bi in zip(binA, binB)])

if numDiff % 2 == 0:
    print('NO')
    sys.exit()

ptns = rec(binA, binB)
print('YES')
print(' '.join([str(int(ptn, 2)) for ptn in ptns]))

Solution (Python):
import sys

ptn00 = [['00', '01', '11', '10'], ['10', '11', '01', '00']]
ptn11 = [['00', '01', '11', '10'], ['10', '00', '01', '11']]
ptnEven = ['01', '10']



def dfs(digit, ptnNow, parity, isLast):
    if digit == maxDigit:
        if N % 2:
            ptns.append(ptnNow)
        else:
            
            ptns.append(ptnNow+ptnEven[parity][0])
            ptns.append(ptnNow+ptnEven[parity][1])
        return

    if digit <= numDiff//2 and parity and isLast:
        
        for v, p in enumerate(ptn11[1]):
            dfs(digit+1, ptnNow+p, v%2, isLast if v == 3 else 0)
    else:
        
        for v, p in enumerate(ptn00[parity]):
            dfs(digit+1, ptnNow+p, v%2, isLast if v == 3 else 0)


N, A, B = map(int, input().split())

binA, binB = bin(A)[2:].zfill(N), bin(B)[2:].zfill(N)
numDiff = sum([Ai != Bi for Ai, Bi in zip(binA, binB)])

if numDiff % 2 == 0:
    print('NO')
    sys.exit()

numSame = N - numDiff
maxDigit = 1 + numDiff//2 + numSame//2

ptns = []
dfs(1, '0', 0, False)
dfs(1, '1', 1, True)


conv2digitPtns = []
digitDiff, digitSame = 0, numDiff
for i, (Ai, Bi) in enumerate(zip(binA, binB)):
    if Ai == Bi:
        conv2digitPtns.append(digitSame)
        digitSame += 1
    else:
        conv2digitPtns.append(digitDiff)
        digitDiff += 1

anss = []
for ptn in ptns:
    ans = ''
    for c in conv2digitPtns:
        ans += ptn[c]
    ans = int(ans, 2) ^ A
    anss.append(ans)

print('YES')
print(' '.join(map(str, anss)))

Solution (Python):
def num_of_1bit(n: int)->int:
    '''n の立っているビットを数えます。
    :param n: number
    :return: number of 1 bit
    '''
    s = 0
    while n > 0:
        s += n & 1
        n >>= 1
    return s


def diff_idx(a: int, b: int)->int:
    '''a と b のビット列を比較してビットの状態が異なる最小の
    インデックスを返します。ここで、インデックスは 2^i の i 
    に対応する。
    :param a: number 1
    :param b: number 2
    :return: index
    '''
    if a == b:
        return -1
    idx = 0
    while a > 0 or b > 0:
        if a & 1 != b & 1:
            break
        idx += 1
        a, b = a >> 1, b >> 1
    return idx


def remove_bit(n: int, idx: int)->int:
    '''n の idx 番目のビットを消去します。idx は 2^idx に対
    応します。
    ex) n = 1011011, idx = 2
    -> 101111

    :param n: number
    :param idx: index
    :return: number removed `idx`th bit
    '''
    upper = (n >> (idx+1)) << idx
    lower = n & ((1 << idx) - 1)
    return upper | lower


def insert_bit(n: int, idx: int, bit: bool)->int:
    '''n の idx 番目にビットを挿入します。
    ex) n = 101111, idx = 2, bit = True
    -> 1011111
    '''
    upper = (n >> idx) << (idx + 1)
    lower = n & ((1 << idx) - 1)
    insert = bit << idx
    return upper | insert | lower


def route(_from: int, to: int, N: int)->list:
    '''_from から to に至る N ビットの数を全て使ったルートを
    計算します。
    :param _from: start
    :param to: end
    :param N: number of bit
    :return: routes
    '''
    if N == 1:
        return [_from, to]

    di = diff_idx(_from, to)

    a = remove_bit(_from, di)
    b = remove_bit(to, di)
    c = a ^ 1

    from_bit = (_from & (1 << di)) != 0
    to_bit = (to & (1 << di)) != 0

    first_half = map(lambda x: insert_bit(x, di, from_bit), route(a, c, N-1))
    second_half = map(lambda x: insert_bit(x, di, to_bit),  route(c, b, N-1))

    return list(first_half) + list(second_half)


def diff_by_1_bit(N: int, A: int, B: int)->list:
    num_A = num_of_1bit(A)
    num_B = num_of_1bit(B)
    if abs(num_A - num_B) % 2 == 0:
        return []

    return route(A, B, N)


if __name__ == "__main__":
    N, A, B = map(int, input().split())
    ans = diff_by_1_bit(N, A, B)
    if len(ans) == 0:
        print('NO')
    else:
        print('YES')
        print(' '.join(map(str, ans)))

Solution (Python):
def Double(vec):
	m=len(vec)
	res=[]
	for i in range(0,m,2):
		res.extend([vec[i],vec[i]^m,vec[i+1]^m,vec[i+1]])
	return res

def Greedy(n):
	m=1<<n
	res=[0]*m
	vis=[False]*m
	vis[0]=True
	for i in range(1,m):
		x=res[i-1]
		for j in range(n):
			if not vis[x^(1<<j)]:
				x^=1<<j
				break
		vis[x]=True
		res[i]=x
	return res

def Perfect(n):
	if n==1:
		return [0,1]
	m=1<<n
	A=Greedy(n-1)
	B=Double(Perfect(n-2))
	B.reverse()
	for i in range(m>>1):
		B[i]^=m-1
	A.extend(B)
	return A

n,a,b=map(int,input().split())
m=1<<n
c=bin(a^b).count('1')
if not c&1:
	print('NO')
	exit()
tmp=Perfect(c)
for i in range(c,n):
	tmp=Double(tmp)
p=0
q=c
ans=[0]*m
for i in range(n):
	if (a^b)>>i&1:
		for j in range(m):
			ans[j]^=(((tmp[j]>>p)^(a>>i))&1)<<i
		p+=1
	else:
		for j in range(m):
			ans[j]^=(((tmp[j]>>q)^(a>>i))&1)<<i
		q+=1
print('YES\n',' '.join(str(i) for i in ans),sep='')

Solution (Python):
n,a,b = [int(i) for i in input().split()]


def f(n,a,b):
    if n==1:
        return [a,b]

    mask = 1<<(n-1)

    
    if a & mask == b & mask:
        v1 = f(n-1, a, b)
        v2 = f(n-1, a^mask, v1[1]^mask)
        ret = [a]
        ret.extend(v2)
        ret.extend(v1[1:])
        return ret        
    else:
        
        v1 = f(n-1, a, a^1)
        v2 = f(n-1, a^1^mask, b)
        v1.extend(v2)
        return v1




if bin(a^b).count("1") %2==0:
    print("NO")
else:
    print("YES")
    res = f(n,a,b)
    for p in res:
        print(p, end=" ")

Solution (Python):
from sys import setrecursionlimit
setrecursionlimit(10 ** 9)

n, a, b = [int(i) for i in input().split()]

if bin(a ^ b).count('1') % 2 == 0:
    print('NO')
    exit()

def dfs(i, a, b):
    if i == 1:
        return [a, b]
    d = (a ^ b) & -(a ^ b)
    ad = ((a & (~d ^ d - 1)) >> 1) + (a & d - 1)
    bd = ((b & (~d ^ d - 1)) >> 1) + (b & d - 1)
    c = ad ^ 1

    res1 = dfs(i - 1, ad, c)
    res2 = dfs(i - 1, c, bd)
    ans1 = [((r & ~(d - 1)) << 1) + (r & d - 1) + (d & a) for r in res1]
    ans2 = [((r & ~(d - 1)) << 1) + (r & d - 1) + (d & b) for r in res2]
    return ans1 + ans2

print('YES')
print(*dfs(n, a, b))

Solution (Python):
from sys import setrecursionlimit
setrecursionlimit(10 ** 9)

n, a, b = [int(i) for i in input().split()]

if bin(a ^ b).count('1') % 2 == 0:
    print('NO')
    exit()

def dfs(i, a, b):
    if i == 1:
        return [a, b]
    d = (a ^ b) & -(a ^ b)
    ad = ((a & (~d ^ d - 1)) >> 1) + (a & d - 1)
    bd = ((b & (~d ^ d - 1)) >> 1) + (b & d - 1)
    c = ad ^ 1

    res1 = dfs(i - 1, ad, c)
    res2 = dfs(i - 1, bd, c)
    ans1 = [((r & ~(d - 1)) << 1) + (r & d - 1) + (d & a) for r in res1]
    ans2 = [((r & ~(d - 1)) << 1) + (r & d - 1) + (d & b) for r in res2[::-1]]
    return ans1 + ans2

print('YES')
print(*dfs(n, a, b))

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007


def A():
    n = I()
    s = S()
    d = defaultdict(int)
    for i in s:
        d[i] += 1
    ans = 1
    for i in d.keys():
        ans *= d[i]+1
        ans %= mod
    print(ans-1)

def B():
    n = I()
    c = IR(n)
    v = [[i+1] for i in range(n-1)]
    v.append([])
    d = defaultdict(deque)
    for i in range(n):
        d[c[i]].append(i)
    for i in d.keys():
        x = None
        while d[i]:
            y = d[i].popleft()
            if x != None and y != x+1:
                v[x].append(y)
            x = y
    path = [0]*(n)
    path[0] = 1
    q = [0]
    for x in range(n-1):
        for y in v[x]:
            path[y] += path[x]
            path[y] %= mod
    print(path[-1])

def C():
    def dfs(ans,l,r,d):
        if r != l+1:
            i = n-1
            c = ans[l]^ans[r]
            while not c&po2[i]:i -= 1
            d[i] = 1
            j = 0
            while d[j] or i == j:j += 1;j %= n
            m = (l+r)//2
            ans[m] = ans[l]^po2[j]
            ans[m+1] = ans[m]^po2[i]
            dfs(ans,l,m,d[:i]+[1]+d[i+1:])
            dfs(ans,m+1,r,d[:i]+[1]+d[i+1:])
    n,a,b = LI()
    po2 = [1]*(n+1)
    for i in range(1,n+1):
        po2[i] = po2[i-1]*2
    k = b^a
    c = 0
    bi = []
    i = 0
    g = [0]*n
    while k > 0:
        if k%2:
            bi.append(i)
            g[i] = 1
        c += k%2
        k//= 2
        i += 1
    f = c
    bi = bi[::-1]
    if not f%2:
        print("NO")
        quit()
    ans = [0]*po2[n]
    ans[0] = a
    ans[po2[n]-1] = b
    d = [0]*n
    dfs(ans,0,po2[n]-1,d)
    print("YES")
    print(*ans)

def D():
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == "__main__":
    C()

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007


def A():
    n = I()
    s = S()
    d = defaultdict(int)
    for i in s:
        d[i] += 1
    ans = 1
    for i in d.keys():
        ans *= d[i]+1
        ans %= mod
    print(ans-1)

def B():
    n = I()
    c = IR(n)
    v = [[i+1] for i in range(n-1)]
    v.append([])
    d = defaultdict(deque)
    for i in range(n):
        d[c[i]].append(i)
    for i in d.keys():
        x = None
        while d[i]:
            y = d[i].popleft()
            if x != None and y != x+1:
                v[x].append(y)
            x = y
    path = [0]*(n)
    path[0] = 1
    q = [0]
    for x in range(n-1):
        for y in v[x]:
            path[y] += path[x]
            path[y] %= mod
    print(path[-1])

def C():
    def dfs(ans,l,r,d):
        if r != l+1:
            i = n-1
            c = ans[l]^ans[r]
            while not c&po2[i]:i -= 1
            d[i] = 1
            j = 0
            while d[j] or i == j:j += 1;j %= n
            m = (l+r)//2
            ans[m] = ans[l]^po2[j]
            ans[m+1] = ans[m]^po2[i]
            dfs(ans,l,m,d[:i]+[1]+d[i+1:])
            dfs(ans,m+1,r,d[:i]+[1]+d[i+1:])
    n,a,b = LI()
    po2 = [1]*(n+1)
    for i in range(1,n+1):
        po2[i] = po2[i-1]*2
    k = b^a
    c = 0
    bi = []
    i = 0
    g = [0]*n
    while k > 0:
        if k%2:
            bi.append(i)
            g[i] = 1
        c += k%2
        k//= 2
        i += 1
    f = c
    bi = bi[::-1]
    if not f%2:
        print("NO")
        quit()
    ans = [0]*po2[n]
    ans[0] = a
    ans[po2[n]-1] = b
    d = [0]*n
    dfs(ans,0,po2[n]-1,d)
    print("YES")
    print(*ans)

def D():
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == "__main__":
    C()

Solution (Python):
n, a, b = [int(i) for i in input().split(" ")]

def p2l(x):
    l = []
    for i in range(0, n):
        l = [x % 2] + l
        x = x // 2
    return l

def l2p(x):
    return sum([x[i] * 2**(len(x) - i - 1) for i in range(0, len(x))])

al = p2l(a)
bl = p2l(b)

def gray(a, b):
    if len(a) == 1:
        return [a, b]
    
    for i in range(0, len(a)):
        if a[i] != b[i]:
            break
    ah = a[i]
    bh = b[i]
    del a[i]
    del b[i]
    
    c = [1 - a[0]] + a[1:]
    g1 = gray(a[:], c[:])
    g2 = gray(c[:], b[:])
    g1 = [h[0:i] + [ah] + h[i:] for h in g1]
    g2 = [h[0:i] + [bh] + h[i:] for h in g2]
    return g1 + g2

if sum(al) % 2 != sum(bl) % 2:
    print("YES")
    g = gray(al, bl)
    p = [l2p(gi) for gi in g]
    print(' '.join(map(str, p)))
else:
    print("NO")

Solution (Python):
n, a, b = map(int, raw_input().split())

def doit(aa, bb, mask):
    bits   = filter(lambda b: mask & (1 << b) == 0, range(n))
    differ = filter(lambda b: (aa ^ bb) & (1 << b), bits)[-1]
    change = filter(lambda b: differ != b, bits)
    if not change:
        return [aa, bb]
    ab = aa ^ (1 << change[0])
    ba = ab ^ (1 << differ)
    return doit(aa, ab, mask | (1 << differ)) + doit(ba, bb, mask | (1 << differ))

if bin(a ^ b).count('1') % 2 != 1:
    print "NO"
else:
    print "YES"
    ret = doit(a, b, 0)
    print " ".join(map(str, ret))

Solution (Python):
n, a, b = map(int, raw_input().split())

def doit(a, b, mask):
    differ = -1
    for i in range(n):
        if mask & (1 << i) == 0 and (a ^ b) & (1 << i) != 0:
            differ = i
    same = -1
    for i in range(n):
        if mask & (1 << i) == 0 and i != differ:
            same = i
    if same == -1:
        return [a, b]
    return (doit(a, a ^ (1 << same), mask | (1 << differ)) +
            doit(a ^ (1 << same) ^ (1 << differ), b, mask | (1 << differ)))

if bin(a ^ b).count('1') % 2 != 1:
    print "NO"
else:
    print "YES"
    ret = doit(a, b, 0)
    print " ".join(map(str, ret))

Solution (Python):
def split(n, a, b, remain, ai, ans, width, l=None):
    
    
    
    if width == 2:
        ans[ai] = a
        ans[ai + 1] = b
        return
    if l is None:
        x = a ^ b
        y = x & -x  
        l = y.bit_length() - 1
    else:
        y = 1 << l
    remain.remove(l)
    i = next(iter(remain))
    lb = a ^ (1 << i)
    ra = lb ^ y
    width >>= 1
    split(n, a, a ^ (1 << i), remain, ai, ans, width, i)
    split(n, ra, b, remain, ai + width, ans, width)
    remain.add(l)


def solve(n, a, b):
    if bin(a).count('1') % 2 == bin(b).count('1') % 2:
        print('NO')
        return
    remain = set(range(n))
    ans = [0] * (1 << n)
    split(n, a, b, remain, 0, ans, 1 << n)
    print('YES')
    print(*ans)


n, a, b = list(map(int, input().split()))
solve(n, a, b)

Solution (Python):
def split(n, a, b, remain, ai, ans, width):
    
    
    
    if width == 2:
        ans[ai] = a
        ans[ai + 1] = b
        return
    x = a ^ b
    y = x & -x  
    l = y.bit_length() - 1
    remain.remove(l)
    i = next(iter(remain))
    lb = a ^ (1 << i)
    ra = lb ^ y
    width >>= 1
    split(n, a, a ^ (1 << i), remain, ai, ans, width)
    split(n, ra, b, remain, ai + width, ans, width)
    remain.add(l)


def solve(n, a, b):
    if bin(a).count('1') % 2 == bin(b).count('1') % 2:
        print('NO')
        return
    remain = set(range(n))
    ans = [0] * (1 << n)
    split(n, a, b, remain, 0, ans, 1 << n)
    print('YES')
    print(*ans)


n, a, b = list(map(int, input().split()))
solve(n, a, b)

Solution (Python):
N, A, B = map(int, input().split())


def f(n, a, b):
    if n == 1:
        return [a, b]

    mask = 1 << (n-1)
    if a & mask == b & mask:
        v1 = f(n-1, a, b)
        v2 = f(n-1, a ^ mask, v1[1] ^ mask)
        ret = v1[:1]
        ret.extend(v2)
        ret.extend(v1[1:])
        return ret
    else:
        v1 = f(n-1, a, a ^ 1)
        v2 = f(n-1, a ^ 1 ^ mask, b)
        v1.extend(v2)
        return v1


if bin(A ^ B).count('1') % 2 == 0:
    print("NO")
else:
    print("YES")
    res = f(N, A, B)
    for p in res:
        print(p)

Solution (Python):
def int_raw():
    return int(raw_input())

def ss_raw():
    return raw_input().split()

def ints_raw():
    return map(int,ss_raw())

INF = 1<<29

DIV = 10**9+7

def make2d_arr(w,h,val =INF):
    return [[val]*w for i in xrange(h)]

def floyd_warshall(costs):
    N = len(costs)
    for k in xrange(N):
        for i in xrange(N):
            for j in xrange(N):
                costs[i][j]  = min(costs[i][j],costs[i][k]+costs[k][j])
    return costs

def findMinDiff(N,A,B,checked):
    for i in xrange(N):
        if checked[i]:
            continue
        if ((A ^ B)& (1<<i))!= 0:
            return i
    return -1


def findC(N,A,B,checked):
    
    now_chk = checked[:]
    m = findMinDiff(N,A,B,checked)
    if m ==-1:
        return [A]
    now_chk[m]= True
    exp_m = 1<<m
    for i in xrange(N):
        if not now_chk[i]:
            exp_i = (1<<i)
            C = A ^ exp_i
            fst = findC(N,A,C, now_chk)
            snd = findC(N,C,B, now_chk)
            snd = map(lambda x: (x&((1<<N)-1-exp_m)) + (B & exp_m),  snd)
            return fst+snd

    else:
        return [A,B]

N,A,B = ints_raw()

DIF_B = []
A2s = []
B2s = []
num_db =0

for i in xrange(N):
    a2 = (A>>i)%2
    b2 = (B>>i)%2
    A2s.append(a2)
    B2s.append(b2)
    if a2 == b2:
        DIF_B.append(0)    
    else:
        DIF_B.append(1)
        num_db+=1
    
numP = (2**N)-1
if (num_db > numP) or (num_db%2 ==0):
    print "NO"
else:
    print "YES"
    ans =  findC(N,A,B,[False]*N)
    for a in ans:
        print a,

Solution (Python):
def bit_remove(x,bit):
    return ((x>>(bit+1))<<bit)+(x%(1<<bit))
def bit_insert(x,bit,i):
    return ((x>>bit)<<(bit+1))+(i<<bit)+(x%(1<<bit))

def solve(n,a,b):
    if n==1:
        return [a,b]
    for bit in range(n):
        if (a^b)&(1<<bit):
            break
    c,d=bit_remove(a,bit),bit_remove(b,bit)
    L=list(map(lambda x: bit_insert(x,bit,(a>>bit)&1),solve(n-1,c,c^1)))
    R=list(map(lambda x: bit_insert(x,bit,(b>>bit)&1),solve(n-1,c^1,d)))
    return L+R

n,a,b=map(int,input().split())
if not bin(a^b).count('1')%2:
    print('NO')
else:
    print('YES')
    print(*solve(n,a,b))