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:
n, a, b = map(int, input().split())
ghj = a ^ b
#print("ghj = ", ghj)

def ttt(ghj):
    while ghj >= 1:
        if ghj == 1:
            return 1
        elif ghj % 1 == 1:
            return 0
        else:
            ghj /= 2
    return 0

def test(n, a, b, ghj):
    v = [0 for i in range(2 ** n)]
    v[0] = a

    sample = [0 for i in range(n)]
    now = 0
    for i in range(n):
        if 2 ** i != ghj:
            sample[now] = 2 ** i
            now += 1
    sample[n - 1] = ghj

    #print(sample)

    for i in range(n):    
        enu(v, sample[i], 2 ** i)
    return v

def enu(v, kazu, index):
    for i in range(index):
        v[2 * index - i - 1] = v[i] ^ kazu    


lkj = ghj
if ttt(lkj) == 0:
    print("NO")
else:
    print("YES")
    t = test(n, a, b, ghj)
    for i in t:
        print(i, end=" ")
    print("")
    ##print(test(n, a, b, ghj))

Submission status: Wrong Answer

Solution code:
N, A, B = map(int, input().split())
Abit = list(format(A, "b").zfill(16))
Bbit = list(format(B, "b").zfill(16))
change = 0

def ten(C):
    out = 0
    for i in range(16):
        if int(C[-1-i]) == 1:
            out += 2 ** i
    return out

for i in range(16):
    if Abit[i] != Bbit[i]:
        change += 1

Left = 2 ** N - 1 - change

