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())
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])
used = [False for _ in range(N)]
ans = []
while not q.empty():
    l = q.get()
    v, k = l[0], l[1]
    if used[v]:
        continue
    ans.append(v)
    if v == B and k == N:
        break
    elif v == B and k < N:
        ans.pop(len(ans) - 1)
        continue
    used[v] = True
    for u in es[v]:
        if not used[u]:
            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 -*-
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_)]
        res = " ".join(str(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 = [int(i) for i in input().split() ]
 
diff = list([])
for i in range(n, 0, -1):
    x = 1 << (i-1)
    if (a&x) != (b&x):
        diff.append(x)
 
if ((len(diff) % 2) == 0):
    print('NO')
    exit()
else:
    print('YES')
    ax = a
    ans=str(ax)
    c = 2**n - 1
    for d in diff:
        if ax & d != 0:
            ax = ax & (~d)
        else:
            ax = ax | d
        ans += str(ax)
        c -= 1
    if c % 2 == 1:
        if (ax & 1) == 1:
            ax = (ax & ~1)
        else:
            ax = (ax | 1)
        ans += str(ax)
        c -= 1
 
    if (ax & 1) == 1:
        a1 = (ax & ~1)
        a2 = ax
    else:
        a1 = (ax | 1)
        a2 = ax
    aaa = str(a1) + str(a2)
    k=int(c/2)
    print(ans + (aaa*k))

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_)]
        res = " ".join(str(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:
def encode_gray(num):
    return num ^ (num >> 1)


def decode_gray(num):
    if num == 0:
        return 0
    bit_str = bin(num)[2:]
    bit_len = len(bit_str)
    result = "1"
    for idx in range(1, bit_len):
        last_bit = result[len(result) - 1]
        if bit_str[idx] == "1":
            result = result + "0" if last_bit == "1" else result + "1"
        else:
            result = result + last_bit
    return int(result, 2)


[N, A, B] = list(map(int, input().split()))
gray_A = decode_gray(A)
gray_B = decode_gray(B)
if abs(gray_A - gray_B) == 1:
    print("Yes")
    if gray_A > gray_B:
        for n in range(gray_A, 2 ** N - 1):
            print(encode_gray(n), end=" ")
        for n in range(0, gray_B):
            print(encode_gray(n), end=" ")
        print(B)
    else:
        for n in range(gray_A, -1, -1):
            print(encode_gray(n), end=" ")
        for n in range(2 ** N - 1, gray_B, -1):
            print(encode_gray(n), end=" ")
        print(B)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
N,A,B=[int(s) for s in input().split()]

def graycode(x):
    return x^(x>>1)

graycode_strings=[graycode(x) for x in range(2**N)]

I=0
for i, g in enumerate(graycode_strings):
    if g == A:
        I=i

if graycode_strings[I-1] == B:
    print("YES")
    print(' '.join([str(g) for g in (graycode_strings[I:] + graycode_strings[:I])]))
elif graycode_strings[(I+1)%(2**N)] == B:
    print("YES")
    l=graycode_strings[(I+1)%(2**N):]+graycode_strings[:(I+1)%(2**N)]
    l.reverse()
    print(' '.join([str(g) for g in l]))
else:
    print("No")

Submission status: Wrong Answer

Solution code:
N,A,B=[int(s) for s in input().split()]

def graycode(x):
    return x^(x>>1)

graycode_strings=[graycode(x) for x in range(2**N)]

I=0
for i, g in enumerate(graycode_strings):
    if g == A:
        I=i

if graycode_strings[I-1] == B:
    print("YES")
    print(' '.join([str(g) for g in (graycode_strings[I:] + graycode_strings[:I])]))
elif graycode_strings[(I+1)%(2**N)] == B:
    print("YES")
    l=graycode_strings[(I+1)%(2**N):]+graycode_strings[:(I+1)%(2**N)]
    l.reverse()
    print(' '.join([str(g) for g in l]))
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
N, A, B = [int(i) for i in input().split()]

def checkio(n, m):
    return format(n ^ m, 'b').count('1')

if 1 == checkio(A, B):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
from math import log


N, A, B = [int(elem) for elem in input().split()]
diff_bit = int(log(A ^ B, 2)) + 1

# def enumerate_digits_up(num):
#     result = set()
#     cur_digit = 0
#     while (1 << cur_digit) <= num:
#         if num & (1 << cur_digit):
#             result.add(cur_digit + 1)
#         cur_digit += 1
#     return result


# probably if A and B differ in more than one bit it's impossible.
def num_ups(num):
    res = 0
    while num:
        res += 1
        num -= num & -num
    return res


def gray_code(num_bits, diff_bit):
    if num_bits == 0:
        return [0]
    prev_gray_code = gray_code(num_bits - 1, diff_bit)
    if num_bits != diff_bit:
        return prev_gray_code + \
            [bits_without_msb | 1 << num_bits - 1 for bits_without_msb in reversed(prev_gray_code)]
    else:
        mask = (1 << num_bits - 1) - 1
        common_bits = A & mask
        # print(num_bits, common_bits)
        split_idx = prev_gray_code.index(common_bits)
        result = []
        result.extend(bits | 1 << num_bits - 1 for bits in reversed(prev_gray_code[:split_idx]))
        result.extend(prev_gray_code[:split_idx + 1])
        result.append(prev_gray_code[split_idx] | 1 << num_bits - 1)
        result.extend(bits | 1 << num_bits - 1 for bits in prev_gray_code[split_idx + 1:])
        result.extend(reversed(prev_gray_code[split_idx + 1:]))
        return result


def is_gray_code(perm):
    for i in range(len(perm) - 1):
        num1, num2 = perm[i], perm[i + 1]
        if num_ups(num1 ^ num2) != 1:
            return False
    return True


if num_ups(A ^ B) > 1:
    print("NO")
    exit(0)

# print(diff_bit)
# diff_bits = enumerate_digits_up(A ^ B)
# print(diff_bits)


# print(N, A, B)
perm = gray_code(N, diff_bit)
print("YES")
# print(perm)
if A < B:
    split_idx = perm.index(B)
    perm = perm[split_idx:] + perm[:split_idx]
    # print(is_gray_code(perm[::-1]))
    print(*perm[::-1])
else:
    split_idx = perm.index(A)
    perm = perm[split_idx:] + perm[:split_idx]
    # print(is_gray_code(perm))
    print(*perm)

Submission status: Wrong Answer

Solution code:
n,a,b=map(int,input().split())
if not sum(map(int,format(a^b,'b')))%2:
    print('No')
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
N, A, B = [int(_) for _ in input().split()]
if bin(A ^ B).count('1') != 1:
    print('NO')
    exit()
ans = [str(A)]
for X in range(N+10):
    if (A ^ B) >> X == 1:
        break
for i in range(1, 2**N):
    for Y in range(N+10):
        if i >> Y & 1:
            break
    A ^= 2**((X + Y + 1) % N)
    ans += [str(A)]
print('YES')
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
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 = [i] + P
    else:
      P = P + [i]
    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))

