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



Sample input:
2 1 3


Sample output:
YES
1 0 2 3

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N,A,B=map(int,input().split())
if N==1:
    print('YES')
    print(A,B)
    sys.exit()
C=A^B
C=bin(C)[2:]
num=0
for i in range(len(C)):
    if C[i]=='1':
        num+=1
if num%2==0:
    print('NO')
    sys.exit()
def f(a,b,n):
    if n==1:
        return [a,b]
    for i in range(n):
        if a&(1<<i)!=b&(1<<i):
            break
    x=(((1<<i)-1)&a)+((a>>(i+1))<<i)
    y = (((1 << i) - 1) & b) + ((b >> (i + 1)) << i)

    for z in range(1<<(n)):
        if x!=z and y!=z and bin(x^z).count('1')==1 and bin(y^z).count('1')==1:
            break
    #print(i,a,b,x,y,z)
    L=f(x,z,n-1)
    M=f(z,y,n-1)
    dp=[0]*(2*len(L))
    num=0
    for l in L:
        dp[num]=(((1<<i)-1)&l)+((l>>i)<<(i+1))+(a&(1<<i))
        num+=1
    for l in M:
        dp[num]=(((1<<i)-1)&l)+((l>>i)<<(i+1))+(b&(1<<i))
        num+=1
    #print(L,M,dp,i,a,b,x,y,z)
    return dp

L=f(A,B,N)
print('YES')
print(' '.join(map(str,L)))
#for i in range(len(L)-1):
    #s=bin(L[i]^L[i+1]).count('1')
    #print(bin(L[i]))
    #if s!=1:
        #print(i)
#print(len(set(L)))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N,A,B=map(int,input().split())
if N==1:
    print('YES')
    print(A,B)
    sys.exit()
C=A^B
C=bin(C)[2:]
num=0
for i in range(len(C)):
    if C[i]=='1':
        num+=1
if num%2==0:
    print('NO')
    sys.exit()
def f(a,b,n):
    if n==1:
        return [a,b]
    for i in range(n):
        if a&(1<<i)!=b&(1<<i):
            break
    x=(((1<<i)-1)&a)+((a>>(i+1))<<i)
    y = (((1 << i) - 1) & b) + ((b >> (i + 1)) << i)

    for z in range(1<<n):
        if x!=z and y!=z:
            break
    L=f(x,z,n-1)
    M=f(z,y,n-1)
    dp=[0]*(2*len(L))
    num=0
    for l in L:
        dp[num]=(((1<<i)-1)&l)+((l>>(i))<<(i+1))+(a&(1<<i))
        num+=1
    for l in M:
        dp[num]=(((1<<i)-1)&l)+((l>>(i))<<(i+1))+(b&(1<<i))
        num+=1
    #print(L,M,i,a,b,x,y,z)
    return dp

L=f(A,B,N)
print('YES')
print(' '.join(map(str,L)))

Submission status: Wrong Answer

Solution code:
import numpy as np
n,a,b = list(map(int,input().split()))
bit = 2**np.array(list(range(0,n)))
bit = list(bit)
point = list(range(0,2**n))
point.remove(a)
point.remove(b)
way = list()
way.append(a)
ans = "NO"
stand = 0
abi = bin(a)
bbi = bin(b)
if (abi.count("1")-bbi.count("1"))%2 == 1:
    ans ="YES"
if ans == "YES":
    for i in range(0,2**n-2):
        for k in range(0,n):
            if bit[k]^way[i] in point:
                stand = bit[k]^way[i]
                way.append(stand)
                point.remove(stand)
                break
print(ans)
if ans == "YES":
    way.append(b)
    print(*way)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,a,b = list(map(int,input().split()))
c = a^b
bit = 2**np.array(list(range(0,n)))
bit = list(bit)
point = list(range(0,2**n))
point.remove(a)
point.remove(b)
way = list()
way.append(a)
ans = "NO"
stand = 0
for i in range(0,n):
    if(c in bit):
        ans = "YES"
        break
if ans == "YES":
    for i in range(0,2**n-2):
        for k in range(0,n):
            if bit[k]^way[i] in point:
                stand = bit[k]^way[i]
                way.append(stand)
                point.remove(stand)
                break
