Problem:
 Score : 800 points    Problem Statement You are given integers N,\ A and B .
Determine if there exists a permutation (P_0,\ P_1,\ ...\ P_{2^N-1}) of (0,\ 1,\ ...\ 2^N-1) that satisfies all of the following conditions, and create one such permutation if it exists.   P_0=A  P_{2^N-1}=B  For all 0 \leq i < 2^N-1 , the binary representations of P_i and P_{i+1} differ by exactly one bit.       Constraints  1 \leq N \leq 17  0 \leq A \leq 2^N-1  0 \leq B \leq 2^N-1  A \neq B  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  A  B       Output If there is no permutation that satisfies the conditions, print NO .  If there is such a permutation, print YES in the first line.
Then, print (P_0,\ P_1,\ ...\ P_{2^N-1}) in the second line, with spaces in between.
If there are multiple solutions, any of them is accepted.        Sample Input 1 2 1 3      Sample Output 1 YES
1 0 2 3  The binary representation of P=(1,0,2,3) is (01,00,10,11) , where any two adjacent elements differ by exactly one bit.       Sample Input 2 3 2 1      Sample Output 2 NO

Solution (Python):
def 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 = -1
    for i in range(N):
        if (A ^ B) & (2**i):
            idx = i
            break
    ra = rm_bit(A, idx)
    rb = rm_bit(B, idx)
    a = list(map(lambda x: ins_bit(x, idx, (A >> idx) & 1), solve(N-1, ra, ra ^ 1)))
    b = list(map(lambda x: ins_bit(x, idx, (B >> idx) & 1), solve(N-1, ra ^ 1, rb)))
    return a + b


def main():
    N, A, B = map(int, input().split())
    x = A ^ B
    cnt = 0
    for i in range(N):
        if x & (2**i):
            cnt += 1
    if cnt % 2 == 0:
        print("NO")
    else:
        print("YES")
        print(*solve(N, A, B))


if __name__ == "__main__":
    main()

Solution (Python):
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 = -1
    for i in range(N):
        if (A ^ B) & (2**i):
            idx = i
            break
    ra = rm_bit(A, idx)
    rb = rm_bit(B, idx)
    a = list(map(lambda x: ins_bit(x, idx, (A >> idx) & 1), solve(N-1, ra, ra ^ 1)))
    b = list(map(lambda x: ins_bit(x, idx, (B >> idx) & 1), solve(N-1, ra ^ 1, rb)))
    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()

Solution (Python):
def dc(n):
    return sum(map(int, list(bin(n)[2:])))
def calc(a, b, X):
    if len(X) == 1:
        return [a, b]
    i = 0
    while i not in X or (a & (1<<i)) == (b & (1<<i)):
        i += 1
    j = X[1] if i == X[0] else X[0]
    Y = [x for x in X if x != i]
    return calc(a, a ^ (1<<j), Y) + calc(a ^ (1<<j) ^ (1<<i), b, Y)

N, A, B = map(int, input().split())
if dc(A^B) % 2 == 0:
    print("NO")
else:
    print("YES")
    print(*calc(A, B, list(range(N))))

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


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

print('YES')
visited = set([A])
now = A
targets = [[B, set([2**i for i in range(N)])]]
print(now)
while True:
  if now == B:
    break
  target, bits = targets[-1]
  
  
  xor = now ^ target
  flag = False
  for i in bits:
    if xor & i:
      for j in bits:
        if j != i and now ^ j not in visited:
          targets.append([now ^ j ^ i, {j}])
          targets.append([now ^ j, bits - {i}])
          flag = True
          break
      if flag:
        break
  else:
    
    now = target
    visited.add(now)
    targets.pop()
    print(now)

Solution (Python):
N, A, B = map(int, input().split())
AB = A ^ B
D = bin(AB)[2:].count('1')
if D % 2 == 0:
  print('NO')
