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: Runtime Error

Solution code:
import sys
import time
import random
sys.setrecursionlimit(100000000)
N, A, B = map(int, input().split())
sTime = time.time()

P = [-1] * N
Q = [0] * (2**N)
X = [0] * (2**N)
X[0] = 1
X[1] = 1
Y = [1<<i for i in range(N)]
Z = [0]

def calc(k, l, M):
    aa = 0
    bb = 0
    done = [0] * N
    for i in range(M):
        # print("M, i, k,aa,Y=", M, i, k, aa, Y)
        while k & Y[aa] == 0:
            aa += 1
        while l & Y[bb] == 0:
            bb += 1
        P[aa] = bb
        done[bb] = 1
        aa += 1
        bb += 1
    j = 0
    # print(P)
    # print(done)
    for i in range(N):
        if P[i] >= 0:
            continue
        while done[j] == 1:
            j += 1
        P[i] = j
        j += 1

def change(n):
    ret = 0
    for i in range(N):
        if n & (1<<i):
            ret += 1<<P[i]
    return ret

def dc(n):
    return sum(map(int, list(bin(n)[2:])))


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 == 1:
            X[m] = 1
            Z.append(m)
            return 1
        if X[m] == 0 and m != 1:
            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 (dc(A) + dc(B)) % 2 == 0:
    print("NO")
else:
    r = dfs(0, 0)
    for i in range(2**N):
        if Z[i] == A:
            Z = Z[i:] + Z[:i]
    # print(X, Y, Z)
    if r < 0:
        print("NO")
    else:
        # print("A", Z[:10], Z[-10:])
        mi = 99999
        while mi > 0:
            # for i in range(3, 2**N-1, 2):
            i = random.randrange(max(2**N-1001, 1),2**N-1,2)
            if Z[i-1]^Z[-1] in Y:
                Z = Z[:i] + Z[i:][::-1]
                mi = abs(dc(Z[-1]) - dc(B))
        # print("A", Z[:10], Z[-10:])
        # print("(Z[-1], B, dc(B))", Z[-1], B, dc(B))
        calc(Z[-1], B, dc(B))
        # print("P=", P)
        for i in range(N):
            s = 1<<P[i]
            t = 1<<i
            for j in range(2**N):
                if j & t:
                    Q[j] += s
        Z = [Q[z] for z in Z]
        # print(Z[:10], Z[-10:])
        print("YES")
        print(*Z)

Submission status: Runtime Error

Solution code:
def f(n,lis,s):
    if n==1:
        change(lis[0],s)
    else:
        f(n-1,lis,s)
        change(lis[n-1])
        f(n-1,lis,s)

def g(m,lis):
    if m==1:
        change(lis[1],1)
        change(lis[0],1)
        change(lis[1],1)
    else:
        f(m-1,lis,1)
        change(lis[m],1)
        g(m-2,lis)

def change(n,s):
    foo = ans[-1]^(1<<n)
    ans.append(foo)
    if s==1:
        call()

def call():
    f(neven,liseven,0)

N,A,B =[int(i)for i in input().split()]
st = format(A^B,"b")
st = [i for i in st]
num = st.count("1")
ans = [A]
if num%2==0:
    print("NO")
else:
    print("YES")
    listeven =[]
    listodd =[]
    for i,j in enumerate(st):
        if j=="0":
            listeven.append(i)
        else:
            lisodd.append(i)
    neven = len(liseven)
    nodd = len(lisodd)

    g(nodd,lisodd)

    print(" ".join(ans))

Submission status: Runtime Error

Solution code:
N = int(input())
A= int(input())
B = int(input())
B1 = A^B


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

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


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

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

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



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

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

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

Submission status: Runtime Error

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

twoN = 2**N
visited = 1 << A
cache = {}
def dfs(node, visited, path, depth):
    neighbor_list = [node ^ (1<<mask) for mask in range(N)]
    if depth == twoN-2 and B in neighbor_list:
        print("YES\n" + " ".join([str(i) for i in path + [B]]))
        return True
    elif B in neighbor_list:
        neighbor_list.remove(B)
    for neighbor in neighbor_list:
        if not visited & (1 << neighbor):
            if not cache.get((neighbor, depth), True): continue
            if dfs(neighbor, visited ^ (1 << neighbor), path +
                    [neighbor], depth+1):
                return True
            cache[(neighbor, depth)] = False                                                                   
    return False

if not dfs(A, visited, [A], 0):
    print("NO")

Submission status: Runtime Error