Submission status: Wrong Answer

Solution code:
n, x, y = [int(x) for x in input().strip().split(" ")]

bx = bin(x)[2:]
by = bin(y)[2:]
bxy = bin(x ^ y)[2:]
if len(bx) > len(by):
    by = '0' * (len(bx) - len(by)) + by
elif len(by) > len(bx):
    bx = '0' * (len(by) - len(bx)) + bx

c = 1

r=[x]
for i, b in enumerate(bxy):
    if b == '1':
        r.append(int(by[:i+1] + bx[i+1:], 2))
        c += 1

bb = by[-1]
bbb = by[:-1]
while True:
    if c == pow(2,n):
        break
    if bb == "0":
        r.append(int(bbb + "1",2))
        bb = "1"
    else:
        r.append(int(bbb + "0",2))
        bb = "0"
    c += 1

if r[-1] == y:
    print("YES")
    print(" ".join([str(rr) for rr in r]))
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
N, A, B = map(int, input().split())
a_str = bin(A)[2:]
b_str = bin(B)[2:]
a_bin = int(a_str)
b_bin = int(b_str)
a = [0] * (N+1)
b = [0] * (N+1)
c = [A] + [0]*(2**N-2) + [B]
# for i in range(len(a_str),2,-1):
# 	a.append(int(a_str[i-1]))
# for i in range(len(b_str),2,-1):
# 	b.append(int(b_str[i-1]))
cnta = 0
cntb = 0
while (a_bin):
	a[cnta] = a_bin%10
	a_bin = a_bin//10
	cnta += 1
while (b_bin):
	b[cntb] = b_bin%10
	b_bin = b_bin//10
	cntb += 1

# print(cnta,cntb)
d = A^B
h = 0
while(d):
	d &= d-1
	h += 1
