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:
print("NO")

Submission status: Wrong Answer

Solution code:
#パリティのみ?
#未証明で適当に再帰

n,a,b=map(int, input().split())
p=0
for i in range(n):
    p^=(a>>i)&1
    p^=(b>>i)&1
if not p:
    print("NO")
    exit()

def rec(x,y,m):
    if m==1:
        return [x,y]
    b2= ((1<<m)-1)^x
    b1= ((1<<m)-1)^y
    return rec(x,b1,m-1)+rec(b2,y,m-1)
ans=rec(a,b,n)
print("YES")
print(*ans)

Submission status: Wrong Answer

Solution code:
def differ_by_1bit(n,end):
    if n==1:
        return [0,1]
    list=differ_by_1bit(n-1,0)
    for i in range(len(list)):
        q=list[i]//2**end
        r=list[i]%2**end
        list[i]=q*2**(end+1)+r
    list2=[2**end+list[-i-1] for i in range(len(list))]
    return list+list2

N,A,B=map(int,input().split())
check=B^A
if check&(check-1)==0:
    t=0
    for i in range(N):
        if check>>i &1==1:
            t=i
            break
    res=differ_by_1bit(N,t)
    for i in range(len(res)):
        res[i]=res[i]^A
    print("YES")
    print(*res)
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    popcnt=lambda x:bin(x).count("1")

    n,a,b=MI()
    if (popcnt(a)&1)^(popcnt(b)&1)==0:
        print("NO")
        exit()
    deg=[n]*(1<<n)
    ans=[a]
    deg[a]=n+1
    deg[b]=n+1
    for _ in range((1<<n)-2):
        mn=(n+1,-1,-1)   #deg,dist,nxt
        for i in range(n):
            cur=ans[-1]^1<<i
            if deg[cur]==n+1:continue
            dist=popcnt(b^cur)
            if (deg[cur],-dist,cur)<mn:
                mn=(deg[cur],-dist,cur)

        nxt=mn[2]
        ans.append(nxt)
        deg[nxt]=n+1
        for i in range(n):
            cur = nxt ^ 1 << i
            if deg[cur] == n + 1: continue
            deg[cur]-=1

    ans.append(b)
    print("YES")
    print(*ans)
    #for x in ans:print(format(x,"b").zfill(n))

main()

Submission status: Wrong Answer

Solution code:
n,a,b = map(int,input().split())
if bin(a^b).count("1")%2 == 0:
  print("NO")
  exit()
st = min(a,b)
dif = a^b
t = 1<<(n-1)
chls = []
chcnt = n-1
for i in range(n)[::-1]:
  if dif&1<<i:
    chls.append((chcnt,i))
    chcnt -= 1
def change(x,p,q):
  if (x>>p)&1 ^ (x>>q)&1:
    x ^= (1<<p)+(1<<q)
  return x
def changeall(x):
  x ^= st
  for l,r in chls:
    x = change(x,l,r)
  return x
if n == 1:
  print("YES")
  print(a,b)
  exit()
ls = []
x = 0
for i in range(1,2**n):
  cnt = 0
  while i%2 == 0:
    i //= 2
    cnt += 1
  ls.append(1<<cnt)
chcnt += 1
while chcnt < n-1:
  i = chcnt+1
  ls[-2**i] //= 2
  ls[-2**(i-1)] *= 2
  ls[-2**(i-1)-2**i] *= 2
  chcnt += 2
nls = [0]
for i in ls:
  nls.append(nls[-1]^i)
print("YES")
ansls = [changeall(nls[i]) for i in range(2**n)]
if a < b:
  print(*ansls)
else:
  print(*ansls[::-1])

Submission status: Wrong Answer

Solution code:
n,a,b = map(int,input().split())
for i in range(n):
  if a^b == 1<<i:
    break
else:
  print("NO")
  exit()
st = min(a,b)
bt = (a^b).bit_length()-1
def change(x):
  x ^= st
  if ((x>>bt)&1) ^ ((x>>(n-1))&1):
    x ^= (1<<bt)+(1<<(n-1))
  return x
if n == 1:
  print("YES")
  print(a,b)
  exit()
ls = [0]
x = 0
cnt = 1
while x != 1<<(n-1):
  cntc = cnt
  divcnt = 0
  while cntc%2 == 0:
    cntc //= 2
    divcnt += 1
  x ^= 1<<divcnt
  ls.append(x)
  cnt += 1
print("YES")
ansls = [change(ls[i]) for i in range(2**n)]
if a < b:
  print(*ansls)