Solution code:
import sys
import time
import random
sys.setrecursionlimit(100000000)
N, A, B = map(int, input().split())
sTime = time.time()

P = [-1] * N
Q = [0] * (2**N)
X = [0] * (2**N)
X[0] = 1
X[1] = 1
Y = [1<<i for i in range(N)]
Z = [0]

def calc(k, l, M):
    aa = 0
    bb = 0
    done = [0] * N
    for i in range(M):
        # print("M, i, k,aa,Y=", M, i, k, aa, Y)
        while k & Y[aa] == 0:
            aa += 1
        while l & Y[bb] == 0:
            bb += 1
        P[aa] = bb
        done[bb] = 1
        aa += 1
        bb += 1
    j = 0
    # print(P)
    # print(done)
    for i in range(N):
        if P[i] >= 0:
            continue
        while done[j] == 1:
            j += 1
        P[i] = j
        j += 1

def change(n):
    ret = 0
    for i in range(N):
        if n & (1<<i):
            ret += 1<<P[i]
    return ret

def dc(n):
    return sum(map(int, list(bin(n)[2:])))


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 == 1:
            X[m] = 1
            Z.append(m)
            return 1
        if X[m] == 0 and m != 1:
            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 (dc(A) + dc(B)) % 2 == 0:
    print("NO")
else:
    r = dfs(0, 0)
    for i in range(N):
        if Z[i] == A:
            Z = Z[i:] + Z[:i]
    # print(X, Y, Z)
    if r < 0:
        print("NO")
    else:
        # print("A", Z[:10], Z[-10:])
        mi = 99999
        while mi > 0:
            # for i in range(3, 2**N-1, 2):
            i = random.randrange(max(2**N-1001, 1),2**N-1,2)
            if Z[i-1]^Z[-1] in Y:
                Z = Z[:i] + Z[i:][::-1]
                mi = abs(dc(Z[-1]) - dc(B))
        # print("A", Z[:10], Z[-10:])
        # print("(Z[-1], B, dc(B))", Z[-1], B, dc(B))
        calc(Z[-1], B, dc(B))
        # print("P=", P)
        for i in range(N):
            s = 1<<P[i]
            t = 1<<i
            for j in range(2**N):
                if j & t:
                    Q[j] += s
        Z = [Q[z] for z in Z]
        # print(Z[:10], Z[-10:])
        print("YES")
        print(*Z)

Submission status: Runtime Error

Solution code:
import sys
import time
import random
sys.setrecursionlimit(100000000)
N, A, B = map(int, input().split())
sTime = time.time()

P = [-1] * N
Q = [0] * (2**N)
X = [0] * (2**N)
X[0] = 1
X[1] = 1
Y = [1<<i for i in range(N)]
Z = [0]

def calc(k, l, M):
    aa = 0
    bb = 0
    done = [0] * N
    for i in range(M):
        # print("M, i, k,aa,Y=", M, i, k, aa, Y)
        while k & Y[aa] == 0:
            aa += 1
        while l & Y[bb] == 0:
            bb += 1
        P[aa] = bb
        done[bb] = 1
        aa += 1
        bb += 1
    j = 0
    # print(P)
    # print(done)
    for i in range(N):
        if P[i] >= 0:
            continue
        while done[j] == 1:
            j += 1
        P[i] = j
        j += 1

def change(n):
    ret = 0
    for i in range(N):
        if n & (1<<i):
            ret += 1<<P[i]
    return ret

def dc(n):
    return sum(map(int, list(bin(n)[2:])))


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 == 1:
            X[m] = 1
            Z.append(m)
            return 1
        if X[m] == 0 and m != 1:
            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 (dc(A) + dc(B)) % 2 == 0:
    print("NO")
else:
    r = dfs(0, 0)
    # print(X, Y, Z)
    if r < 0:
        print("NO")
    else:
        # print("A", Z[:10], Z[-10:])
        mi = 99999
        while mi > 0:
            # for i in range(3, 2**N-1, 2):
            i = random.randrange(max(2**N-1001, 1),2**N-1,2)
            if Z[i-1]^Z[-1] in Y:
                Z = Z[:i] + Z[i:][::-1]
                mi = abs(dc(Z[-1]) - dc(B))
        # print("A", Z[:10], Z[-10:])
        # print("(Z[-1], B, dc(B))", Z[-1], B, dc(B))
        calc(Z[-1], B, dc(B))
        # print("P=", P)
        for i in range(N):
            s = 1<<P[i]
            t = 1<<i
            for j in range(2**N):
                if j & t:
                    Q[j] += s
        Z = [Q[z] for z in Z]
        # print(Z[:10], Z[-10:])
        print("YES")
        print(*Z)