# print(h)

if (2**N-h)%2 == 0 or h > 2**N-2:
	print('NO')
else:
	print('YES')
	j = 1
	C = A
	for i in range(max(cnta, cntb)):
		if a[i] != b[i]:
			if a[i] == 1:
				C -= 2**i
				c[j] = C
			else:
				C += 2**i
				c[j] = C
			j += 1
	for i in range(j,2**N-1,2):
		c[j],c[j+1] = c[j-2],c[j-1]
	for i in range(2**N):
		print(c[i], end=' ')
	print('')

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))

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

Submission status: Wrong Answer

Solution code:
n, x, y = [int(x) for x in input().strip().split(" ")]

bx = bin(x)[2:]
by = bin(y)[2:]
bxy = bin(x ^ y)[2:]
if len(bx) > len(by):
    by = '0' * (len(bx) - len(by)) + by
elif len(by) > len(bx):
    bx = '0' * (len(by) - len(bx)) + bx

c = 1

r=[x]
for i, b in enumerate(bxy):
    if b == '1':
        r.append(int(by[:i+1] + bx[i+1:], 2))
        c += 1

bb = by[-1]
bbb = by[:-1]
while True:
    if c == pow(2,n):
        break
    if bb == "0":
        r.append(int(bbb + "1",2))
        bb = "1"
    else:
        r.append(int(bbb + "0",2))
        bb = "0"
    c += 1

if r[-1] == y:
    print("YES")
    print(" ".join([str(rr) for rr in r]))
else:
    print("NO",end="")

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


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


def combination(n, r):
    return fact[n] // fact[r] // fact[n-r]


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
    visited = set()
    cnt = [0] * (N+1)
    for i in range(N+1):
        cnt[i] = combination(N, i)
    ans = [-1] * (2**i)
    b = i_to_b((2**N-1)-B, N)
    idx = 2**N - 1
    visited.add(B)
    ans[idx] = B
    cnt[sum(i_to_b(B, N))] -= 1
    for i in range(N):
        if b[i] == 1:
            idx -= 1
            B += 2**i
            visited.add(B)
            ans[idx] = B
            cnt[sum(i_to_b(B, N))] -= 1
    end = idx
    current = A
    idx = 0
    while idx < end:
        visited.add(current)
        ans[idx] = current
        b = i_to_b(current, N)
        c = sum(b)
        cnt[c] -= 1
        idx += 1
        if c >= 1 and cnt[c-1] >= 1:
            for i in range(N):
                if b[i] == 1:
                    nxt = current - 2**i
                    if nxt not in visited:
                        current = nxt
                        break
        else:
            for i in range(N):
                if b[i] == 0:
                    nxt = current + 2**i
                    if nxt not in visited:
                        current = nxt
                        break
    print("YES")
    print(" ".join(map(str, ans)))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

# judge
res = "NO"
n0 = 0

C = min(A, B)

for n in range(N):
    if abs(A - B) == 2**n:
        if C % 2**(n+1) < 2**n:
            res = "YES"
            n0 = n
            break

res_path = [0]*(2**N)
res_path[1] = 1

for n in range(N):
    if n != n0:
        for i in range(2**n, 2**(n+1)):
            res_path[i] = res_path[2**(n+1) - i - 1] + 2**n
    else:
        i0 = res_path.index(C % 2**n)
        for i in range(i0+1, 2**n):
            res_path[2**n + i] = res_path[i]
        for i in range(i0+1, 2**n):
            res_path[2**n + 2*i0 + 1 - i] = res_path[2**n + i] + 2**n
        for i in range(i0+1):
            res_path[2*i0 + 1 - i] = res_path[i] + 2**n 

            
ind_A = res_path.index(A)
ind_B = res_path.index(B)

print(res)

if res == "YES":
    if abs(ind_A - ind_B) != 1 and  abs(ind_A - ind_B) != (N-1):
        # raise
        print(fugaa)
    if ind_A < ind_B:
        res_path_a = list(reversed(res_path[:(ind_B)])) + list(reversed(res_path[ind_B:]))
    else:
        res_path_a = res_path[ind_A:] + res_path[:ind_A]
        
    # assert
    check = [2**n for n in range(N)]
    for i in range(2**N - 1):
        if abs(res_path_a[i+1] - res_path_a[i]) not in check:
            print(fugaa)
    print(" ".join([str(a) for a in res_path_a]))

Submission status: Wrong Answer