else:
  print(*ansls[::-1])

Submission status: Wrong Answer

Solution code:
n,a,b = map(int,input().split())
for i in range(n):
  if a^b == 1<<i:
    break
else:
  print("NO")
  exit()
st = min(a,b)
bt = (a^b).bit_length()-1
def change(x):
  x ^= st
  if ((x>>bt)&1) ^ ((x>>(n-1))&1):
    x ^= (1<<bt)+(1<<(n-1))
  return x
if n == 1:
  print("YES")
  print(a,b)
  exit()
ls = [0]
x = 0
cnt = 1
while x != 1<<(n-1):
  cntc = cnt
  divcnt = 0
  while cntc%2 == 0:
    cntc //= 2
    divcnt += 1
  x ^= 1<<divcnt
  ls.append(x)
  cnt += 1
print("YES")
ansls = [change(ls[i]) for i in range(2**n)]
print(*ansls)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, a, b = map(int, input().split())
ba = "{:020b}".format(a)[::-1]
bb = "{:020b}".format(b)[::-1]
d = [False] * 20
for i in range(20):
    if ba[i] != bb[i]:
        d[i] = True
bn = (1 << n) - 1
sd = sum(d)
if sd % 2 == 0 or sd > bn - 1:
    print("NO")
    exit()
print("YES")
print(a, end="")
for k in range(bn):
    if k < sd:
        for i in range(20):
            if d[i]:
                d[i] = False
                a = a ^ (1 << i)
                print("", a, end="")
                break
    else:
        a ^= 1
        print("", a, end="")
print("")

Submission status: Wrong Answer

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

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

# グレイコードのリスト生成
gray = [0] * (1 << n)
for i in range(1 << n):
    gray[i] = i ^ (i >> 1)

# グレイコードリストでのa, bの位置探索
ind_a, ind_b = -1, -1
for i in range(1 << n):
    if gray[i] == a:
        ind_a = i
    if gray[i] == b:
        ind_b = i
if ind_a > ind_b:
    ind_a, ind_b = ind_b, ind_a

# a->b までの距離と b->a までの距離でより長い方をベースに使う
res1 = [gray[i] for i in range(ind_a, ind_b + 1)]
res2 = [gray[i] for i in range(ind_b, len(gray))] + [gray[i] for i in range(0, ind_a + 1)]
if len(res1) > len(res2):
    res = res1[0:]
    nokori = res2[1:-1]
else:
    res = res2[0:]
    nokori = res1[1:-1]

# 余ったほうをベースに加えるための整理（cnt1で分類）
nokori_memo = {}
for i in range(0, len(nokori), 2):
    tmp = nokori[i:i + 2]
    cnt0 = bin(tmp[0]).count("1")
    cnt1 = bin(tmp[1]).count("1")
    if (cnt0, cnt1) not in nokori_memo:
        nokori_memo[cnt0, cnt1] = []
    nokori_memo[cnt0, cnt1].append(tmp)

# ベースとあまりをマージ
ans = []
while res:
    num = res.pop()
    ans.append(num)
    num = bin(num).count("1")
    while (num + 1, num) in nokori_memo and nokori_memo[(num + 1, num)]:
        tmp = nokori_memo[(num + 1, num)].pop()
        ans.append(tmp[0])
        ans.append(tmp[1])
    while (num - 1, num) in nokori_memo and nokori_memo[(num - 1, num)]:
        tmp = nokori_memo[(num - 1, num)].pop()
        ans.append(tmp[0])
        ans.append(tmp[1])
    while (num, num + 1) in nokori_memo and nokori_memo[(num, num + 1)]:
        tmp = nokori_memo[(num, num + 1)].pop()
        ans.append(tmp[1])
        ans.append(tmp[0])
    while (num, num - 1) in nokori_memo and nokori_memo[(num, num - 1)]:
        tmp = nokori_memo[(num, num - 1)].pop()
        ans.append(tmp[1])
        ans.append(tmp[0])

print("YES")
if ans[0] == a:
    print(*ans)
else:
    print(*ans[::-1])

Submission status: Wrong Answer

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

data=[[] for i in range(2**N)]

lst=[[] for i in range(N+1)]
for i in range(2**N):
    cnt=0
    for j in range(N):
        if i&(1<<j)==0:
            continue
        cnt+=1
    lst[cnt].append(i)
    
for que in lst:
    for u in que:
        for j in range(N):
            if u&(1<<j)==0:
                v=u+(1<<j)
                data[u].append(v)
                data[v].append(u)