Submission status: Runtime Error

Solution code:
import sys
import time
import random
sys.setrecursionlimit(100000000)
N, A, B = map(int, input().split())
sTime = time.time()

P = [-1] * N
Q = [0] * (2**N)
X = [0] * (2**N)
X[0] = 1
X[1] = 1
Y = [1<<i for i in range(N)]
Z = [0]

def calc(k, l, M):
    aa = 0
    bb = 0
    done = [0] * N
    for i in range(M):
        # print("M, i, k,aa,Y=", M, i, k, aa, Y)
        while k & Y[aa] == 0:
            aa += 1
        while l & Y[bb] == 0:
            bb += 1
        P[aa] = bb
        done[bb] = 1
        aa += 1
        bb += 1
    j = 0
    # print(P)
    # print(done)
    for i in range(N):
        if P[i] >= 0:
            continue
        while done[j] == 1:
            j += 1
        P[i] = j
        j += 1

def change(n):
    ret = 0
    for i in range(N):
        if n & (1<<i):
            ret += 1<<P[i]
    return ret

def dc(n):
    return sum(map(int, list(bin(n)[2:])))


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 == 1:
            X[m] = 1
            Z.append(m)
            return 1
        if X[m] == 0 and m != 1:
            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 (dc(A) + dc(B)) % 2 == 0:
    print("NO")
else:
    r = dfs(0, 0)
    # print(X, Y, Z)
    if r < 0:
        print(1/0)
    else:
        # print("A", Z[:10], Z[-10:])
        mi = 99999
        while mi > 0:
            # for i in range(3, 2**N-1, 2):
            i = random.randrange(1,2**N-1,2)
            if Z[i-1]^Z[-1] in Y:
                Z = Z[:i] + Z[i:][::-1]
                mi = abs(dc(Z[-1]) - dc(B))
        # print("A", Z[:10], Z[-10:])
        # print("(Z[-1], B, dc(B))", Z[-1], B, dc(B))
        calc(Z[-1], B, dc(B))
        # print("P=", P)
        for i in range(N):
            s = 1<<P[i]
            t = 1<<i
            for j in range(2**N):
                if j & t:
                    Q[j] += s
        Z = [Q[z] for z in Z]
        # print(Z[:10], Z[-10:])
        print("YES")
        print(*Z)

Submission status: Runtime Error

Solution code:
def num_of_1bit(n: int)->int:
    s = 0
    while n > 0:
        s += n & 1
        n >>= 1
    return s


def diff_by_1_bit(N: int, A: int, B: int)->list:
    num_A = num_of_1bit(A)
    num_B = num_of_1bit(B)
    if abs(num_A - num_B) & 1 == 1:
        raise [0]
    else:
        return []


if __name__ == "__main__":
    N, A, B = map(int, input().split())
    ans = diff_by_1_bit(N, A, B)
    if len(ans) == 0:
        print('NO')
    else:
        print('YES')
        print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
n, a, b = map(int, input().split())
def bit(x):
    ret = []
    for _ in range(n):
        ret.append(x % 2)
        x //= 2
    return ret
def neighbor(x):
    ret = []
    for i in range(n):
        ret.append(x ^ (1 << i))
    return ret
d = sum(bit(a ^ b))
if d % 2 == 0:
    print('NO')
    exit()
print('YES')
def dfs(x, route):
    if len(route) >= (1 << n) - 2:
        if b in neighbor(x):
            route.append(x)
            route.append(b)
            for r in route:
                print(r, end = ' ')
            print()
            exit()
    else:
        route.append(x)
        for y in neighbor(x):
            if y not in route:
                dfs(y, route)
dfs(a, [])

Submission status: Runtime Error

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

twoN = 2**N
visited = 1 << A
cache = {}
def dfs(node, visited, path, depth):
    mask = 1 
    neighbor_list = [node ^ (1<<mask) for mask in range(N)]
    if depth == twoN-2 and B in neighbor_list:
        print("YES")
        print(" ".join([str(i) for i in path + [B]]))
        return True
    elif B in neighbor_list:
        neighbor_list.remove(B)
    for neighbor in neighbor_list:
        if not visited & (1 << neighbor):
            if not cache.get((neighbor, depth), True): continue
            if dfs(neighbor, visited ^ (1 << neighbor), path +
                    [neighbor], depth+1):
                return True
        cache[(neighbor, depth)] = False
    return False