Solution code:
N, A, B = map(int, input().split())
a_str = bin(A)[2:]
b_str = bin(B)[2:]
a_bin = int(a_str)
b_bin = int(b_str)
a = [0] * (N+1)
b = [0] * (N+1)
c = [A] + [0]*(2**N-2) + [B]
# for i in range(len(a_str),2,-1):
# 	a.append(int(a_str[i-1]))
# for i in range(len(b_str),2,-1):
# 	b.append(int(b_str[i-1]))
cnta = 0
cntb = 0
while (a_bin):
	a[cnta] = a_bin%10
	a_bin = a_bin//10
	cnta += 1
while (b_bin):
	b[cntb] = b_bin%10
	b_bin = b_bin//10
	cntb += 1

# print(cnta,cntb)
d = A^B
h = 0
while(d):
	d &= d-1
	h += 1
# print(h)

if (2**N-h)%2 == 0 or h > 2**N-2:
	print('NO')
else:
	print('YES')
	# j = 1
	# C = A
	# for i in range(max(cnta, cntb)):
	# 	if a[i] != b[i]:
	# 		if a[i] == 1:
	# 			C -= 2**i
	# 			c[j] = C
	# 		else:
	# 			C += 2**i
	# 			c[j] = C
	# 		j += 1
	# for i in range(j,2**N-1,2):
	# 	c[j],c[j+1] = c[j-2],c[j-1]
	# for i in range(2**N):
	# 	print(c[i], end=' ')

Submission status: Wrong Answer

Solution code:
def solve(N, A, B):
    f = "{:0%db}" % N
    AStr = f.format(A)
    BStr = f.format(B)
    d1 = sum(abs(int(a) - int(b)) for a, b in zip(AStr, BStr))
    rAStr = "".join(["1" if c == "0" else "0" for c in f.format(A)])
    rBStr = "".join(["1" if c == "0" else "0" for c in f.format(B)])
    d2 = sum(abs(int(a) - int(b)) for a, b in zip(rAStr, rBStr))

    if N == 1:
        ans = [A, B]
        return "\n".join(["YES", " ".join([str(a) for a in ans])])

    if d2 != 1:
        return "NO"
    else:
        same_i = -1
        for i, (ra, rb) in enumerate(zip(rAStr, rBStr)):
            if ra != rb:
                same_i = i

        AStr = list(AStr)
        ans = [int("".join(AStr), 2)]
        for _ in range(2 ** N // 2):
            for i, (a, rb) in enumerate(zip(AStr, rBStr)):
                if a != rb and i != same_i:
                    AStr[i] = rb
                    ans.append(int("".join(AStr), 2))
        AStr[same_i] = rAStr[same_i]
        ans.append(int("".join(AStr), 2))
        for _ in range(2 ** N // 2):
            for i, (a, rb) in enumerate(zip(AStr, BStr)):
                if a != rb:
                    AStr[i] = rb
                    ans.append(int("".join(AStr), 2))
        return "\n".join(["YES", " ".join([str(a) for a in ans])])

if __name__ == "__main__":
    N, A, B = tuple(map(int, input().split(" ")))
    print(solve(N, A, B))

Submission status: Wrong Answer

Solution code:
def listToString(_list, split=""):
    maped_list = map(str, _list)  # mapで要素すべてを文字列に
    mojiretu = split.join(maped_list)
    return mojiretu

def binToInt(_bin):
    out = 0
    for i in range(len(_bin)):
        out += (2**i)*_bin[i]
    return out

n, a, b = [int(i) for i in input().split()]
_a = bin(a)[2:]
_b = bin(b)[2:]
a = [int(_a[i]) for i in reversed(range(len(_a)))]
b = [int(_b[i]) for i in reversed(range(len(_b)))]
# 長さをそろえる
for i in range(len(a) - len(b)):
    b.append(0)
for i in range(len(b) - len(a)):
    a.append(0)
hamDis = 0
for i in range(len(a)):
    if a[i] != b[i]:
        hamDis += 1
# print("hamDis:{}".format(hamDis))
if hamDis <= 2 ** n - 1 and (2 ** n - 1 - hamDis) % 2 == 0:
    print("YES")
    _a = binToInt(a)
    out = [_a]
    for i in range(len(a)):
        if a[i] != b[i]:
            if a[i] == 0:
                _a += 2 ** i
            else:
                _a -= 2 ** i
            out.append(_a)
    margin = 2 ** n - 1 - hamDis
    tmp = 2 ** 30
    for i in range(margin):
        if i % 2 == 0:
            _a += tmp
        else:
            _a -= tmp
        out.append(_a)
    print(listToString(out, " "))
    # for i in out:
        # print(bin(i)[2:])
else:
    print("NO")

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)-1):
    ANS.append(NOW)
    
    WN=N
    for j in range(N):
        if check[NOW ^ (1<<j)]==0:
            if W[NOW ^ (1<<j)]<WN:
                WN=W[NOW ^ (1<<j)]
                NEXT=NOW ^ (1<<j)

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

    NOW=NEXT
    check[NOW]=1

    #print(W,NOW)