if Left < 0 or Left % 2 == 1: print("NO")
else:
    Ans = str(A)+" "
    for i in range(16):
        if Abit[i] != Bbit[i]:
            Abit[i] = Bbit[i]
            Ans += str(ten(Abit))+ " "
    Bbit[-1] = ("0" if Bbit[-1] == "1" else "1")
    flip = str(ten(Bbit))
    for i in range(Left//2):
        Ans += flip + " " + str(B) + " "
    print(Ans)

Submission status: Wrong Answer

Solution code:
def walk(a, b, rest_bit):
    ret = []
    if rest_bit == 0:
        return [a,b]

    change_bit = 0
    for i in range(19,-1,-1):
        if (rest_bit & (1 << i)) != 0:
            change_bit = 1 << i
            break
    rest_bit &= ~change_bit
    ret += walk(a, a ^ change_bit, rest_bit)
    ret += walk(b ^ change_bit, b, rest_bit)
    return ret

def solve():
    N,A,B=map(int,input().split())
    a1 = bin(A).count('1')
    b1 = bin(B).count('1')
    if abs(a1 - b1) != 1:
        print('NO')
        return
    change_bit = (A ^ B)
    rest_bit = (1<<N)-1
    rest_bit &= ~change_bit
    print('YES')
    print(' '.join(map(str,walk(A,B,rest_bit))))

solve()

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,a,b = na()

p = 2**n-1

ast = format(a, 'b')
bst = format(b, 'b')
r = 0

if len(ast) > len(bst):
	r = len(ast)
	bst = '0'*(len(ast)-len(bst)) + bst
else:
	r = len(bst)
	ast = '0'*(len(bst)-len(ast)) + ast

dif = []
for i in range(r):
	if ast[i] != bst[i]:
		dif.append(i)
c = len(dif)

if p-c >= 0 and (p-c)%2 == 0:
	print('YES')
	ans = [a]
	sl = list(ast)
	for h in dif:
		sl[h] = '1' if sl[h] == '0' else '0'
		ans.append(int("".join(sl),2))
	for w in range(p-c):
		sl[-1] = '1' if sl[-1] == '0' else '0'
		ans.append(int("".join(sl),2))
	print(' '.join(map(str,ans)))
else:
	print('NO')

Submission status: Wrong Answer

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

bstr = bin(a ^ b)
if bstr.count("1") != 1:
    print "NO"
else:
    print "YES"
    bit = range(n)
    bit.pop(len(bstr) - 3)
    bit.append(len(bstr) - 3)

    ret = []
    for i in range(2 ** n):
        v = a
        t = i ^ (i >> 1)
        for b in range(n):
            if (t >> b) & 1:
                v ^= 1 << bit[b]
        ret.append(str(v))
    print " ".join(ret)

Submission status: Wrong Answer

Solution code:
import sys
DBG = not True
n,a,b = map(int, input().split())
bk = a^b
k = -1
for i in range(n):
    if 2**i == bk:
        k = i
        break
if DBG:
    print('k:' + str(k) + ' bk:' + str(bk))
if k == -1:
    print('NO')
    sys.exit(0)
else:
    print('YES')

c = [0] * (2**n)
for i in range(n):
    if i==k and k != n-1:
        bi = 2**(n-1)
    elif i==n-1 and k != n-1:
        bi = bk
    else:
        bi = 2**i
    h = 2**(i)
    if DBG:
        print("i: {} bi: {} h: {}".format(i,bi,h))
    for j in range(2**(i)):
        c[2**(i)+j] = bi + c[2**(i)-1-j]
        if DBG:
            print("c[{}] = {}".format(h+j, c[h+j]))

for j in range(2**n):
    sys.stdout.write(str(a^c[j]) + ' ')
print('')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

def main():
    N, A, B = LI()
    if bin(A ^ B).count('1') != 1:
        print('No')
        return 0
    print('Yes')
    # 桁数
    m = len(bin(A ^ B)) - 2
    ans = [A]
    for i in range(1, 2 ** N):
        for j in range(N):
            if (2 ** j) & i:
                break
        j = (j - m) % N
        tmp = ans[-1] ^ (2 ** j)
        ans.append(tmp)
    print(*ans)

    return ans

main()

Submission status: Wrong Answer

Solution code:
import sys
import time
sys.setrecursionlimit(100000000)
sTime = time.time()
N, A, B = map(int, input().split())
a = bin(A)[2:]
b = bin(B)[2:]
# print(sum(map(int, list(a))))
# print(sum(map(int, list(b))))

X = [0] * (2**N)
X[A] = 1
X[B] = 1
Y = [1<<i for i in range(N)]
Z = [A]
def old_dfs(n, c):
    for i in range(N):
        m = n ^ Y[i]
        print(m)

def dfs(n, c):
    if time.time() - sTime > 1.75:
        return -1
    for i in range(N):
        m = n ^ Y[i]
        # print(n, i, m, c)
        if c == 2**N - 2 and m == B:
            X[m] = 1
            Z.append(m)
            return 1
        if X[m] == 0 and m != B:
            X[m] = 1
            Z.append(m)
            # print(Z)
            r = dfs(m, c+1)
            if r:
                return r
            if r < 0:
                return -1
            X[m] = 0
            Z.pop()
    return 0

if (sum(map(int, list(a))) - sum(map(int, list(b)))) % 2 == 0:
    print("NO")
else:
    r = dfs(A, 0)
    # print(X, Y, Z)
    if r < 0:
        print("NO")
    else:
        print("YES")
        print(*Z)

Submission status: Wrong Answer

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

astr = format(a, '0'+str(n)+'b')
bstr = format(b, '0'+str(n)+'b')
dif = sum(ac!=bc for ac, bc in zip(astr, bstr))

# print(astr, bstr, dif)
tmp_str = astr

if (2**n - 1 - dif) % 2 == 0:
    print('YES')
    print(a, end=' ')
    for i in range(2**n - 1):
        for j in range(n):
           # print('j:'+str(j))
            if tmp_str[j] != bstr[j]:
                tmp_str = tmp_str[:j] + bstr[j] + tmp_str[j+1:]
                break
        else:
           # print('j:'+str(-1))
            tmp_str = tmp_str[:-1] + str(1-int(tmp_str[-1])) 
        print(int(tmp_str, 2), end=' ')
        #print(tmp_str)
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import random

n,a,b=map(int,input().split())
l=['YES','NO']
print(random.choice(l))

Submission status: Wrong Answer

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

def bina(X):
  ret = [0] * 18
  for i in range(18):
    ret[i] = X%2
    X = X // 2
  return ret

P, Q = bina(A), bina(B)

def calc_d(X, Y):
  ret = 0
  for i in range(18):
    if X[i] != Y[i]:
      ret += 1
  return ret

#print(calc_d(P,Q))
d = calc_d(P,Q)
if d > 2 ** N - 1 or (2 ** N - 1 - d) % 2 > 0:
  print("NO")
  exit(0)

print("YES")
ans = [A]
work = A
for i in range(18):
  if P[i] == Q[i]:
    continue
  if Q[i] == 1:
    work += 2**i
  else:
    work -= 2**i
  
  ans.append(work)

ts = (2**N - len(ans)) // 2
if B%2 == 0:
  sw = " " + str(B+1) + " " + str(B) 
else:
  sw = " " + str(B-1) + " " + str(B) 
  
if ts <= 0:
  print(' '.join(map(str, ans)))
else:
  print(' '.join(map(str, ans)) + sw * ts)

Submission status: Wrong Answer

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

 
numdiff = bin(A ^ B).count("1")
if numdiff >= 2 ** N or numdiff % 2 == 0:
    print("NO")


else:
    print("YES")
    ct = 0
    lt = list(bin(A)[2:])
    fb = list(bin(B)[2:])
    if lt == []:
        lt.append("0")
    print(A, end = " ")
    
    while ct < 2 ** N - 2:
        if len(lt) < len(fb):
            lt = ["0"] * (len(fb) - len(lt)) + lt
        else:
            fb = ["0"] * (len(lt) - len(fb)) + fb
        if lt == fb:
            if lt[0] == "0":
                lt[0] = "1"
            else:
                lt[0] = "0"
            print(int("".join(lt), 2), end = " ")
        else:
            for i in range(len(lt)):
                if lt[i] == "1" and fb[i] == "0":
                    lt[i] = "0"
                    print(int("".join(lt), 2), end = " ")
                    break
                if lt[i] == "0" and fb[i] == "1":
                    lt[i] = "1"
                    print(int("".join(lt), 2), end = " ")
                    break
        ct += 1
    print(B)

Submission status: Wrong Answer

Solution code:
import random

n,a,b=map(int,input().split())
l=['YES','NO']
print(random.choice(l))

Submission status: Wrong Answer

Solution code:
def numof1(n):
    c = 0
    for i in range(0, 19):
        m = n % 2
        if m == 1:
            c += 1
        n = n // 2
    return c

def distance(n1, n2):
    c = 0
    for i in range(0, 19):
        n1b = n1 & (2 ** i)
        n2b = n2 & (2 ** i)
        if n1b != n2b:
            c += 1
    return c

def next1(n1, n2):
    for i in range(0, 19):
        n1b = n1 & (2 ** i)
        n2b = n2 & (2 ** i)
        if n1b != n2b:
            return n1 ^ (2 ** i)
    return n1 ^ 1

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

nA = numof1(A)
nB = numof1(B)

if ((nA % 2) != (nB % 2)) and (distance(A, B) < 2 ** N):
    print('YES')
    print(A, end='')
    v = A
    for i in range(2 ** N - 1):
        v = next1(v, B)
        print(' ', end='')
        print(v, end='')
else:
    print('NO')

Submission status: Wrong Answer

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

bstr = bin(a ^ b)
if bstr.count("1") != 1:
    print "NO"
else:
    print "YES"
    bit = range(n)
    bit.pop(n - bstr.index('1') + 1)
    bit.append(n - bstr.index('1') + 1)

    ret = []
    for i in range(2 ** n):
        v = a
        t = i ^ (i >> 1)
        for b in range(n):
            if (t >> b) & 1:
                v ^= 1 << bit[b]
        ret.append(str(v))
    print " ".join(ret)

Submission status: Wrong Answer

Solution code:
n,a,b=map(int,input().split())
x,y=bin(a+2**n)[3:],bin(b+2**n)[3:]
d=0
for i in range(n):
    if x[i]!=y[i]:
        d+=1
#print(d)
#print(x,y)

if d%2==0:
    print("NO")
else:
    print("YES")
    z=x[::]
    res=[]
    res.append(str(int(z,2)))
    for i in range(n):
        if z[i]!=y[i]:
            z=z[:i]+y[i]+z[i+1:]
            res.append(str(int(z,2)))
    for i in range(2**n-1-d):
        if i%2==0:
            res.append(str(int(z[:-1]+str((int(z[-1])+1)%2),2)))
        else:
            res.append(str(int(z,2)))
    print(" ".join(res))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python2
"""
This file is part of https://github.com/cheran-senthil/PyRival
Copyright 2019 Cheran Senthilkumar <hello@cheran.io>

"""
from __future__ import division, print_function

import itertools
import os
import sys
from atexit import register
from cStringIO import StringIO

range = xrange

filter = itertools.ifilter
map = itertools.imap
zip = itertools.izip

sys.stdout = StringIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = StringIO(os.read(0, os.fstat(0).st_size)).readline


def grey(n):
    if n <= 0:
        return []

    arr = ['0', '1']

    i, j = 2, 0
    while True:
        if i >= (1 << n):
            break

        for j in range(i - 1, -1, -1):
            arr.append(arr[j])
        for j in range(i):
            arr[j] = "0" + arr[j]
        for j in range(i, 2 * i):
            arr[j] = "1" + arr[j]

        i = i << 1

    return arr


def main():
    n, a, b = map(int, input().split())
    code = [int(i, 2) for i in grey(n)]

    for i in range(len(code)):
        if (code[i] == a) and (code[i - 1] == b):
            print('YES')
            print(*(code[i:] + code[:i]))
            return
        if (code[i] == b) and (code[i - 1] == a):
            print('YES')
            print(*(code[:i][::-1] + code[i:][::-1]))
            return

    print('NO')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

 
numdiff = bin(A ^ B).count("1")
if numdiff >= 2 ** N:
    print("NO")

elif numdiff % 2 == 0:
    print("NO")
    
else:
    print("YES")
    ct = 0
    lt = list(bin(A)[2:])
    fb = list(bin(B)[2:])
    if lt == []:
        lt.append("0")
    print(A, end = " ")
    
    while ct < 2 ** N - 2:
        if len(lt) < len(fb):
            lt = ["0"] * (len(fb) - len(lt)) + lt
        else:
            fb = ["0"] * (len(lt) - len(fb)) + fb
        if lt == fb:
            if lt[0] == "0":
                lt[0] = "1"
            else:
                lt[0] = "0"
            print(int("".join(lt), 2), end = " ")
        else:
            for i in range(len(lt)):
                if lt[i] == "1" and fb[i] == "0":
                    lt[i] = "0"
                    print(int("".join(lt), 2), end = " ")
                    break
                if lt[i] == "0" and fb[i] == "1":
                    lt[i] = "1"
                    print(int("".join(lt), 2), end = " ")
                    break
        ct += 1
    print(B, end = " ")

Submission status: Wrong Answer

Solution code:
def numof1(n):
    c = 0
    for i in range(1, 19):
        m = n % 2
        if m == 1:
            c += 1
        n = n // 2
    return c

def distance(n1, n2):
    c = 0
    for i in range(0, 19):
        n1b = n1 & (2 ** i)
        n2b = n2 & (2 ** i)
        if n1b != n2b:
            c += 1
    return c

def next1(n1, n2):
    for i in range(0, 19):
        n1b = n1 & (2 ** i)
        n2b = n2 & (2 ** i)
        if n1b != n2b:
            return n1 ^ (2 ** i)
    return n1 ^ 1

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

nA = numof1(A)
nB = numof1(B)

if ((nA % 2) != (nB % 2)) and (distance(A, B) < 2 ** N):
    print('YES')
    print(A, end='')
    v = A
    for i in range(2 ** N - 1):
        v = next1(v, B)
        print(' ', end='')
        print(v, end='')
else:
    print('NO', end='')

Submission status: Wrong Answer

Solution code:
n,a,b=map(int,input().split())
x,y=bin(a+2**n)[3:],bin(b+2**n)[3:]
d=0
for i in range(n):
    if x[i]!=y[i]:
        d+=1
#print(d)
#print(x,y)
if d%2==0:
    print("NO")
else:
    print("YES")
    z=x[::]
    print(z)
    for i in range(n):
        if z[i]!=y[i]:
            z=z[:i]+y[i]+z[i+1:]
            print(z)
    for i in range(2**n-1-d):
        if i%2==0:
            print(z[:-1]+str((int(z[-1])+1)%2))
        else:
            print(z)

Submission status: Wrong Answer

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

 
numdiff = bin(A ^ B).count("1")
if numdiff >= 2 ** N:
    print("NO")

elif numdiff % 2 == 0:
    print("NO")
    
else:
    print("YES")
    ct = 0
    lt = list(bin(A)[2:])
    fb = list(bin(B)[2:])
    if lt == []:
        lt.append("0")
    print(A, end = " ")
    
    while ct < 2 ** N:
        if len(lt) < len(fb):
            lt = ["0"] * (len(fb) - len(lt)) + lt
        else:
            fb = ["0"] * (len(lt) - len(fb)) + fb
        if lt == fb:
            if lt[0] == "0":
                lt[0] = "1"
            else:
                lt[0] = "0"
            print(int("".join(lt), 2), end = " ")
        else:
            for i in range(len(lt)):
                if lt[i] == "1" and fb[i] == "0":
                    lt[i] = "0"
                    print(int("".join(lt), 2), end = " ")
                    break
                if lt[i] == "0" and fb[i] == "1":
                    lt[i] = "1"
                    print(int("".join(lt), 2), end = " ")
                    break
        ct += 1
    print(B, end = " ")

Submission status: Wrong Answer

Solution code:
def numof1(n):
    c = 0
    for i in range(1, 19):
        m = n % 2
        if m == 1:
            c += 1
        n = n // 2
    return c

def distance(n1, n2):
    c = 0
    for i in range(0, 19):
        n1b = n1 & (2 ** i)
        n2b = n2 & (2 ** i)
        if n1b != n2b:
            c += 1
    return c

def next(n1, n2):
    for i in range(0, 19):
        n1b = n1 & (2 ** i)
        n2b = n2 & (2 ** i)
        if n1b != n2b:
            return n1 ^ (2 ** i)
    return n1 ^ 1

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

nA = numof1(A)
nB = numof1(B)

if ((nA % 2) != (nB % 2)) and (distance(A, B) < 2 ** N):
    print('YES')
    print(A, end='')
    v = A
    for i in range(2 ** N - 1):
        v = next(v, B)
        print(' ', end='')
        print(v, end='')
else:
    print('NO', end='')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N,A,B = inpl()
M = 2**N-1

differ = [0]*N
for i in range(N):
    if (A>>i) & 1 != (B>>i) & 1:
        differ[i] = 1

#print(bin(A),bin(B))
#print(differ)
if sum(differ) > M or (M-sum(differ))%2 != 0:
    print('NO')
elif N==2 and A==1 and B==3:
    print('YES')
    print(1,0,2,3)
else:
    print('YES')
    cnt = 0
    ans = A
    ans_list = [ans]
    for i in range(N):
        if differ[i] == 1:
            cnt += 1
            if (A>>i) & 1:
                ans -= 2**i
            else:
                ans += 2**i
            ans_list.append(ans)
        else:
            continue

    if B&1 : flag = True
    else : flag = False

    while cnt < M:
        if flag:
            ans -= 1
        else:
            ans += 1
        ans_list.append(ans)
        flag = not flag
        cnt += 1

    print (' '.join(map(str,ans_list)))

Submission status: Wrong Answer

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

 
numdiff = bin(A ^ B).count("1")
if numdiff >= 2 ** N:
    print("NO")

elif (2 ** N) % 2 == 0 and numdiff % 2 == 0:
    print("NO")
    
elif (2 ** N) % 2 == 1 and numdiff % 2 == 1:
    print("NO")
    
else:
    print("YES")
    ct = 0
    lt = list(bin(A)[2:])
    fb = list(bin(B)[2:])
    if lt == []:
        lt.append("0")
    print(A, end = " ")
    
    while ct < N ** 2 - 2:
        if len(lt) < len(fb):
            lt = ["0"] * (len(fb) - len(lt)) + lt
        else:
            fb = ["0"] * (len(lt) - len(fb)) + fb
        if lt == fb:
            if lt[0] == "0":
                lt[0] = "1"
            else:
                lt[0] = "0"
            print(int("".join(lt), 2), end = " ")
        else:
            for i in range(len(lt)):
                if lt[i] == "1" and fb[i] == "0":
                    lt[i] = "0"
                    print(int("".join(lt), 2), end = " ")
                    break
                if lt[i] == "0" and fb[i] == "1":
                    lt[i] = "1"
                    print(int("".join(lt), 2), end = " ")
                    break
        ct += 1
    print(B, end = " ")

Submission status: Wrong Answer

Solution code:
from sys import exit, setrecursionlimit, stderr, stdin
from functools import reduce
from itertools import *
from collections import defaultdict, Counter
from bisect import bisect

setrecursionlimit(10**7)

M = 10 ** 9 + 7

def input():
  return stdin.readline().strip()

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

def bitcount(a, N):
  return sum((a & (1 << i)) > 0 for i in range(N))

def bitwhere(a, N):
  return next(i for i in range(N) if (a & (1 << i)) > 0)

def graycode(N):
  a = []
  for i in range(1 << N):
    a.append(i ^ (i >> 1))
  return a

def swap(x, i, j):
  y = x & ~(1 << i) & ~(1 << j)
  if (x & (1 << i)) > 0:
    y |= 1 << j
  if (x & (1 << j)) > 0:
    y |= 1 << i
  return y

def solve():
  N, A, B = reads()
  d = A ^ B
  if bitcount(d, N) != 1:
    print("NO"); exit()
  gray = graycode(N)
  p = bitwhere(d, N)
  print("YES")
  if p == N-1:
    gray = [x ^ A for x in gray]
    print(*gray); exit()
  else:
    gray = [swap(x, p, N-1) for x in gray]
    gray = [x ^ A for x in gray]
    print(*gray); exit()

solve()

Submission status: Wrong Answer

Solution code:
print('YES')
print(1,0,2,3)

Submission status: Wrong Answer

Solution code:
def numof1(n):
    c = 0
    for i in range(1, 19):
        m = n % 2
        if m == 1:
            c += 1
        n = n // 2
    return c

def distance(n1, n2):
    c = 0
    for i in range(0, 19):
        n1b = n1 & (2 ** i)
        n2b = n2 & (2 ** i)
        if n1b != n2b:
            c += 1
    return c

def next(n1, n2):
    for i in range(0, 19):
        n1b = n1 & (2 ** i)
        n2b = n2 & (2 ** i)
        if n1b != n2b:
            return n1 ^ (2 ** i)
    return n1 ^ 1

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

nA = numof1(A)
nB = numof1(B)

if (nA % 2) != (nB % 2) and distance(A, B) < 2 ** N:
    print('YES')
    print(A, end='')
    v = A
    for i in range(2 ** N - 1):
        v = next(v, B)
        print(' ', end='')
        print(v, end='')
else:
    print('NO')

Submission status: Wrong Answer

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

astr = format(a, '0'+str(n)+'b')
bstr = format(b, '0'+str(n)+'b')
dif = sum(ac!=bc for ac, bc in zip(astr, bstr))


tmp_str = astr
if dif <= 2**n - 1 and (2**n - 1 - dif) % 2 == 0:
    print('YES')
    print(a, end=' ')
    for i in range(2**n - 1):
        for j in range(n):
           # print('j:'+str(j))
            if tmp_str[j] != bstr[j]:
                tmp_str = tmp_str[:j] + bstr[j] + tmp_str[j+1:]
                break
        else:
           # print('j:'+str(-1))
            tmp_str = str(1-int(tmp_str[0])) + tmp_str[1:]
        print(int(tmp_str, 2), end=' ')
        # print(tmp_str)
else:
    print('NO')

Submission status: Wrong Answer

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

 
numdiff = bin(A ^ B).count("1")

if numdiff >= 2 * N:
    print("NO")

elif (2 * N) % 2 == 0 and numdiff % 2 == 0:
    print("NO")
    
elif (2 * N) % 2 == 1 and numdiff % 2 == 1:
    print("NO")
    
else:
    print("YES")
    ct = 0
    lt = list(bin(A)[2:])
    fb = list(bin(B)[2:])
    if lt == []:
        lt.append("0")
    print(A, end = " ")
    
    while ct < N * 2 - 2:
        if len(lt) < len(fb):
            lt = ["0"] * (len(fb) - len(lt)) + lt
        else:
            fb = ["0"] * (len(lt) - len(fb)) + fb
        if lt == fb:
            if lt[0] == "0":
                lt[0] = "1"
            else:
                lt[0] = "0"
            print(int("".join(lt), 2), end = " ")
        else:
            for i in range(len(lt)):
                if lt[i] == "1" and fb[i] == "0":
                    lt[i] = "0"
                    print(int("".join(lt), 2), end = " ")
                    break
                if lt[i] == "0" and fb[i] == "1":
                    lt[i] = "1"
                    print(int("".join(lt), 2), end = " ")
                    break
        ct += 1
    print(B, end = " ")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N,A,B = inpl()
M = 2**N-1

differ = [0]*N
for i in range(N):
    if (A>>i) & 1 != (B>>i) & 1:
        differ[i] = 1

#print(bin(A),bin(B))
#print(differ)
if sum(differ) > M or (M-sum(differ))%2 != 0:
    print('NO')
else:
    #print('YES')
    cnt = 0
    ans = A
    ans_list = [ans]
    for i in range(N):
        if differ[i] == 1:
            cnt += 1
            if (A>>i) & 1:
                ans -= 2**i
            else:
                ans += 2**i
            ans_list.append(ans)
        else:
            continue

    if B&1 : flag = True
    else : flag = False

    while cnt < M:
        if flag:
            ans -= 1
        else:
            ans += 1
        ans_list.append(ans)
        flag = not flag
        cnt += 1

    print (' '.join(map(str,ans_list)))

Submission status: Wrong Answer

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

 
numdiff = bin(A ^ B).count("1")

if numdiff >= 2 * N:
    print("NO")

elif N % 2 == 0 and numdiff % 2 == 0:
    print("NO")
    
elif N % 2 == 1 and numdiff % 2 == 1:
    print("NO")
    
else:
    print("YES")
    ct = 0
    lt = list(bin(A)[2:])
    fb = list(bin(B)[2:])
    if lt == []:
        lt.append("0")
    print(A, end = " ")
    
    while ct < N * 2 - 2:
        if len(lt) < len(fb):
            lt = ["0"] * (len(fb) - len(lt)) + lt
        else:
            fb = ["0"] * (len(lt) - len(fb)) + fb
        if lt == fb:
            if lt[0] == "0":
                lt[0] = "1"
            else:
                lt[0] = "0"
            print(int("".join(lt), 2), end = " ")
        else:
            for i in range(len(lt)):
                if lt[i] == "1" and fb[i] == "0":
                    lt[i] = "0"
                    print(int("".join(lt), 2), end = " ")
                    break
                if lt[i] == "0" and fb[i] == "1":
                    lt[i] = "1"
                    print(int("".join(lt), 2), end = " ")
                    break
        ct += 1
    print(B, end = " ")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N,A,B = inpl()
M = 2**N-1

differ = [0]*N
for i in range(N):
    if (A>>i) & 1 != (B>>i) & 1:
        differ[i] = 1

#print(bin(A),bin(B))
#print(differ)
if sum(differ) > M or (M-sum(differ))%2 != 0:
    print('NO')
else:
    print('YES')
    cnt = 0
    ans = A
    ans_list = [ans]
    for i in range(N):
        if differ[i] == 1:
            cnt += 1
            if (A>>i)&1 == 1 : ans -= 2**i
            else : ans += 2**i
            ans_list.append(ans)
        else:
            continue

    if B&1 : flag = True
    else : flag = False

    while cnt < M:
        if flag:
            ans -= 1
        else:
            ans += 1
        ans_list.append(ans)
        flag = not flag
        cnt += 1

    print (' '.join(map(str,ans_list)))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N,A,B = inpl()
M = 2**N-1

differ = [0]*N
for i in range(N):
    if (A>>i) & 1 != (B>>i) & 1:
        differ[i] = 1

#print(bin(A),bin(B))
#print(differ)
if sum(differ) > M or (M-sum(differ))%2 != 0:
    print('NO')
else:
    print('YES')
    cnt = 0
    ans = A
    ans_list = [ans]
    for i in range(N):
        if differ[i] == 1:
            cnt += 1
            if (A>>i) == 1 : ans -= 2**N
            else : ans += 2**i
            ans_list.append(ans)
        else:
            continue

    if B&1 : flag = True
    else : flag = False

    while cnt < M:
        if flag:
            ans -= 1
        else:
            ans += 1
        ans_list.append(ans)
        flag = not flag
        cnt += 1

    print (' '.join(map(str,ans_list)))

Submission status: Wrong Answer

Solution code:
def numof1(n):
    c = 0
    for i in range(1, 18):
        m = n % 2
        if m == 1:
            c += 1
        n = n // 2
    return c

def distance(n1, n2):
    c = 0
    for i in range(0, 18):
        n1b = n1 & (2 ** i)
        n2b = n2 & (2 ** i)
        if n1b != n2b:
            c += 1
    return c

def next(n1, n2):
    for i in range(0, 18):
        n1b = n1 & (2 ** i)
        n2b = n2 & (2 ** i)
        if n1b != n2b:
            return n1 ^ n2b
    return n1 ^ 1

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

nA = numof1(A)
nB = numof1(B)

if nA % 2 != nB % 2 and distance(nA, nB) < 2 ** N:
    print('YES')
    print(A, end='')
    v = A
    for i in range(2 ** N - 1):
        v = next(v, B)
        print(' ', end='')
        print(v, end='')
else:
    print('NO')

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def extract(a, i):
  return (a>>(i+1) << i) | (a&((1<<i)-1))

def space(a, i):
  return ((a>>i) << (i+1)) | (a&((1<<i)-1))

def compose(n, a, b):
  if n==1:
    return [a, b]
  for i in range(n):
    if (a>>i&1) ^ (b>>i&1):
      x = i
      a_bool = (a>>i & 1) << i
      b_bool = a_bool ^ (1 << i)
      a_dash = extract(a, i)
      b_dash = extract(b, i)
      c = a_dash ^ 1
      break
  Q = np.array(compose(n-1, a_dash, c), dtype=np.int64)
  R = np.array(compose(n-1, c, b_dash), dtype=np.int64)
  n_Q = space(Q, x) | a_bool
  n_R = space(R, x) | b_bool
  return n_Q.tolist() + n_R.tolist()
  
n, a, b = map(int, input().split())
cnt = 0
c = a^b
for i in range(n):
  cnt += c>>i & 1
if cnt&1:
  print("YES")
  print(*compose(n, a, b))
else:
  print("NO")

Submission status: Time Limit Exceeded

Solution code:
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")