if not dfs(A, visited, [A], 0):                                                                                
    print("NO")

Submission status: Runtime Error

Solution code:
def num_of_1bit(n: int)->int:
    s = 0
    while n > 0:
        s += n & 1
        n >>= 1
    return s


def diff_by_1_bit(N: int, A: int, B: int)->list:
    num_A = num_of_1bit(A)
    num_B = num_of_1bit(B)
    if abs(num_A - num_B) & 1 == 1:
        raise Exception('')
    else:
        return []


if __name__ == "__main__":
    N, A, B = map(int, input().split())
    ans = diff_by_1_bit(N, A, B)
    if len(ans) == 0:
        print('NO')
    else:
        print('YES')
        print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
def num_of_1bit(n: int)->int:
    s = 0
    while n > 0:
        s += n & 1
        n >>= 1
    return s


def diff_by_1_bit(N: int, A: int, B: int)->list:
    num_A = num_of_1bit(A)
    num_B = num_of_1bit(B)
    if abs(num_A - num_B) == 1:
        raise Exception('')
    else:
        return []


if __name__ == "__main__":
    N, A, B = map(int, input().split())
    ans = diff_by_1_bit(N, A, B)
    if len(ans) == 0:
        print('NO')
    else:
        print('YES')
        print(' '.join(map(str, ans)))

Submission status: Runtime Error

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] * 17
for i in range(1, 17):
    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
    current = A
    idx = 0
    while sum(cnt) > 0:
        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: Runtime Error

Solution code:
N,A,B=map(int,input().split())
Astr=bin(A)[2:]
Bstr=bin(B)[2:]

conb=[1]
for i in range(1,N+1):
    conb.append(int(conb[-1]*(N-i+1)/i))
cango=[]
for i in range(N+1):
    if i==0:
        cango.append(conb[1]%2)
    elif i==N:
        cango.append(conb[N-1]%2)
    else:
        cango.append((conb[i-1]+conb[i+1])%2)

data=[[] for _ in range(N+1)]
for i in range(2**N):
    if i==A or i==B:
        continue
    istr=bin(i)[2:]
    data[istr.count('1')].append(i)
res=''
if cango.count(1)==0:
    res='YES'
elif cango.count(1)==2 and cango[Astr.count('1')]==1 and cango[Bstr.count('1')]==1:
    res='YES'
if res=='YES':
    print('YES')
    Bidx=Bstr.count('1')
    now=Astr.count('1')
    conb[now]-=1
    conb[Bidx]-=1
    ans=str(A)+' '
    while True:
        if conb[now-1]==0:
            break
        if now==0:
            break
        now-=1
        conb[now]-=1
        ans+=str(data[now].pop())+' '
    while True:
        if conb[now-1]==0 and conb[now+1]==0:
            break
        if now==0:
            now+=1
        elif now==N:
            now-=1
        elif conb[now-1]==0:
            now+=1
        elif conb[now+1]==0:
            now-=1
        elif now-1>Bidx and conb[now-1]==1:
            now+=1
        elif now-1<=Bidx and conb[now-1]>0:
            now-=1
        elif now-1>Bidx and conb[now-1]>1:
            now-=1
        conb[now]-=1
        ans+=str(data[now].pop())+' '
    print(ans+str(B))
else:
    print('NO')

Submission status: Runtime Error

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

#C_list = [0]*N
#for n in range(N):
#    if C % 2**(n+1) >= 2**n:
#        C_list[n] = 1
        
# path 0 -> 2**(i)
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)
if abs(ind_A - ind_B) != 1 and  abs(ind_A - ind_B) != (N-1):
    # raise
    print(fugaa)

print(res)
if res == "YES":
    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: Runtime Error

Solution code:
N,A,B=map(int,input().split())
Astr=bin(A)[2:]
Bstr=bin(B)[2:]

conb=[1]
for i in range(1,N+1):
    conb.append(int(conb[-1]*(N-i+1)/i))
cango=[]
for i in range(N+1):
    if i==0:
        cango.append(conb[1]%2)
    elif i==N:
        cango.append(conb[N-1]%2)
    else:
        cango.append((conb[i-1]+conb[i+1])%2)

data=[[] for _ in range(N+1)]
for i in range(2**N):
    if i==A or i==B:
        continue
    istr=bin(i)[2:]
    data[istr.count('1')].append(i)
res=''
if cango.count(1)==0:
    res='YES'