ANS.append(B)

print(*ANS)

Submission status: Wrong Answer

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

diff = list([])
for i in range(n, 0, -1):
    x = 1 << (i-1)
    if (a&x) != (b&x):
        diff.append(x)

if ((len(diff) % 2) == 0):
    print('NO')
else:
    print('YES')
    ax = a
    ans=str(ax)
    c = 2**n - 1
    for d in diff:
        if ax & d != 0:
            ax = ax & (~d)
        else:
            ax = ax | d
        ans += str(ax)
        c -= 1
    if c % 2 == 1:
        if (ax & 1) == 1:
            ax = (ax & ~1)
        else:
            ax = (ax | 1)
        ans += str(ax)
        c -= 1

    if (ax & 1) == 1:
        a1 = (ax & ~1)
        a2 = ax
    else:
        a1 = (ax | 1)
        a2 = ax
    aaa = str(a1) + str(a2)
    k=int(c/2)
    print(ans + (aaa*k))

Submission status: Wrong Answer

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

# judge
res = "NO"
n0 = 0

C = min(A, B)

for n in range(N):
    if abs(A - B) == 2**n:
        if C % 2**(n+1) < 2**n:
            res = "YES"
            n0 = n
            break

res_path = [0]*(2**N)
res_path[1] = 1

for n in range(N):
    if n != n0:
        for i in range(2**n, 2**(n+1)):
            res_path[i] = res_path[2**(n+1) - i - 1] + 2**n
    else:
        i0 = res_path.index(C % 2**n)
        for i in range(i0+1, 2**n):
            res_path[2**n + i] = res_path[i]
        for i in range(i0+1, 2**n):
            res_path[2**n + 2*i0 + 1 - i] = res_path[2**n + i] + 2**n
        for i in range(i0+1):
            res_path[2*i0 + 1 - i] = res_path[i] + 2**n 

            
ind_A = res_path.index(A)
ind_B = res_path.index(B)


print(res)
if res == "YES":
    if abs(ind_A - ind_B) != 1 and  abs(ind_A - ind_B) != (N-1):
        # raise
        print(fugaa)
    if ind_A < ind_B:
        res_path_a = list(reversed(res_path[:(ind_B)])) + list(reversed(res_path[ind_B:]))
    else:
        res_path_a = res_path[ind_A:] + res_path[:ind_A]
        
    print(" ".join([str(a) for a in res_path_a]))

Submission status: Wrong Answer

Solution code:
n, x, y = [int(x) for x in input().strip().split(" ")]

bx = bin(x)[2:]
by = bin(y)[2:]
bxy = bin(x ^ y)[2:]
if len(bx) > len(by):
    by = '0' * (len(bx) - len(by)) + by
elif len(by) > len(bx):
    bx = '0' * (len(by) - len(bx)) + bx

c = 1

r=[x]
for i, b in enumerate(bxy):
    if b == '1':
        r.append(int(by[:i+1] + bx[i+1:], 2))
        c += 1

bb = by[-1]
bbb = by[:-1]
while True:
    if c == pow(2,n):
        break
    if bb == "0":
        r.append(int(bbb + "1",2))
        bb = "1"
    else:
        r.append(int(bbb + "0",2))
        bb = "0"
    c += 1

if r[-1] == y:
    print("YES")
    print(" ".join([str(rr) for rr in r]))