flag=[0]*2**N
u=A
flag[u]=1
ans=[u]
for i in range(2**N-1):
    for v in data[u]:
        if flag[v]==0 and v!=B:
            flag[v]=1
            u=v
            ans.append(v)
            break
    else:
        ans.append(B)
        break
if len(ans)==2**N:
    print("YES")
    print(*ans)
else:
    print("NO")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
d2lis = {}
for i in range(20):
    d2lis[2**i] = i

def f(start,goal,usedbit):

    #異なるbitかつ未使用を見つけよう
    for i in range(N):
        if ( ( (2**i) & usedbit == 0) and (start&(2**i) != goal&(2**i)) ):
            usedbit |= 2**i
            break

    #もう一つ未使用bitを見つけよう→見つからなかった時の処理も書く

    flag = False
    for i in range(N):
        if ((2**i) & usedbit == 0):
            flag = True
            mids = start ^ (2**i)
            midg = goal  ^ (2**i)
            break

    if not flag:
        return [start,goal]
    else:
        ret = f(start,mids,usedbit) + f(midg,goal,usedbit)
        return ret #答えのリスト

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

#print (format(A,"b").zfill(N))
#print (format(B,"b").zfill(N))

#numbit = 0
#for i in range(N):
#    if ((A^B) & (2**i)) > 0:
#        numbit += 1
#if numbit % 2 == 0:
#    print ("NO")
#    sys.exit()

ans = f(A,B,0)

for i in range(2**N-1):
    if abs(ans[i+1]-ans[i]) not in d2lis:
        print ("NO")
        sys.exit()