elif cango.count(1)==2 and cango[Astr.count('1')]==1 and cango[Bstr.count('1')]==1:
    res='YES'
if res=='YES':
    print('YES')
    Bidx=Bstr.count('1')
    now=Astr.count('1')
    conb[now]-=1
    conb[Bidx]-=1
    ans=str(A)+' '
    while True:
        if now==0:
            break
        now-=1
        conb[now]-=1
        ans+=str(data[now].pop())+' '
    while True:
        if conb[now-1]==0 and conb[now+1]==0:
            break
        if now==0:
            now+=1
        elif now==N:
            now-=1
        elif conb[now-1]==0:
            now+=1
        elif conb[now+1]==0:
            now-=1
        elif now-1>Bidx and conb[now-1]==1:
            now+=1
        elif now-1<=Bidx and conb[now-1]>0:
            now-=1
        elif now-1>Bidx and conb[now-1]>1:
            now-=1
        conb[now]-=1
        ans+=str(data[now].pop())+' '
    print(ans+str(B))
else:
    print('NO')

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
import numpy as np
input = sys.stdin.readline

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

num_no_use = [True] * 2**N
#num_no_use[B] = False

def make_str(i, n, s, nnu):
    nnu = nnu[:]
#    print(i, n, ":", s)
    nnu[n] = False
#    print(nnu)

    if i == (2**N - 1): # 最後までたどり着けた
        if n == B:
            return s
        else:
            return None

    r = [2**j for j in range(N)]

    for d in r: # 2**d
        if n + d >= 0 and n + d < 2**N and nnu[int(n + d)]:
            result =  make_str(i+1, n+d, str(s) + ' ' + str(n+d), nnu)
            if result is None and n - d >= 0 and n - d < 2**N and nnu[int(n - d)]:
                return make_str(i+1, n-d, str(s) + ' ' + str(n-d), nnu)
            else:
                return result

    return None

result = make_str(0, A, A, num_no_use)

if result is None:
    print("NO")
else:
    print("YES")
    print(result)

Submission status: Runtime Error

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:]
hamDis = 0
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)
for i in range(min(len(a), len(b))):
    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 - len(out)
    tmp = 2 ** 20
    for i in range(margin):
        if i % 2 == 0:
            _a += tmp
        else:
            _a -= tmp
        out.append(_a)
    print(listToString(out, " "))
else:
    print("NO")
    print(1/0)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
import numpy as np
input = sys.stdin.readline

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

num_no_use = [True] * 2**N
#num_no_use[B] = False

def make_str(i, n, s, nnu):
    nnu = nnu[:]
#    print(i, n, ":", s)
    nnu[n] = False
#    print(nnu)

    if i == (2**N - 1): # 最後までたどり着けた
        if n == B:
            return s
        else:
            return None

    r = [2**j for j in range(N)]
    r.extend([-(2**j) for j in range(0, N)])

    for d in r: # 2**d
        if n + d >= 0 and n + d < 2**N and nnu[int(n + d)]:
            result =  make_str(i+1, n+d, str(s) + ' ' + str(n+d), nnu)
            if result is None and n - d >= 0 and n - d < 2**N and nnu[int(n - d)]:
                return make_str(i+1, n-d, str(s) + ' ' + str(n-d), nnu)
            else:
                return result

    return None

result = make_str(0, A, A, num_no_use)

if result is None:
    print("NO")
else:
    print("YES")
    print(result)

Submission status: Runtime Error

Solution code:
N,A,B=map(int,input().split())
Astr=bin(A)[2:]
Bstr=bin(B)[2:]

conb=[1]
for i in range(1,N+1):
    conb.append(int(conb[-1]*(N-i+1)/i))
cango=[]
for i in range(N+1):
    if i==0:
        cango.append(conb[1]%2)
    elif i==N:
        cango.append(conb[N-1]%2)
    else:
        cango.append((conb[i-1]+conb[i+1])%2)

data=[[] for _ in range(N+1)]
for i in range(2**N):
    if i==A or i==B:
        continue
    istr=bin(i)[2:]
    data[istr.count('1')].append(i)
res=''
if cango.count(1)==0:
    res='YES'
elif cango.count(1)==2 and cango[Astr.count('1')]==1 and cango[Bstr.count('1')]==1:
    res='YES'