else:
  print('YES')
  C = [0] * (2 ** N)
  C[0] = str(A)
  P = []
  for i in range(N):
    if AB % 2:
      P = P + [i]
    else:
      P = [i] + P
    AB >>= 1
  for i in range(1, 2 ** N):
    k = 0
    b = i
    while b % 2 == 0:
      k += 1
      b >>= 1
    A ^= 1 << P[k]
    C[i] = str(A)
  k = 0
  for i in range(D // 2):
    k += 1 / (4 ** (i + 1))
    K = int(2 ** N * (1 - k))
    C = C[:K-1] + C[K+1:] + C[K-1:K+1][::-1]
  print(' '.join(C))

Solution (Python):
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()
    X = A ^ B
    if bin(X).count('1') % 2 == 0:
        print('No')
        return 0
    print('Yes')
    
    m = []
    s = []
    for i in range(N):
        if (X // (2 ** i)) % 2:
            m.append(i)
        else:
            s.append(i)
    ans = [0] * (2 ** N)
    def get(se, ms):
        if len(ms) > 2:
            return get(ms[1:2] + se, ms[2:]) + ms[0:1] + get(ms[2:] + se, ms[1:2])
        elif len(ms) == 1:
            return get(se, []) + ms[0:1] + get(se, [])
        if len(se) == 0:
            return []
        return get(se[1:], []) + se[0:1] + get(se[1:], [])
    swap = get(s, m)
    ans = [A]
    for sw in swap:
        ans.append(ans[-1] ^ (2 ** sw))
    print(*ans)

    return 0

main()

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


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

print('YES')
visited = set([A])
n = 1
now = A
xbits = set([2**i for i in range(N)])
is_neighbor = lambda a, b: (a ^ b) in xbits
targets = [(B, xbits)]
M = 2 ** N - 1
print(now)
while True:
  if now == B:
    break
  target, bits = targets[-1]
  
  
  xor = now ^ target
  flag = False
  for i in bits:
    if xor & i:
      for j in bits:
        if j != i and now ^ j not in visited:
          targets.append((now ^ j ^ i, {j}))
          targets.append((now ^ j, bits - {i}))
          flag = True
          break
      if flag:
        break
  else:
    
    now = target
    visited.add(now)
    n += 1
    print(now)
    targets.pop()

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

ans = []


def f(s, d):
    if bin(d).count('1') == 1:
        ans.append(s)
        ans.append(s ^ d)
        return
    else:
        if (s & ~d == B & ~d) and ((s & d) ^ (B & d) != ((1 << N) - 1)):
            d1 = d - (1 << next(i for i in range(N) if s & (1 << i) != B & (1 << i)))
        else:
            d1 = d - (1 << next(i for i in range(N) if (d >> i) & 1))
        
        f(s, d1)
        t = ans[-1] ^ (d ^ d1)
        d2 = d - (d ^ d1)
        f(t, d2)
        return


if sum((A & (1 << i)) != (B & (1 << i)) for i in range(N)) % 2 == 0:
    print('NO')
else:
    print('YES')
    f(A, ((1 << N) - 1))
    print('\n'.join(map(str, ans)))

Solution (Python):
def solve():
    N, A, B = map(int, input().split())
    X = A ^ B
    
    x = X
    s = 0
    while x > 0:
        s += x % 2
        x //= 2
    
    if s % 2 == 0:
        print("NO")
        return
    print("YES")
    g = []
    for i in range(2 ** N):
        g.append(i ^ (i >> 1))
    
    i = 0
    while True:
        print(g[i] ^ A, end = " ")
        if i % 2 == 0:
            j = 2 ** N - 1 - i
        elif j < 2 ** (N - 1):
            j += 1
        else:
            j -= 1
        if g[j] == X:
            break
        i = j
    if i < 2 ** (N - 1):
        for k in range(i + 1, j + 1):
            print(g[k] ^ A, end = " ")
    else:
        for k in range(i - 1, j - 1, -1):
            print(g[k] ^ A, end = " ")
    return
        
solve()

Solution (Python):
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 topbit(a, N):
  return next(i for i in range(N-1, -1, -1) if (a & (1 << i)) > 0)

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):
  if N == 1:
    ans = [A & 1, B & 1]
    return ans
  C = A ^ B
  t = topbit(C, N)
  AA = swap(A, t, N-1)
  BB = swap(B, t, N-1)
  D = AA ^ 1
  ans = []
  if B & (1 << t) > 0:
    ans.extend(swap(x, t, N-1) for x in solve(N-1, AA, D))
    ans.extend(swap(x, t, N-1) | (1 << t) for x in solve(N-1, D, BB))
  else:
    ans.extend(swap(x, t, N-1) | (1 << t)for x in solve(N-1, AA, D))
    ans.extend(swap(x, t, N-1) for x in solve(N-1, D, BB))
  return ans

N, A, B = reads()
if bitcount(A ^ B, N) % 2 == 0:
  print("NO"); exit()
print("YES")
print(*solve(N, A, B))

Solution (Python):
N,A,B=map(int,input().split())
C=[]
D=[]
tmp=1
for i in range(N):
    if A^tmp>A:
        C.append(0)
    else:
        C.append(1)
    if B^tmp>B:
        D.append(0)
    else:
        D.append(1)
    tmp=tmp*2
X=[];Y=[]
S=0
T=0
for i in range(N):
    if C[i]==D[i]:
        X.append(i)
        if C[i]==1:
            S+=2**i
    else:
        Y.append(i)
        if C[i]==1:
            T+=2**i

if len(Y)%2==0:
    print("NO")
    exit()
def xadd(Lst,k):
    Res=[]
    for i in Lst:
        Res.append(i)
    for i in Lst[::-1]:
        Res.append(i+2**k)
    return Res
XL=[0]
if len(X)!=0:
    XL.append(2**X[0])
for i in range(1,len(X)):
    XL=xadd(XL,X[i])
XL=[i^S for i in XL]
YL=[0,2**Y[0]]
def yadd2(Lst,k1,k2):
    Res=[]
    for i in range(len(Lst)-1):
        if i%2==0:
            Res.append(Lst[i]+0*2**k1+0*2**k2)
            Res.append(Lst[i]+0*2**k1+1*2**k2)
            Res.append(Lst[i]+1*2**k1+1*2**k2)
            Res.append(Lst[i]+1*2**k1+0*2**k2)
        else:
            Res.append(Lst[i]+1*2**k1+0*2**k2)
            Res.append(Lst[i]+1*2**k1+1*2**k2)
            Res.append(Lst[i]+0*2**k1+1*2**k2)
            Res.append(Lst[i]+0*2**k1+0*2**k2)
    Res.append(Lst[-1]+1*2**k1+0*2**k2)
    Res.append(Lst[-1]+0*2**k1+0*2**k2)
    Res.append(Lst[-1]+0*2**k1+1*2**k2)
    Res.append(Lst[-1]+1*2**k1+1*2**k2)
    return Res
for i in range(len(Y)//2):
    YL=yadd2(YL,Y[-1-2*i],Y[-2-2*i])
YL=[i^T for i in YL]

ans=[]
for i in range(len(YL)):
    for j in range(len(XL)):
        if i%2==0:
            ans.append(YL[i]+XL[j])
        else:
            ans.append(YL[i]+XL[-j-1])
print("YES")
print(" ".join([str(i) for i in ans]))

Solution (Python):
def split(n, a, b, used, ai, ans, width):
    
    
    
    if width == 2:
        ans[ai] = a
        ans[ai + 1] = b
        return
    x = a ^ b
    y = x & -x  
    l = y.bit_length() - 1
    used.append(l)
    i = 0
    for i in range(n):
        if i not in used:
            break
    la, lb = a, a ^ (1 << i)
    ra, rb = lb ^ y, b
    width >>= 1
    split(n, la, lb, used, ai, ans, width)
    split(n, ra, rb, used, ai + width, ans, width)
    used.pop()


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


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