print ("YES")
print (" ".join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
import sys
d2lis = {}
for i in range(20):
    d2lis[2**i] = i

def f(start,goal,usedbit):

    #異なるbitかつ未使用を見つけよう
    for i in range(N):
        if ( ( (2**i) & usedbit == 0) and (start&(2**i) != goal&(2**i)) ):
            usedbit |= 2**i
            break

    #もう一つ未使用bitを見つけよう→見つからなかった時の処理も書く

    flag = False
    for i in range(N):
        if ((2**i) & usedbit == 0):
            flag = True
            mids = start ^ (2**i)
            midg = goal  ^ (2**i)
            break

    if not flag:
        return [start,goal]
    else:
        ret = f(start,mids,usedbit) + f(midg,goal,usedbit)
        return ret #答えのリスト

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

#print (format(A,"b").zfill(N))
#print (format(B,"b").zfill(N))

numbit = 0
for i in range(N):
    if ((A^B) & (2**i)) > 0:
        numbit += 1
if numbit % 2 == 0:
    print ("NO")
    sys.exit()

ans = f(A,B,0)

ans2 = ans.copy()
ans2.sort()
for i in range(2**N):
    if i != ans2[i]:
        print (assaasa)

print ("YES")
print (" ".join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
import sys
d2lis = {}
for i in range(20):
    d2lis[2**i] = i

def f(start,goal,usedbit):

    #異なるbitかつ未使用を見つけよう
    for i in range(N):
        if ( ( (2**i) & usedbit == 0) and (start&(2**i) != goal&(2**i)) ):
            usedbit |= 2**i
            break

    #もう一つ未使用bitを見つけよう→見つからなかった時の処理も書く

    flag = False
    for i in range(N):
        if ((2**i) & usedbit == 0):
            flag = True
            mids = start ^ (2**i)
            midg = goal  ^ (2**i)
            break

    if not flag:
        return [start,goal]
    else:
        ret = f(start,mids,usedbit) + f(midg,goal,usedbit)
        return ret #答えのリスト

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

numbit = 0
for i in range(N):
    if (A^B) & (2**i) > 0:
        numbit += 1
if numbit % 2 == 0:
    print ("NO")
    sys.exit()

ans = f(A,B,0)

if len(ans) != 2**N:
    print (asssa)
print ("YES")
print (" ".join(map(str,ans)))

Submission status: Wrong Answer

Solution code:

"""
ハミング距離がちょうど1
絶対不可能なのは、AとBの1の数の偶奇が違うとき
それ以外は可能か？

可能と仮定して構成法を考えてみる
ハミング距離を1だけ変化させる方法
→2**xを xorする

AとBが1桁だけ違う場合は可能、それ以外は不可能？
→1回だけ選ぶ桁は1つしかないのでおｋそう

根本的に違う？
→最初と最後のハミング距離が1以外も可能？
→いやー無理やろ

N=1 → 必ずハミング1になる
N=2 → 必ずハミング1になる
N=3 →

ある桁にxorするのを考える
1,0良い感じに分配→2個セットで分配してく
1個小さいの奴より2倍以上になってはダメ

差が2以下になるように分配可能
→分配したらどう取るか分かれば解ける

残り数が最大に奴から見ていく→未出ならやっちゃえばいい
たぶん行ける…たぶん…

→むりだった()

========ちょっとだけ解説を見た==========
ハミング距離が偶数なら無理、奇数なら可能

証明しよう
N = 1,2 の時　全列挙で自明
N >= 3のとき、そのN-1以下で全て成立すると仮定すると

0(0*(N-1))→0(ハミング奇数ALL)→1(ハミング奇数ALL)→1(ハミング偶数ALL)
となるので、ハミング奇数の奴をすべて構成できる(1の位置を途中に入れればすべて可能)

この考え方をどうやって構成に応用する？
→再帰っぽい
→またはdp?

小さい桁から合わせていく？

=========解説を見ました=============

再帰かぁ
やることは→始点と終点で違うbitを見つける
→そこで分割→始点未確定から1bitずらしたやつを前半終点・後半始点に据えて問題を2分割
必要なのは？→確定したbitを保持する変数
"""
import sys
d2lis = {}
for i in range(20):
    d2lis[2**i] = i

def f(start,goal,usedbit):

    #異なるbitかつ未使用を見つけよう
    for i in range(N):
        if ( ( (2**i) & usedbit == 0) and (start&(2**i) != goal&(2**i)) ):
            usedbit |= 2**i
            break

    #もう一つ未使用bitを見つけよう→見つからなかった時の処理も書く

    flag = False
    for i in range(N):
        if ((2**i) & usedbit == 0):
            flag = True
            mids = start ^ (2**i)
            midg = goal  ^ (2**i)
            break

    if not flag:
        return [start,goal]
    else:
        ret = f(start,mids,usedbit) + f(midg,goal,usedbit)
        return ret #答えのリスト

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

numbit = 0
for i in range(N):
    if (A^B) & (2**i) > 0:
        numbit += 1
if numbit % 2 == 0:
    print ("NO")
    sys.exit()

ans = f(A,B,0)
print ("YES")
print (" ".join(map(str,ans)))




#WA解答2 貪欲で構成しようとしてみてだめだった
"""
import sys

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

lis = [[0,i] for i in range(N)]
able = [True] * (2**N)
rem = 2**N-1

for i in range(N):
    if (A^B) & (2**i) > 0:
        lis[i][0] += 1
        rem -= 1

if rem % 2 == 1:
    print ("NO")
    sys.exit()

lis.sort()

for i in range(rem//2):
    lis[i % N][0] += 2

ans = []
#print (lis)
able[B] = False

for loop in range(2**N-1):

    if loop == 0:
        pass
    else:
        lis.sort()
        for i in range(N-1,-1,-1):
            if able[A ^ (2 ** lis[i][1])]:
                lis[i][0] -= 1
                if lis[i][0] < 0:
                    print ("NO")
                    sys.exit()
                A ^= 2 ** lis[i][1]
                break
        
    ans.append(A)
    able[A] = False

ans.append(B)
print ("YES")
print (" ".join(map(str,ans)))
"""

#WA解答1(そもそもハミングが1以外無理だと思ってた時)
"""
import sys

def want_min(x):

    for i in range(20):
        if (2**i)&x > 0:
            return i

d2lis = {}

for i in range(20):
    d2lis[2**i] = i

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


if A^B not in d2lis:
    print ("NO")
    sys.exit()

print ("YES")

upk = d2lis[A^B]
ans = []

for i in range(2**N):

    if i == 0:
        ans.append(A)
    elif i == 2 ** (N-1):
        A ^= 2 ** upk
        ans.append(A)
    else:
        keta = want_min(i)
        if keta >= upk:
            keta += 1
        A ^= 2**keta
        ans.append(A)

    #print (format(A,"b").zfill(N))

#print (ans[-1])
print (" ".join(map(str,ans)))

"""

Submission status: Wrong Answer

Solution code:

import sys

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

lis = [[0,i] for i in range(N)]
able = [True] * (2**N)
rem = 2**N-1

for i in range(N):
    if (A^B) & (2**i) > 0:
        lis[i][0] += 1
        rem -= 1

if rem % 2 == 1:
    print ("NO")
    sys.exit()

lis.sort()

for i in range(rem//2):
    lis[i % N][0] += 2

ans = []
#print (lis)
able[B] = False

for loop in range(2**N-1):

    if loop == 0:
        pass
    else:
        lis.sort()
        for i in range(N-1,-1,-1):
            if able[A ^ (2 ** lis[i][1])]:
                lis[i][0] -= 1
                if lis[i][0] < 0:
                    print ("NO")
                    sys.exit()
                A ^= 2 ** lis[i][1]
                break
        
    ans.append(A)
    able[A] = False

ans.append(B)
print ("YES")
print (" ".join(map(str,ans)))

Submission status: Wrong Answer

Solution code:

"""
ハミング距離がちょうど1
絶対不可能なのは、AとBの1の数の偶奇が違うとき
それ以外は可能か？

可能と仮定して構成法を考えてみる
ハミング距離を1だけ変化させる方法
→2**xを xorする

AとBが1桁だけ違う場合は可能、それ以外は不可能？
→1回だけ選ぶ桁は1つしかないのでおｋそう

根本的に違う？
→最初と最後のハミング距離が1以外も可能？
→いやー無理やろ

N=1 → 必ずハミング1になる
N=2 → 必ずハミング1になる
N=3 →

ある桁にxorするのを考える
1,0良い感じに分配→2個セットで分配してく
1個小さいの奴より2倍以上になってはダメ

差が2以下になるように分配可能
→分配したらどう取るか分かれば解ける

残り数が最大に奴から見ていく→未出ならやっちゃえばいい
たぶん行ける…たぶん…
"""

import sys

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

lis = [[0,i] for i in range(N)]
able = [True] * (2**N)
rem = 2**N-1

for i in range(N):
    if (A^B) & (2**i) > 0:
        lis[i][0] += 1
        rem -= 1

if rem % 2 == 1:
    print ("NO")
    sys.exit()

lis.sort()

for i in range(rem//2):
    lis[i % N][0] += 2

ans = []
#print (lis)
able[B] = False

for loop in range(2**N-1):

    if loop == 0:
        pass
    else:
        lis.sort()
        for i in range(N-1,-1,-1):
            if able[A ^ (2 ** lis[i][1])]:
                lis[i][0] -= 1
                A ^= 2 ** lis[i][1]
                break
        
    ans.append(A)
    able[A] = False

ans.append(B)
print ("YES")
print (" ".join(map(str,ans)))

"""
import sys

def want_min(x):

    for i in range(20):
        if (2**i)&x > 0:
            return i

d2lis = {}

for i in range(20):
    d2lis[2**i] = i

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


if A^B not in d2lis:
    print ("NO")
    sys.exit()

print ("YES")

upk = d2lis[A^B]
ans = []

for i in range(2**N):

    if i == 0:
        ans.append(A)
    elif i == 2 ** (N-1):
        A ^= 2 ** upk
        ans.append(A)
    else:
        keta = want_min(i)
        if keta >= upk:
            keta += 1
        A ^= 2**keta
        ans.append(A)

    #print (format(A,"b").zfill(N))

#print (ans[-1])
print (" ".join(map(str,ans)))

"""

Submission status: Wrong Answer

Solution code:

"""
ハミング距離がちょうど1
絶対不可能なのは、AとBの1の数の偶奇が違うとき
それ以外は可能か？

可能と仮定して構成法を考えてみる
ハミング距離を1だけ変化させる方法
→2**xを xorする

AとBが1桁だけ違う場合は可能、それ以外は不可能？
→1回だけ選ぶ桁は1つしかないのでおｋそう

根本的に違う？
→最初と最後のハミング距離が1以外も可能？
→いやー無理やろ

N=1 → 必ずハミング1になる
N=2 → 必ずハミング1になる
N=3 →

ある桁にxorするのを考える
1,0良い感じに分配→2個セットで分配してく
1個小さいの奴より2倍以上になってはダメ

差が2以下になるように分配可能
→分配したらどう取るか分かれば解ける

残り数が最大に奴から見ていく→未出ならやっちゃえばいい
たぶん行ける…たぶん…
"""

import sys

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

lis = [[0,i] for i in range(N)]
able = [True] * (2**N)
rem = 2**N-1

for i in range(N):
    if (A^B) & (2**i) > 0:
        lis[i][0] += 1
        rem -= 1

if rem % 2 == 1:
    print ("NO")
    sys.exit()

lis.sort()

for i in range(rem//2):
    lis[i % N][0] += 2

ans = []

for loop in range(2**N):

    if loop == 0:
        pass
    else:
        lis.sort()
        for i in range(N-1,-1,-1):
            if able[A ^ (2 ** lis[i][1])]:
                lis[i][0] -= 1
                A ^= 2 ** lis[i][1]
                break
        
    ans.append(A)
    able[A] = False
        
print ("YES")
print (" ".join(map(str,ans)))

"""
import sys

def want_min(x):

    for i in range(20):
        if (2**i)&x > 0:
            return i

d2lis = {}

for i in range(20):
    d2lis[2**i] = i

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


if A^B not in d2lis:
    print ("NO")
    sys.exit()

print ("YES")

upk = d2lis[A^B]
ans = []

for i in range(2**N):

    if i == 0:
        ans.append(A)
    elif i == 2 ** (N-1):
        A ^= 2 ** upk
        ans.append(A)
    else:
        keta = want_min(i)
        if keta >= upk:
            keta += 1
        A ^= 2**keta
        ans.append(A)

    #print (format(A,"b").zfill(N))

#print (ans[-1])
print (" ".join(map(str,ans)))

"""

Submission status: Wrong Answer

Solution code:

"""
ハミング距離がちょうど1
絶対不可能なのは、AとBの1の数の偶奇が違うとき
それ以外は可能か？

可能と仮定して構成法を考えてみる
ハミング距離を1だけ変化させる方法
→2**xを xorする

AとBが1桁だけ違う場合は可能、それ以外は不可能？
→1回だけ選ぶ桁は1つしかないのでおｋそう

"""
import sys

def want_min(x):

    for i in range(20):
        if (2**i)&x > 0:
            return i

d2lis = {}

for i in range(20):
    d2lis[2**i] = i

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

if A^B not in d2lis:
    print ("NO")
    sys.exit()

print ("YES")

upk = d2lis[A^B]
ans = []

for i in range(2**N):

    if i == 0:
        ans.append(A)
    elif i == 2 ** (N-1):
        A ^= 2 ** upk
        ans.append(A)
    else:
        keta = want_min(i)
        if keta >= upk:
            keta += 1
        A ^= 2**keta
        ans.append(A)

    #print (format(A,"b").zfill(N))

print (" ".join(map(str,ans)))

Submission status: Wrong Answer

Solution code:

"""
ハミング距離がちょうど1
絶対不可能なのは、AとBの1の数の偶奇が違うとき
それ以外は可能か？

可能と仮定して構成法を考えてみる
ハミング距離を1だけ変化させる方法
→2**xを xorする

AとBが1桁だけ違う場合は可能、それ以外は不可能？
→1回だけ選ぶ桁は1つしかないのでおｋそう

"""
import sys

def want_min(x):

    for i in range(20):
        if (2**i)&x > 0:
            return i

d2lis = {}

for i in range(18):
    d2lis[2**i] = i

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

if A^B not in d2lis:
    print ("NO")
    sys.exit()

print ("YES")

upk = d2lis[A^B]
ans = []

for i in range(2**N):

    if i == 0:
        ans.append(A)
    elif i == 2 ** (N-1):
        A ^= 2 ** upk
        ans.append(A)
    else:
        keta = want_min(i)
        if keta >= upk:
            keta += 1
        A ^= 2**keta
        ans.append(A)

print (*ans)

Submission status: Wrong Answer

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, A, B = list(map(int, sys.stdin.buffer.readline().split()))


# 解説
# 違うbitの数が奇数なら戻ってこれる


def bitcount(n):
    ret = 0
    while n > 0:
        ret += n & 1
        n >>= 1
    return ret


# vs = []
# for i in range(1 << N):
#     for j in range(i, 1 << N):
#         if bitcount(i ^ j) == 1:
#             vs.append((i, j))
# plot_graph(vs)


# @debug
def solve(n, a, b):
    if n <= 0:
        return []
    if bitcount(a ^ b) % 2 == 0:
        return []
    if n == 1:
        return [a, b]
    # 奇数
    # 違うbitを1つ取り除く
    mask = 1
    while (a ^ b) & mask == 0:
        mask <<= 1
    na = a // (mask << 1) + a % mask
    nb = b // (mask << 1) + b % mask
    mid = na ^ 1
    ret = []
    for r in solve(n - 1, na, mid):
        ret.append((r // mask * mask << 1) + (a & mask) + r % mask)
    for r in solve(n - 1, mid, nb):
        ret.append((r // mask * mask << 1) + (b & mask) + r % mask)
    return ret


ans = solve(N, A, B)
if ans:
    print('YES')
    print(*ans)
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, A, B = list(map(int, sys.stdin.buffer.readline().split()))


# 解説
# 違うbitの数が奇数なら戻ってこれる


def bitcount(n):
    ret = 0
    while n > 0:
        ret += n & 1
        n >>= 1
    return ret


# vs = []
# for i in range(1 << N):
#     for j in range(i, 1 << N):
#         if bitcount(i ^ j) == 1:
#             vs.append((i, j))
# plot_graph(vs)


# @debug
def solve(n, a, b):
    if n <= 0:
        return []
    if bitcount(a ^ b) % 2 == 0:
        return []
    if n == 1:
        return [a, b]
    # 奇数
    # 違うbitを1つ取り除く
    mask = 1
    while (a ^ b) & mask == 0:
        mask <<= 1
    na = a // (mask << 1) + a % mask
    nb = b // (mask << 1) + b % mask
    mid = na ^ 1
    ret = []
    for r in solve(n - 1, na, mid):
        ret.append((r // mask << 1) + (a & mask) + r % mask)
    for r in solve(n - 1, mid, nb):
        ret.append((r // mask << 1) + (b & mask) + r % mask)
    return ret


ans = solve(N, A, B)
if ans:
    print('YES')
    print(*ans)
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, A, B = list(map(int, sys.stdin.buffer.readline().split()))


# 解説
# 違うbitの数が奇数なら戻ってこれる


def bitcount(n):
    ret = 0
    while n > 0:
        ret += n & 1
        n >>= 1
    return ret


# vs = []
# for i in range(1 << N):
#     for j in range(i, 1 << N):
#         if bitcount(i ^ j) == 1:
#             vs.append((i, j))
# plot_graph(vs)


# @debug
def solve(n, a, b):
    if n <= 0:
        return []
    if bitcount(a ^ b) % 2 == 0:
        return []
    if n == 1:
        return [a, b]
    # 奇数
    # 違うbitを1つ取り除く
    mask = 1
    while a ^ b & mask == 0:
        mask <<= 1
    na = a // (mask << 1) + a % mask
    nb = b // (mask << 1) + b % mask
    mid = na ^ 1
    ret = []
    for r in solve(n - 1, na, mid):
        ret.append((r // mask << 1) + (a & mask) + r % mask)
    for r in solve(n - 1, mid, nb):
        ret.append((r // mask << 1) + (b & mask) + r % mask)
    return ret


ans = solve(N, A, B)
if ans:
    print('YES')
    print(*ans)
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import sys
num=0
N,A,B=map(int,input().split())
flag=[0]*2**N
C=A^B
cnt=0
for i in range(N):
    if (C>>i)&1==1:
        cnt+=1
if cnt%2==0:
    print("NO")
    sys.exit()
flag[0]=1
flag[C]=1
ans=[0]
for i in range(2**N-2):
    for j in range(N):
        zzz=num^(1<<j)
        if flag[zzz]==0:
            flag[zzz]=1
            num=zzz
            ans.append(num)
            break
ans.append(C)
lst=[A]
for i in range(1,2**N):
    lst.append(A^ans[i])
print("YES")
print(*lst)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    def parity_onebit(x):
        return bin(x).count("1") & 1

    def make_route(s, g, state):
        if state.count(-1) == 1:
            return [s, g]
        sep_dig = bin(s ^ g)[::-1].find("1")
        s_bit = (s >> sep_dig) & 1
        Lstate = state[:]
        Rstate = state[:]
        Lstate[sep_dig] = s_bit
        Rstate[sep_dig] = 1 - s_bit
        for i in range(16):
            ng = 0
            i_copy = i
            for sk in Lstate[::-1]:
                ng <<= 1
                if sk == -1:
                    ng += i_copy & 1
                    i_copy >>= 1
                else:
                    ng += sk
            ns = ng ^ (1 << sep_dig)
            if ng != s and parity_onebit(ng) != parity_onebit(s) and ns != g:
                break
        res = make_route(s, ng, Lstate)
        state[sep_dig] = 1 - s_bit
        res += make_route(ns, g, Rstate)
        return res

    n, s, g = map(int, input().split())
    if parity_onebit(s) == parity_onebit(g):
        print("NO")
        exit()
    ans=make_route(s, g, [-1] * n)
    print(*ans)

main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

def seq(start,bits):
    arr = [0]
    for i in bits:
        arr += [x^(1<<i) for x in arr[::-1]]
    return [x^start for x in arr]

def solve(A,B,rest):
    if len(rest) == 1:
        return [A,B]
    if len(rest) == 2:
        i,j = rest
        x,y = 1<<i,1<<j
        if A^x == B:
            return [A,A^y,A^x^y,B]
        elif A^y == B:
            return [A,A^x,A^x^y,B]
        else:
            return None
    diff_bit = [i for i in rest if (A&(1<<i)) != (B&(1<<i))]
    if len(diff_bit) % 2 == 0:
        return None
    i = diff_bit[0]
    rest = [j for j in rest if j != i]
    if A^(1<<rest[0])^(1<<i) == B:
        rest[0],rest[1] = rest[1],rest[0]
    arr = seq(A,rest)
    return arr + seq(arr[-1]^(1<<i),rest)

rest = list(range(N))
answer = solve(A,B,rest)

if answer is None:
    print('NO')
else:
    print('YES')
    print(' '.join(map(str,answer)))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

def seq(start,bits):
    arr = [0]
    for i in bits:
        arr += [x^(1<<i) for x in arr[::-1]]
    return [x^start for x in arr]

def solve(A,B,rest):
    if len(rest) == 1:
        return [A,B]
    if len(rest) == 2:
        i,j = rest
        x,y = 1<<i,1<<j
        if A^x == B:
            return [A,A^y,A^x^y,B]
        elif A^y == B:
            return [A,A^x,A^x^y,B]
        else:
            return None
    diff_bit = [i for i in rest if A&(1<<i) != B&(1<<i)]
    if len(diff_bit) % 2 == 0:
        return None
    i = diff_bit[0]
    rest = [j for j in rest if j != i]
    if A^(1<<rest[0])^(1<<i) == B:
        rest[0],rest[1] = rest[1],rest[0]
    arr = seq(A,rest)
    return arr + seq(arr[-1]^(1<<i),rest)

rest = list(range(N))
answer = solve(A,B,rest)

if answer is None:
    print('NO')
else:
    print('YES')
    print(' '.join(map(str,answer)))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

def seq(start,bits):
    arr = [0]
    for i in bits:
        arr += [x+(1<<i) for x in arr[::-1]]
    return [x^start for x in arr]

def solve(A,B,rest):
    if len(rest) == 1:
        return [A,B]
    if len(rest) == 2:
        i,j = rest
        x,y = 1<<i,1<<j
        if A^x == B:
            return [A,A^y,A^x^y,B]
        elif A^y == B:
            return [A,A^x,A^x^y,B]
        else:
            return None
    diff_bit = [i for i in rest if A&(1<<i) != B&(1<<i)]
    if len(diff_bit) % 2 == 0:
        return None
    i = diff_bit[0]
    rest = [j for j in rest if j != i]
    if A^(1<<rest[0])^(1<<i) == B:
        rest[0],rest[1] = rest[1],rest[0]
    arr = seq(A,rest)
    return arr + seq(arr[-1]^(1<<i),rest)

rest = list(range(N))
answer = solve(A,B,rest)

if answer is None:
    print('NO')
else:
    print('YES')
    print(' '.join(map(str,answer)))

Submission status: Wrong Answer

Solution code:
n,a,b=map(int,input().split())
a=[[int(i)for i in format(a,'b').zfill(18)]]
b=[int(i)for i in format(b,'b').zfill(18)]
for i in range(2**n-1):
  t=a[-1][:]
  for i,(x,y)in enumerate(zip(t,b)):
    if x!=y:
      t[i]^=1
      break
  else:
    t[-1]^=1
  a+=t,
if a[-1]==b:
  print('YES')
  print(*[sum(2**(17-i)for i,v in enumerate(t)if v)for t in a])
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
n,a,b=map(int,input().split())
a=[[int(i)for i in format(a,'b').zfill(18)]]
b=[int(i)for i in format(b,'b').zfill(18)]
for i in range(2**n-1):
  t=a[-1][:]
  for i,(x,y)in enumerate(zip(t,b)):
    if x!=y:
      t[i]^=1
      break
  else:
    t[0]^=1
  a+=t,
if a[-1]==b:
  print('YES')
  print(*[sum(2**(17-i)for i,v in enumerate(t)if v)for t in a])
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
def gray_code(n):
    return n ^ (n >> 1)

n, a, b = map(int, input().split())
gray_list = []
for i in range(2**n):
    gray_list.append(gray_code(i))

for i in range(2**n):
    if gray_list[i] == a:
        ai = i

gray_list = gray_list[ai:] + gray_list[0:ai]

for i in range(2**n):
    if gray_list[i] == b:
        bi = i

if bin(gray_list[bi-1] ^ gray_list[-1]).count("1") != 1:
    print("NO")
else:
    print("YES")
    gray_list = gray_list[0:bi] + gray_list[bi:][::-1]
    print(" ".join(map(str, gray_list)))