else:
    print("No")

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)]

    start, mid, end = [], [], []
    flag1, flag2 = False, False
    for i in code:
        if (not flag1) and (not flag2):
            if (i == a) or (i == b):
                flag1 = True
            else:
                start.append(i)
        elif flag1 and (not flag2):
            if (i == a) or (i == b):
                flag2 = True
            else:
                mid.append(i)
        else:
            end.append(i)

    c = []

    c += [[a] + start + mid + end + [b]]
    c += [[a] + start + mid + end[::-1] + [b]]
    c += [[a] + start + mid[::-1] + end + [b]]
    c += [[a] + start[::-1] + mid + end + [b]]
    c += [[a] + start + mid[::-1] + end[::-1] + [b]]
    c += [[a] + start[::-1] + mid + end[::-1] + [b]]
    c += [[a] + start[::-1] + mid[::-1] + end + [b]]
    c += [[a] + start[::-1] + mid[::-1] + end[::-1] + [b]]

    def is_grey(x):
        for i in range(1, len(x)):
            y = x[i] ^ x[i - 1]
            if y & (y - 1) != 0:
                return False
        return True

    for ci in c:
        if is_grey(ci):
            print('YES')
            print(*ci)
            return

    print('NO')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

    return ret

def solve():
    N,A,B=map(int,input().split())
    change_bit = A ^ B
    if (bin(change_bit).count('1')) % 2 == 0:
        print('NO')
        return
    key_bit = 1<<(change_bit.bit_length()-1)
    change_bit &= ~key_bit
    rest_bit = (1<<N)-1
    rest_bit &= ~key_bit
    print('YES')
    print(' '.join(map(str,walk(A,B,change_bit,rest_bit))))

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)

def dfs():
    used = [False for _ in range(N)]
    ans = []
    def sub_dfs(v, k):
        if v == B and k == N:
            ans.append(v)
            return True
        elif v == B and k < N:
            return False
        used[v] = True
        for u in es[v]:
            if used[u]:
                continue
            if sub_dfs(u, k + 1):
                ans.append(v)
                return True
            else:
                used[u] = False
        return False
    if sub_dfs(A, 1):
        print("YES")
        ret = ""
        for i in ans[::-1]:
            ret += str(i) + " "
        print(ret[:len(ret)-1])
    else:
        print("NO")
dfs()

Submission status: Wrong Answer

Solution code:
N, A, B = map(int, input().split())
a_str = bin(A)[2:]
b_str = bin(B)[2:]
a_bin = int(a_str)
b_bin = int(b_str)
a = [0] * (N+1)
b = [0] * (N+1)
c = [A] + [0]*(2**N-2) + [B]
# for i in range(len(a_str),2,-1):
# 	a.append(int(a_str[i-1]))
# for i in range(len(b_str),2,-1):
# 	b.append(int(b_str[i-1]))
cnta = 0
cntb = 0
while (a_bin):
	a[cnta] = a_bin%10
	a_bin = a_bin//10
	cnta += 1
while (b_bin):
	b[cntb] = b_bin%10
	b_bin = b_bin//10
	cntb += 1

# print(cnta,cntb)
d = A^B
h = 0
while(d):
	d &= d-1
	h += 1
# print(h)

if (2**N-h)%2 == 0 or h > 2**N-2:
	print('NO')
else:
	print('YES')
	j = 1
	C = A
	for i in range(max(cnta, cntb)):
		if a[i] != b[i]:
			if a[i] == 1:
				C -= 2**i
				c[j] = C
			else:
				C += 2**i
				c[j] = C
			j += 1
	for i in range(j,2**N-1,2):
		c[j],c[j+1] = c[j-2],c[j-1]
	for i in range(2**N):
		print(c[i], end=' ')

Submission status: Wrong Answer

Solution code:
n,a,b = map(int,input().split())
zero ="0"*n
a_bit = zero + str(bin(a))[2:]
b_bit = zero + str(bin(b))[2:]

a_bit = a_bit[::-1]
b_bit = b_bit[::-1]
#print(a_bit,b_bit)
k = 2**18
def bit_count(x): #立ってるビットの本数を出力
    n = 0
    while x:
        n += 1
        x &= x-1

    return n
if bit_count(a^b)%2 == 0:
    print('NO')
    exit()

print('YES')
Ans = [a]
t = -1
for i in range(bit_count(a^b)):
    q = True
    while q:
        t += 1
        if int(a_bit[t]) + int(b_bit[t]) == 1:
            if int(a_bit[t]) == 0:
                ans = Ans[i] + 2**t
            else:
                ans = Ans[i] - 2**t
            Ans.append(ans)
            q = False
#print(Ans)

for i in range(bit_count(a^b)+1,2**n):
    if Ans[i-1] == b:
        Ans.append(b+k)
    else:
        Ans.append(b)

for i in range(2**n):
    print(Ans[i],end=' ')

print()