print(ans)
if ans == "YES":
    way.append(b)
    print(*way)

Submission status: Wrong Answer

Solution code:

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

a_bin = format((2**18 - 1)-a, 'b')
b_bin = format((2**18 - 1)-b, 'b')

d = 0
l = []
for i in range(len(a_bin)):
    if a_bin[i] != b_bin[i]:
        d += 1
        l.append(18 - i)
        
if ((2**n) -1) >= d and (((2**n) -1)-d)%2 == 0:
    print('YES')
    ans = [a]
    for j in range((((2**n) -1)-d)//2):
        if a % 2 == 0:
            ans.append(a+1)
        else:
            ans.append(a-1)
        ans.append(a)
    
    for k in range(len(l)):
        if a_bin[l[k]] != 1:
            a += 2**(l[k]-1)
        else:
            a -= 2**(l[k]-1)
        ans.append(a)
    print(' '.join(map(str,ans)))
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
N, A, B = list(map(int, input().split(' ')))

C = A^B
c = format(A^B, 'b')
p = list()
p.append(A)
for i in range(len(c)):
    if int(c[len(c)-1-i]) == 1:
        tmp = p[-1] ^ (C & (1 << i))
        p.append(tmp)
for i in range(2**N-len(p)):
    p.append(p[-2])
if B == p[2**N-1]:
    print('YES')
    for i in range(len(p)):
        print(p[i], end=' ')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
N, A, B = list(map(int, input().split(' ')))

C = A^B
c = format(A^B, 'b')
p = list()
p.append(A)
for i in range(len(c)):
    if int(c[len(c)-1-i]) == 1:
        tmp = p[-1] ^ (C & (1 << i))
        p.append(tmp)
for i in range(2**N-len(p)):
    p.append(p[-2])
if B == p[-1]:
    print('YES')
    for i in range(len(p)):
        print(p[i], end=' ')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
N, A, B = list(map(int, input().split(' ')))

num = format(A^B, 'b').count('1')
if num >= 2**N - 1 or num % 2 == 0:
    print('NO')
else:
    C = A^B
    c = format(A^B, 'b')
    p = list()
    p.append(A)
    for i in range(len(c)):
        if int(c[len(c)-1-i]) == 1:
            tmp = p[-1] ^ (C & (1 << i))
            p.append(tmp)
    for i in range(2**N-len(p)):
        p.append(p[-2])
    print('YES')
    for i in range(len(p)):
        print(p[i], end=' ')

Submission status: Wrong Answer

Solution code:
N, A, B = list(map(int, input().split(' ')))

num = format(A^B, 'b').count('1')
if num > 2**N - 1 or num % 2 == 0:
    print('NO')
else:
    C = A^B
    c = format(A^B, 'b')
    p = list()
    p.append(A)
    for i in range(len(c)):
        if int(c[len(c)-1-i]) == 1:
            tmp = p[-1] ^ (C & (1 << i))
            p.append(tmp)
    for i in range(2**N-len(p)):
        p.append(p[-2])
    print('YES')
    for i in range(len(p)):
        print(p[i], end=' ')

Submission status: Wrong Answer

Solution code:
import math

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

maxn = 1000000
vis = [0 for i in range(maxn)]
an = 1

def func(now):
    global b
    global vis
    global an
    global n
    an += 1
    # print(now, an, n)
    if an == 1<<n:
        print("", b)
        # print("b = ", b, an)
        return -1
    else:
        for i in range(n):
            if vis[now ^ (1<<i)] == 0 and (now^(1<<i)) != b:
                vis[now ^ (1<<i)] = 1
                print("", now ^ (1<<i),end="")
                return now^(1<<i)
    return -1
x = 0
for i in range(n):
    if (a&(1<<i)) != (b&(i<<i)):
        x += 1
if x == 1:
    print("NO")
else:
    print("YES")
    vis[a] = 1
    print(a,end="")
    a = func(a)
    while a != -1:
        a = func(a)

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B = map(int, input().split())
 
    C = B - A
 
    #差のビット数を数える
    p = bin(C).count("1")
 
    if (N < p):
        print("NO")
        return
 
    if (0 == (N - p) % 2):
        print("NO")
        return
    
    print("YES")
    res = []
    a = A
    r = 1
    for i in range(N//2):
        if (0 == (C & r)):
            res.append(a)
            a ^= r
        r *= 2
    r = 1
    for i in range(N//2):
        if (0 == (C & r)):
            res.append(a)
            a ^= r
        r *= 2
    a ^= (r//2)
    r = 1
    #a = A
    for i in range(N):
        if (0 < (C & r)):
            a |= r
            res.append(a)
        r *= 2
    res.append(B)
    print(" ".join([str(i) for i in res]))
    
main()

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B = map(int, input().split())
 
    C = B - A
 
    #差のビット数を数える
    p = bin(C).count("1")
 
    if (N < p):
        print("NO")
        return
 
    if (0 == (N - p) % 2):
        print("NO")
        return
    
    print("YES")
    res = []
    a = A
    r = 1
    for i in range(N//2):
        if (0 == (C & r)):
            res.append(a)
            a ^= r
        r *= 2
    r = 1
    for i in range(N//2):
        if (0 == (C & r)):
            res.append(a)
            a ^= r
        r *= 2
    r = 1
    a = A
    for i in range(N):
        if (0 < (C & r)):
            a |= r
            res.append(a)
        r *= 2
    res.append(B)
    print(" ".join([str(i) for i in res]))
    
main()

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B = map(int, input().split())
 
    C = B - A
 
    #差のビット数を数える
    p = bin(C).count("1")
 
    if (N < p):
        print("NO")
        return
 
    if (0 == (N - p) % 2):
        print("NO")
        return
    
    print("YES")
    res = [A]
    r = 1
    for i in range(N):
        if (0 < (C & r)):
            res.append(res[-1] | r)
        r *= 2
    for i in range(N - p):
        res.append(res[-1] ^ 1)
    res.append(B)
    print(" ".join([str(i) for i in res]))
    
main()

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B = map(int, input().split())
 
    C = B - A
 
    #差のビット数を数える
    p = bin(C).count("1")
 
    if (N < p):
        print("NO")
        return
 
    if (0 == (N - p) % 2):
        print("NO")
        return
    
    print("YES")
    res = [A]
    r = 1
    for i in range(N):
        if (0 < (C & r)):
            res.append(res[-1] | r)
        r *= 2
    for i in range(N - p):
        res.append(res[-1] ^ 1)
    res.append(B)
    print(res)
    
main()

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B = map(int, input().split())

    C = B - A

    #差のビット数を数える
    p = bin(C).count("1")

    if (N < p):
        print("NO")
        return

    if (0 != (N - p) % 2):
        print("NO")
        return
    
    print("YES")
    res = [A]
    r = 1
    for i in range(N):
        if (0 < (C & r)):
            res.append(res[-1] | r)
        r *= 2
    for i in range(N - p):
        res.append(res[-1] ^ 1)
    print(res)
    
main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


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(' '.join(history), flush=True)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
n, a, b = map(int, raw_input().split(' '))
if n + 1 > 2**n:
  print "NO"
else:
  print "YES"

Submission status: Wrong Answer

Solution code:
import sys

def make_representation(N):
    num_list = [0]
    num = 0
    for n in range(1, 2**N):
        bit = 0
        while True:
            if n & 1 == 1:
                break
            else:
                bit += 1
                n = n >> 1
        num = reverse_bit(num, bit)
        num_list.append(num)
    return num_list

def reverse_bit(num, bit):
    mask = 1 << bit
    return (num & ~mask) + (~num & mask)

def convert4A(A):
    def convert(num):
        return (~num & A) + (num & ~A)
    return convert

def dif_bits(B, BB):
    diffs = []
    bit = 0
    while B > 0 and BB > 0:
        if B & 1 != BB & 1:
            diffs.append(bit)
        bit += 1
        B = B >> 1
        BB = BB >> 1
    return diffs

def swap_bits(num, swap):
    new_num = num
    new_num -= num & (1<<swap[0] + 1<<swap[1])
    new_num += (num & 1<<swap[0]) << abs(swap[1] - swap[0])
    new_num += (num & 1<<swap[1]) >> abs(swap[1] - swap[0])
    return new_num

def convert4B(diffs):
    def convert(num):
        i = 0
        while i+1 < len(diffs):
            swap = [diffs[i], diffs[i+1]]
            num = swap_bits(num, swap)
        return num
    return convert

def search(N, A, B):
    num_list = make_representation(N)
    convA = convert4A(A)
    BB = convA(num_list[-1])

    diffs = dif_bits(B, BB)
    if len(diffs) % 2 != 0:
        print('N0')
        sys.exit(0)
    print('YES')
    convB = convert4B(diffs)
    for num in num_list:
        print(convA(convB(num)))

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

Submission status: Wrong Answer

Solution code:
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(' '.join(str(i) for i in ans))

Submission status: Wrong Answer

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

times = 2 ** N - 1

hamming = format(A^B, 'b')
hamming_distance = hamming.count('1')

if (times - hamming_distance) % 2 == 0:
    differ_vals = []
    hamming_list = list(hamming)
    hamming_len = len(hamming_list)
    B_bin = format(B, 'b')
    B_len = len(B_bin)
    for i, v in enumerate(hamming_list):
        if v == '1':
            order = hamming_len-i-1
            val = 2**order
            val = -val if order < B_len and B_bin[B_len-order-1] == '0' else val
            differ_vals.append(val)
    for i in range(times-hamming_distance):
        differ_vals.append(-differ_vals[0] if i % 2 == 0 else differ_vals[0])
    transition = [str(A)]
    for val in differ_vals:
        A += val
        transition.append(str(A))
    print("YES")
    print(" ".join(transition))
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 16 22:18:05 2019

@author: Yamazaki Kenichi
"""
import sys

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

a = '{:020b}'.format(A)
b = '{:020b}'.format(B)
ab = '{:020b}'.format(A^B)
if ab.count('1') != 1 and ab.count('1') != 3:
    print('NO')
    sys.exit()
diff = []
same = []
for i in range(N):
    if ab[-i-1] == '1':
        diff.append(2**i)
    else:
        same.append(2**i)
res = []
for c in same:
    tmp = res[:]
    res.append(c)
    res += tmp
res1,res2,res3 = res[:],res[:],[]
for i in range(len(diff)):
    if i == len(diff)-1:
        res3 += res1
        res3.append(diff[i])
        res3 += res2
    else:
        tmp = res1[:]
        res1.append(diff[i])
        res1 += tmp
        tmp = res2[:]
        res2.append(diff[-i-2])
        res2 += tmp
ans = [A]
for c in res3:
    ans.append(ans[-1]^c)
print('YES')
print(' '.join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 16 22:18:05 2019

@author: Yamazaki Kenichi
"""
import sys

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

a = '{:020b}'.format(A)
b = '{:020b}'.format(B)
ab = '{:020b}'.format(A^B)
if ab.count('1') % 2 == 0:
    print('NO')
    sys.exit()
diff = []
same = []
for i in range(N):
    if ab[-i-1] == '1':
        diff.append(2**i)
    else:
        same.append(2**i)
res = []
for c in same:
    tmp = res[:]
    res.append(c)
    res += tmp
res1,res2,res3 = res[:],res[:],[]
for i in range(len(diff)):
    if i == len(diff)-1:
        res3 += res1
        res3.append(diff[i])
        res3 += res2
    else:
        tmp = res1[:]
        res1.append(diff[i])
        res1 += tmp
        tmp = res2[:]
        res2.append(diff[-i-2])
        res2 += tmp
ans = [A]
for c in res3:
    ans.append(ans[-1]^c)
print('YES')
print(' '.join(map(str,ans)))

Submission status: Wrong Answer

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

def doit(a, b, mask):
    differ = -1
    same = -1
    for i in range(n):
        if mask & (1 << i) == 0:
            if (a ^ b) & (1 << i) != 0:
                differ = i
            else:
                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))

Submission status: Wrong Answer

Solution code:
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[:2]
        ret.extend(v2)
        ret.extend(v1[2:])
        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)

Submission status: Wrong Answer

Solution code:
from collections import deque


def i_to_b(n, N):
    A = [0] * N
    for i in range(N):
        A[i] = (n // 2**i) % 2
    return A


def b_to_i(A):
    ret = 0
    for i in range(len(A)):
        ret += i**2*A[i]
    return ret


def rm_bit(n, i):
    return n // (2**(i+1)) * (2**i) + (n % (2**i))


def ins_bit(n, i, b):
    return n // (2**i) * (2**(i+1)) + (n % (2**i)) + b * (2**i)


def solve(N, A, B):
    if N == 1:
        return [A, B]
    idx = 0
    for i in range(N):
        if (A ^ B) & 2**i:
            idx = i
            break
    ra = rm_bit(A, idx)
    rb = rm_bit(B, idx) ^ 1
    nxt = solve(N-1, ra, rb)
    a = list(map(lambda x: ins_bit(x, idx, 0), nxt))
    b = list(map(lambda x: ins_bit(x, idx, 1), nxt[::-1]))
    return a + b


def main():
    N, A, B = map(int, input().split())
    binA = i_to_b(A, N)
    binB = i_to_b(B, N)
    if abs(sum(binA) - sum(binB)) % 2 == 0:
        print("NO")
        return
    print("YES")
    print(*solve(N, A, B))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

mask = A ^ B
c = bin(mask).count('1')
rem = pow(2, N) - 1 - c
if rem%2 == 0:
    print('YES')
    print(A)
    if rem > 0:
        tmp = A
        for i in range(rem):
            tmp = tmp ^ 1
            print(tmp)
             
    tmp = A
    for i in range(N):
        if mask & 1 == 1:
            tmp = tmp ^ pow(2, i)
            print(tmp)
        mask = mask >> 1

else:
    print('NO')

Submission status: Wrong Answer

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

def diff(a, b):
    x = a ^ b
    return bin(x).count("1")


p = [1] * 18
for i in range(1, 18):
    p[i] = p[i-1] * 2


#===

y = p[N] - 1 - diff(A, B)



l = []

cur = A
l.append(cur)

d = diff(cur, B)
flag = False

for iii in range(p[N]-1):
    for i in range(N):
        cand = cur^p[i]
        if not cand in l:
            d_new = diff(cand, B)
            if d_new > d:
                cur = cand
                flag = True
                break
    if flag:
        l.append(cur)
    else:
        for i in range(N):
            cand = cur^p[i]
            if not cand in l:
                d_new = diff(cand, B)
                cur = cand
                l.append(cur)
                break
    flag = False

if len(l) == p[N]:
    print('YES')
    print(" ".join(map(str,l)))
else:
    print('NO')

Submission status: Wrong Answer

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

def diff(a, b):
    x = a ^ b
    return bin(x).count("1")


p = [1] * 18
for i in range(1, 18):
    p[i] = p[i-1] * 2


#===

y = p[N] - 1 - diff(A, B)
if y % 2 == 1:
    print('NO')

else:


    l = []

    cur = A
    l.append(cur)

    d = diff(cur, B)
    flag = False

    for iii in range(p[N]-1):
        for i in range(N):
            cand = cur^p[i]
            if not cand in l:
                d_new = diff(cand, B)
                if d_new > d:
                    cur = cand
                    flag = True
                    break
        if flag:
            l.append(cur)
        else:
            for i in range(N):
                cand = cur^p[i]
                if not cand in l:
                    d_new = diff(cand, B)
                    cur = cand
                    l.append(cur)
                    break
        flag = False

    if len(l) == p[N]:
        print('YES')
        print(" ".join(map(str,l)))
    else:
        print('NO')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict
from itertools import product, cycle, chain

def Gray_code(N):
    for i in range(2**N):
        yield format(i^(i>>1), "0>"+str(N)+"b")

def code(N):
    L0 = ["10", "00", "01", "11"]
    if N == 0:
        return ["",]
    elif N == 2:
        return L0
    else:
        L1 = list(Gray_code(2))
        L2 = code(N-2)
        L = ["".join(l) for L_ in zip([[l] for l in L2], chain([L1,],cycle([L0, L0[::-1]]))) for l in product(*L_)]
        return L

def solve():
    N, A, B = map(int, input().split())
    C = A^B
    d = 0
    D = []
    E = []
    b = 0
    while b < N:
        if C&1:
            d += 1
            D.append(b)
        else:
            E.append(b)
        b += 1
        C >>= 1

    if d % 2 == 0:
        res = "NO"
        return res 
    else:
        X = D[:1]+E
        Y = D[1:]
        Z = list(Gray_code(len(X)))
        W = code(len(Y))
        L = ["".join(l) for L_ in zip([[l] for l in Z], chain([list(Gray_code(len(Y))),],cycle([W, W[::-1]]))) for l in product(*L_)]
        #print(X,Y,D,E)
        #print(L)
        res = "YES\n" + " ".join(str((A&B)^sum(int(b)*2**n for n,b in zip(X+Y, l))) for l in L)

        return str(res)
if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
_N, A, B = map(int, input().split())
N = 2**_N
es = [[] for _ in range(N)]
i = 0
while i < N - 1:
    es[i].append(i + 1)
    es[i + 1].append(i)
    i += 2
for i in range(N):
    for j in range(1, _N):
        J = 2**j
        if J > i:
            es[i].append(i + J)
            es[i + J].append(i)
import queue
q = queue.Queue()
q.put([A, 1])
ans = []
while not q.empty():
    l = q.get()
    v, k = l[0], l[1]
    if v in ans:
        continue
    ans.append(v)
    if v == B and k == N:
        break
    elif v == B and k < N:
        ans.pop(len(ans) - 1)
        continue
    for u in es[v]:
        if u not in ans:
            q.put([u, k + 1])

if len(ans) == N:
    print("YES")
    ret = ""
    for i in ans[::-1]:
        ret += str(i) + " "
    print(ret[:len(ret)-1])
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 16 22:18:05 2019

@author: Yamazaki Kenichi
"""

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

a = '{:020b}'.format(A)
b = '{:020b}'.format(B)
c = '{:020b}'.format(A^B)
C = int(c,2)
X = []
i = 1
while i < 2**(N):
    if i != C:
        tmp = X[:]
        X.append(i)
        X += tmp
        i *= 2
    else:
        i *= 2
    
res1,res2 =[A],[B]
if c.count('1') == 1:
    print('YES')
    for xx in X:
        res1.append(res1[-1]^xx)
        res2.append(res2[-1]^xx)
    res2.reverse()
    print(' '.join(map(str,res1))+' '+' '.join(map(str,res2)))
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict
from itertools import product, cycle, chain

def Gray_code(N):
    for i in range(2**N):
        yield format(i^(i>>1), "0>"+str(N)+"b")

def code(N):
    L0 = ["10", "00", "01", "11"]
    if N == 0:
        return ["",]
    elif N == 2:
        return L0
    else:
        L1 = list(Gray_code(2))
        L2 = code(N-2)
        L = ["".join(l) for L_ in zip([[l] for l in L2], chain([L1,],cycle([L0, L0[::-1]]))) for l in product(*L_)]
        return L

def solve():
    N, A, B = map(int, input().split())
    C = A^B
    d = 0
    D = []
    E = []
    b = 0
    while b < N:
        if C&1:
            d += 1
            D.append(b)
        else:
            E.append(b)
        b += 1
        C >>= 1

    if d % 2 == 0:
        res = "NO"
        return res 
    else:
        X = D[:1]+E
        Y = D[1:]
        Z = list(Gray_code(len(X)))
        W = code(len(Y))
        L = ["".join(l) for L_ in zip([[l] for l in Z], chain([list(Gray_code(len(Y))),],cycle([W, W[::-1]]))) for l in product(*L_)]
        #print(X,Y,D,E)
        #print(L)
        res = " ".join(str((A&B)^sum(int(b)*2**n for n,b in zip(X+Y, l))) for l in L)

        return str(res)
if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
N,A,B= list(map(int,input().split()))
B1 = A^B


from collections import defaultdict
d = defaultdict(list)
for i in range(2**N):
    bits_total = bin(i)[2:].count('1')
    d[bits_total].append(i)
def count_bits(n):
    return bin(n)[2:].count('1')

def int_to_bin(n):
    return list(reversed(list((map(lambda x:x=='1',bin(n)[2:])))))+[0]*N
def bin_to_int(V):
    ans = 0
    for i,val in enumerate(V):
        ans += val *(2 **i)
    return ans

assert bin_to_int([1])==1
assert bin_to_int([0,1])==2

def movable(i):
    res = set()
    for bit_num in range(N):
        if i>= 2**bit_num and bin(i)[2:][-bit_num-1]=='1':
            res.add(i - 2**bit_num)
        else:
            res.add(i+2**bit_num)
    return res

used = set()
i=0
next = -1
used.add(0)
order = [0]
try:
    while len(used) < 2**N:
        next = max(movable(i) - used)
        used.add(next)
        i=next
        order.append(i)
except:
    print("NO")
    exit()

if count_bits(next)!= count_bits(B1):
    print("NO")
    exit()



def find_all(x):
    res = []
    for bit_num in range(N):
        if x>= 2**bit_num and bin(x)[2:][-bit_num-1]=='1':
            res.append(bit_num)
    return res
a= find_all(B1)
b= find_all(next)

def convert(x,p,q):
    V = int_to_bin(x)
    for i in range(len(p)):
        tmp = V[p[i]]
        V[p[i]]=V[q[i]]
        V[q[i]]= tmp
    return bin_to_int(V)

print("YES")
print(" ".join([str(convert(n,a,b) ^A) for n in order]))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict
from itertools import product, cycle, chain

def Gray_code(N):
    for i in range(2**N):
        yield format(i^(i>>1), "0>"+str(N)+"b")

def code(N):
    L0 = ["10", "00", "01", "11"]
    if N == 0:
        return ["",]
    elif N == 2:
        return L0
    else:
        L1 = list(Gray_code(2))
        L2 = code(N-2)
        L = ["".join(l) for L_ in zip([[l] for l in L2], chain([L1,],cycle([L0, L0[::-1]]))) for l in product(*L_)]
        return L

def solve():
    N, A, B = map(int, input().split())
    C = A^B
    d = 0
    D = []
    E = []
    b = 0
    while b < N:
        if C&1:
            d += 1
            D.append(b)
        else:
            E.append(b)
        b += 1
        C >>= 1

    if d % 2 == 0:
        res = "NO"
        return res 
    else:
        X = D[:1]+E
        Y = D[1:]
        Z = list(Gray_code(len(X)))
        W = code(len(Y))
        L = ["".join(l) for L_ in zip([[l] for l in Z], chain([list(Gray_code(len(Y))),],cycle([W, W[::-1]]))) for l in product(*L_)]
        print(X,Y,D,E)
        print(L)
        res = " ".join(str((A&B)^sum(int(b)*2**n for n,b in zip(X+Y, l))) for l in L)

        return str(res)
if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
import sys

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

if N==1:
    print("YES")
    print(A,B)
    sys.exit()

BIT=0
for i in range(N):
    #print(1<<i,(1<<i) & A,(1<<i) & B)
    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
W=[N]*(1<<N)

for j in range(N):
    W[A^(1<<j)]-=1
    #W[B^(1<<j)]-=1

NOW=A
#print(W,NOW)
for i in range((1<<N)-2):
    ANS.append(NOW)
    
    #WN=N
    NEXTS=[]
    for j in range(N):
        if check[NOW ^ (1<<j)]==0:
            NEXTS.append([W[NOW ^ (1<<j)],NOW ^ (1<<j)])

    MIN=min(NEXTS)[0]
    NEXT2=[n[1] for n in NEXTS if n[0]==MIN]

    BITS=0

    for n in NEXT2:
        BBITS=0
        for j in range(N):           
            if A & (1<<j)==(n & (1<<j)):
                BBITS+=1

        if BBITS>=BITS:
            NEXT=n
            BBITS=BITS
            
    for j in range(N):
        W[NEXT^(1<<j)]-=1

    NOW=NEXT
    check[NOW]=1

    #print(W,NOW)
ANS.append(NOW)
ANS.append(B)

print(*ANS)