if res=='YES':
    print('YES')
    Bidx=Bstr.count('1')
    now=Astr.count('1')
    conb[now]-=1
    ans=str(A)+' '
    for i in range(2**N-2):
        #move
        if now==N:
            now-=1
        elif now==0:
            now+=1
        elif conb[now+1]==0:
            now-=1
        elif now<=Bidx and conb[now-1]>=1:
            now-=1
        elif now>Bidx and conb[now-1]>=2:
            now-=1
        elif now<=Bidx and conb[now-1]==0:
            now+=1
        elif now>Bidx and conb[now-1]==1:
            now+=1
        #add
        conb[now]-=1
        ans+=str(data[now].pop())+' '
    print(ans+str(B))
else:
    print('NO')

Submission status: Runtime Error

Solution code:
import math

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

array = ['00', '01', '11', '10']


for j in range(N-2):
    
    length = len(array)
    for i in range(length):
        array.append('1' + array[length-1-i])

    for i in range(len(array)):
        array[i] = array[i].zfill(2+j+1)
flag = False
print(array)
for i in range(len(array)):
    #print(int(array[i], 2))
    if int(array[i], 2)==A:
        
        if int(array[i-1],2)==B:
            print('YES')
            for j in range(len(array)):
                if i+j >= int(math.pow(2, N)):
                    #print(i+j)
                    a = i+j-int(math.pow(2,N))
                else:
                    a = i+j
                if j+1==len(array):
                    print(int(array[a], 2), end='')
                else:
                    print(int(array[a], 2), end=' ')
            flag = True
            break
        if i+1==math.pow(2,N):
            a = 0
        else:
            a = i+1
        if int(array[a],2)==B:
            print('YES')
            for j in range(len(array)):
                a = i-j
                if j+1==len(array):
                    print(int(array[a], 2), end='')
                else:
                    print(int(array[a], 2), end=' ')
            flag = True
            break
if flag==False: 
    print('NO')

return 0

Submission status: Runtime Error

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

twoN = 2**N
visited = 1 << A
def dfs(node, visited, path, depth):
    mask = 1 
    while mask < twoN:
        neighbor = node ^ mask
        if neighbor == B:
            if depth == twoN-2:
                print("YES")
                print(" ".join([str(i) for i in path + [B]]))
                return True
            else:
                mask <<= 1
                continue
    
        if not visited & (1 << neighbor):
            if dfs(neighbor, visited ^ (1 << neighbor), path + [neighbor],
                    depth+1):
                return True
        mask <<= 1
    return False

if not dfs(A, visited, [A], 0): 
    print("NO")

Submission status: Runtime Error

Solution code:
n,a,b = map(int,input().split())
def change2(n,c,list):
    ret1 = False
    ret2 = False
    for i in range(n):
        new_value = (1<<i) ^ c
        if (new_value == b) and (len(list) == 2**n-1):
            list.append(new_value)
            print('YES')
            print(*list)
            ret1 =  True
        else:
            if not new_value in list:
                list.append(new_value)
                ret2 = change2(n,new_value,list)
    return ret1 or ret2

if not change2(n,a,[a]):
    print('NO')

Submission status: Runtime Error

Solution code:
N,A,B=map(int,input().split())
Astr=bin(A)[2:]
Bstr=bin(B)[2:]

conb=[1]
for i in range(1,N+1):
    conb.append(int(conb[-1]*(N-i+1)/i))
cango=[]
for i in range(N+1):
    if i==0:
        cango.append(conb[1]%2)
    elif i==N:
        cango.append(conb[N-1]%2)
    else:
        cango.append((conb[i-1]+conb[i+1])%2)

data=[[] for _ in range(N+1)]
for i in range(2**N):
    if i==A or i==B:
        continue
    istr=bin(i)[2:]
    data[istr.count('1')].append(i)
res=''
if cango.count(1)==0:
    res='YES'
elif cango.count(1)==2 and cango[Astr.count('1')]==1 and cango[Bstr.count('1')]==1:
    res='YES'
if res=='YES':
    print('YES')
    Bidx=Bstr.count('1')
    now=Astr.count('1')
    conb[now]-=1
    ans=str(A)+' '
    for i in range(2**N-2):
        #move
        if now==N:
            now-=1
        elif now==0:
            now+=1
        elif conb[now+1]==0:
            now-=1
        elif now<=Bidx and conb[now-1]>=1:
            now-=1
        elif now>Bidx and conb[now-1]>=2:
            now-=1
        elif now<=Bidx and conb[now-1]==0:
            now+=1
        elif now>Bidx and conb[now-1]==1:
            now+=1
        #add
        ans+=str(data[now].pop())+' '
    print(ans+str(B))
else:
    print('NO')