Problem:


Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.



Sample input:
3
1 2 3


Sample output:
Yes

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())
a = input().split()
a = [int(s) for s in a]

#a_2 = [bin(i) for i in a]
#a_3 = ['{0:031d}'.format(i, 'b') for i in a]
#a_4 = [list(s) for s in a_3] # 各入力、2進数、str、文字ごとにリスト化

test = list(itertools.permutations(a))
#print(test)
flag = 0

for i in range(len(test)):
  if((test[i][-2]^test[i][0]==test[i][-1]) and (test[i][-1]^test[i][1]==test[i][0])):
    for j in range(n-2):
#      print("ここ")
#      print(test[i][j]^test[i][j+2])
#      print(test[i][j+1])
      if test[i][j]^test[i][j+2] != test[i][j+1]:
#        print("これ")
        break
      if j == (n-3):
        flag = 1

if flag == 0:
  print('No')
else:
  print('Yes')

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(200000)
n = int(input())
li = list(map(int,input().split()))
a = li[0]
del li[0]

def f(num1,num2,l):
  t = num1^num2
  if len(l) == 1 and t in l:
    return True
  else:
    if t in l:
      l.remove(t)
      return f(num2,t,l)
    else:
      return False

for i in li:
  if not a^i in li:
    continue
  else:
    if f(i,a,li[:]):
      print("Yes")
      exit()
    else:continue
print("No")

Submission status: Time Limit Exceeded

Solution code:
import itertools
 
n = int(input()) # The number of camel
a = [int(i) for i in input().split()] # Hat's number
 
combs = list(itertools.permutations([i for i in range(1, n)]))
 
check = True
for comb in combs:
    tmp = [0]
    tmp.extend(comb)
    for i in range(len(tmp)):
        idx = i
        if check:
            pre = idx - 1
            if pre < 0:
                pre = n - 1
            
            if a[tmp[idx]] == a[tmp[pre]]^a[tmp[(idx + 1)%n]]:
                check = False
        else:
            break
 
    if not check:
        break
 
if not check:
    print("Yes")
else:
    print("No")

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
a = list(map(int ,input().split()))

def get_ans(a, N):
    for l in itertools.permutations(a):
        ans = 'Yes'
        a_tmp = list(l)
        if a_tmp[0] != a[0]:
            continue
        a_tmp.extend(a_tmp[:2])

        for x in range(N):
            #if int(bin(a_tmp[x]^a_tmp[x+2]), 0) != a_tmp[x+1]:
            if bin(a_tmp[x]^a_tmp[x+2]) != bin(a_tmp[x+1]):
                ans = 'No'
                break
        if ans == 'Yes':
            print(ans)
            return True
    print('No')
    return False

ret = get_ans(a, N)

Submission status: Time Limit Exceeded

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

s = []

for i in range(n):
    for j in range(n):
        if not i == j:
            s.append(a[i] ^ a[j])

#print(a)
#print(s)

ans_flag = True
for i in range(n):
    sub_flag = False
    for j in range(len(s)):
        if a[i] == s[j]:
            s[j] = -1
            sub_flag = True
            break
    if sub_flag == False:
        ans_flag = False
        break

#print(a)
#print(s)

if ans_flag:
    print('Yes')
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
import copy

N = input()
A = input().split()
n = int(N)
Int_A = [int(a) for a in A]

result = [Int_A[0]]
Int_A.remove(Int_A[0])


def check(a, other):
    if len(other) == 1:
        if result[0] == other[0] ^ a:
            print("Yes")
            exit()
        return []

    res = []
    co_other = copy.deepcopy(other)
    for o in other:
        c = a ^ o
        co_other.remove(o)
        if c in co_other:
            res.append(o)
        co_other.append(o)

    return res


def deep_check(depth):

    resp = check(result[depth - 1], Int_A)

    if len(resp) == 0:
        return

    for k in resp:
        result.append(k)
        Int_A.remove(k)
        depth += 1
        deep_check(depth)
        result.remove(k)
        Int_A.append(k)
    depth -= 1
    return


deep_check(1)
print('No')

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
a = list(map(int, input().split()))

zikken1 = list(itertools.combinations(a,2))
counter = 0
for z1 in zikken1:
    zz = []
    zz.extend(range(N))
    zz[0] = z1[0]
    zz[1] = z1[1]
    for i in range(N-2):
        zz[i+2] = zz[i]^zz[i+1]
    if sorted(zz) == sorted(a):
        counter = 1

if counter == 1:
    print("Yes")
else:
    print("No")

Submission status: Time Limit Exceeded

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

table = {}
for i in range(len(A)):
    a = A[i]
    B = A.copy()
    B.pop(i)
    for j in range(len(B)):
        for k in range(1, len(B)):
            if A[i] == B[j]^B[k]:
                if A[i] not in table.keys():
                    table[a] = set()
                b1 = min([B[j], B[k]])
                b2 = max([B[j], B[k]])
                table[a].add( (b1, b2) )

def locate(i, a, current, rest):
    global N, table
    if a not in table.keys():
        return False
    else:
        #print("###############")
        #print("i={}".format(i))
        #print(current)
        #print("###############\n")
        L = (i - 1 + N) % N
        R = (i + 1 + N) % N
        if i==N-2 and current[L] != -1 and current[R] != -1:
            if a == current[L]^current[R]:
                return True
            else:
                return False
        else:
            current_ = current.copy()
            current_[i] = a
            for j in range(len(rest)):
                rest_ = rest.copy()
                next_a = rest_.pop(j)
                need_search = False
                for t1,t2 in table[a]:
                    if i < N-2:
                        if t1 == next_a:
                            if t2 in rest_:
                                need_search = True
                                break
                        if t2 == next_a:
                            if t1 in rest_:
                                need_search = True
                                break
                    else:
                        if t1 == next_a or t2 == next_a:
                            need_search = True
                            break
                if need_search:
                    if locate(i+1, next_a, current_, rest_) == True:
                        return True


for i in range(len(A)):
    current = [-1] * N
    rest = A.copy()
    a = rest.pop(i)

    if a in table:
        for t1, t2 in table[a]:
            if t1 in rest and t2 in rest:
                rest_ = rest.copy()
                rest_.pop(rest_.index(t1))
                rest_.pop(rest_.index(t2))
                if N==3:
                    if a == t1^t2:
                        print("Yes")
                        exit()
                elif N>3:
                    b = rest_.pop()
                    current_1 = current.copy()
                    current_1[0]   = a
                    current_1[1]   = t1
                    current_1[N-1] = t2
                    ret = locate(3, b, current_1, rest_)
                    if ret == True:
                        print("Yes")
                        exit()

                    current_2 = current.copy()
                    current_2[0]   = a
                    current_2[1]   = t2
                    current_2[N-1] = t1
                    ret = locate(3, b, current_2, rest_)
                    if ret == True:
                        print("Yes")
                        exit()

                
print("No")

Submission status: Time Limit Exceeded

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

flag = False
import itertools
init = A[0]
A.remove(A[0])

def calc(x_old, x_now, y_old, y_now, A_now):
    for x_next, y_next in itertools.permutations(A_now,2):
        if x_now==x_old^x_next and y_now==y_old^y_next:
            A_next = A_now.copy()
            A_next.remove(x_next)
            A_next.remove(y_next)
            if len(A_next)<=2:
                return x_now, x_next, y_now, y_next, A_next
            else:
                calc(x_now, x_next, y_now, y_next, A_next)
    return 0, 0, 0, 0, []

for x0, y0 in itertools.combinations(A,2):
    if flag:
        break
    B = A.copy()
    if init==x0^y0:
        B.remove(x0)
        B.remove(y0)
        if len(B)==0:
            flag = True
            break
        elif len(B)==1:
            if x0==init^B[0] and B[0]==x0^y0 and y0==init^B[0]:
                flag = True
                break
        elif len(B)==2:
            for x1, y1 in itertools.permutations(B,2):
                if x0==init^x1 and y0==init^y1:
                    if x1==x0^y1 and y1==y0^x1:
                        flag = True
                        break
                        
        elif len(B)%2==1:
            x0, x1, y0, y1, C = calc(init, x0, init, y0, B)
            if x0==0 and x1==0 and y0==0 and y1==0 and C==[]:
                break
            elif x1==x0^C[0] and y1==C[0]^y0:
                if C[0]==x1^y1:
                    flag = True
                    break         
        elif len(B)%2==0:
            x0, x1, y0, y1, C = calc(init, x0, init, y0, B)
            if x0==0 and x1==0 and y0==0 and y1==0 and C==[]:
                break
            else:
                for x2, y2 in itertools.permutations(C,2):
                    if x1==x0^x2 and y1==y0^y2:
                        if x2==x1^y2 and y2==y1^x2:
                            flag = True
                            break

if flag:
    print("Yes")
else:
    print("No")

Submission status: Time Limit Exceeded

Solution code:
import copy

N = input()
A = input().split()
n = int(N)
Int_A = [int(a) for a in A]


result = [Int_A[0]]
Int_A.remove(Int_A[0])

i = 1


def check(a, other):
    if len(other) == 1:
        if result[0] == other[0] ^ a:
            print("Yes")
            exit()
        return []

    res = []
    co_other = copy.deepcopy(other)
    for o in other:
        c = a ^ o
        co_other.remove(o)
        if c in co_other:
            res.append(o)
        co_other.append(o)

    return res

Int_C = copy.deepcopy(Int_A)

def deep_check(depth):


    resp = check(result[depth - 1], Int_C)

    if len(resp) == 0:
        return

    for k in resp:
        result.append(k)
        Int_C.remove(k)
        depth += 1
        deep_check(depth)
        result.remove(k)
        Int_C.append(k)
    depth -= 1
    return

deep_check(1)
print('No')

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
a = list(map(int, input().split()))

ab = list(itertools.permutations(a[1:]))
abc = [[0]*(n+1) for _ in range(len(ab))]

ans = 'No'

for i in range(len(ab)):
  abc[i] = list(ab[i])
  abc[i].append(a[0])
  abc[i].append(a[1])

  for j in range(1,n):
    if bin(abc[i][j-1]^abc[i][j+1]) != bin(abc[i][j]):
      break
    if j == n-1:
      ans = 'Yes'

    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
a = list(map(int, input().split()))

zikken1 = list(itertools.combinations(a,2))
counter = 0
for z1 in zikken1:
    z1 = list(z1)
    for i in range(N-2):
        z1.append(z1[i]^z1[i+1])
    if sorted(z1) == sorted(a):
        counter = 1

if counter == 1:
    print("Yes")
else:
    print("No")

Submission status: Time Limit Exceeded

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

hats = A[:]

from collections import defaultdict
d = defaultdict(lambda: {})

for i, h1 in enumerate(hats):
    hats2 = hats[i+1:]
    for j, h2 in enumerate(hats2):
        h3 = h1^h2
        d[h1][h3] = h2
        d[h3][h1] = h2

#print(d)
        
def dfs(top, now1, now2, count):
    if now2 == top:
        #print(count)
        if count == N:
            return True
        else:
            return False

    if d[now1][now2] in d[now2]:
        return False
    now1, now2 = now2, now2 ^ d[now1][now2]

    return dfs(top, now1, now2, count+1)
    
ans = False
h1 = hats[0]
for h2 in d[h1]:
    if dfs(h1, h1, h2, 2):
        ans = True
        break
        
if ans:
    print('Yes')
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
import itertools
 
boushi_num = int(input())
boushi_No = list(map(int,input().split()))
flag = 0
 
 
for v in itertools.permutations(boushi_No, boushi_num):
  for i in range(0,len(v)):
    i = i
    i_a = i+2
    i_b = i+1
    if i_a >boushi_num -1:
      i_a = i_a - boushi_num
    if i_b >boushi_num -1:
      i_b = i_b - boushi_num
            
    if v[i]^v[i_a] != v[i_b]:
      break
    else:
      None
            
    if i == boushi_num - 1:
      flag = 1
                
  if flag == 1:
    print('Yes')
    break
 
if flag == 0:
  print('No')

Submission status: Time Limit Exceeded

Solution code:
import collections as col
N=int(input())
A=[ai for ai in map(int,input().split())]
CA=col.Counter(A)
CA_KEYS=sorted(CA.keys())
#print(CA_KEYS)
for Ai in CA_KEYS:
    CA_Tmp={ai:0 for ai in CA_KEYS}
#    print()
    crnt=A[0]
    next=Ai
    CA_Tmp[crnt]=1
#    print(crnt, end=" ")
    if CA_Tmp[next] > CA[next]:
        continue
    Ans=True
    for j in range(1, N):
#        print(next,end=" ")
        if next in CA:
            CA_Tmp[next]+=1
            if CA_Tmp[next] > CA[next]:
                Ans=False
                break
            crnt,next=next,crnt^next
        else:
            Ans=False
            break
    if Ans==True:
        if next!=A[0]:
#            print("No!",next,"!=",A[0])
            Ans=False
        else:
            break


print("Yes" if Ans==True else "No")

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
r = list(map(int,input().rstrip().split(' ')))
l = list(itertools.combinations(r,2))
f = 0
for u,v in l:
    if u ^ v in r:
       f += 1
if f >= N:
    print("Yes") 
else:
    print("No")

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import itertools
N = int(input())
a = np.array(list(map(int, input().split())))

zikken1 = list(itertools.combinations(a,2))
counter = 0
for z1 in zikken1:
    z1 = list(z1)
    for i in range(N-2):
        z1.append(z1[i]^z1[i+1])
    if sorted(z1) == np.sort(a).tolist():
        counter = 1

if counter == 1:
    print("Yes")
else:
    print("No")

Submission status: Time Limit Exceeded

Solution code:
import itertools
 
boushi_num = int(input())
boushi_No = list(map(int,input().split()))
flag = 0

jyun = list(itertools.permutations(boushi_No, boushi_num))
jyun_num = len(jyun[0])

 
for v in jyun:
  for i in range(0,jyun_num):
    i = i
    i_a = i+2
    i_b = i+1
    if i_a >boushi_num -1:
      i_a = i_a - boushi_num
    if i_b >boushi_num -1:
      i_b = i_b - boushi_num
            
    if v[i]^v[i_a] != v[i_b]:
      break
    else:
      None
            
    if i == boushi_num - 1:
      flag = 1
                
  if flag == 1:
    print('Yes')
    break
 
if flag == 0:
  print('No')

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
a = list(map(int ,input().split()))

def get_ans(a, N):
    for l in itertools.permutations(a):
        ans = 'Yes'
        a_tmp = list(l)
        if a_tmp[0] != a[0]:
            continue
        a_tmp.extend(a_tmp[:2])

        for x in range(N):
            if int(bin(a_tmp[x]^a_tmp[x+2]), 0) != a_tmp[x+1]:
                ans = 'No'
                break
        if ans == 'Yes':
            print(ans)
            return True
    print('No')
    return False

ret = get_ans(a, N)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import sys

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

for i in itertools.permutations(a[1:], len(a)-1):
    flag = True
    i = list(i)
    i.append(a[0])
    i.append(i[0])
    i.insert(0, a[0])
    for j in range(0, len(i)-2):
        if i[j] ^ i[j+2] != i[j+1]:
            flag = False
    if flag:
        print("Yes")
        sys.exit()

print("No")

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations
N = int(input())
A = list(map(int, input().split()))
per_A = list(permutations(A, len(A)))
def f(A):
    no = 0
    new_A =[A[-1]] + A + [A[0]]
    for i in range(1,N+1):
        a,b,c = new_A[i-1],new_A[i],new_A[i+1]
        a_bin = format(a,"b")
        c_bin = format(c,"b")
        ac = str(int(a_bin) + int(c_bin))
        x = "0b"
        for i in range(len(ac)):
            if int(ac[i]) % 2 == 1:
                x += "1"
            else:
                x += "0"
        if int(x,0) != b:
            no = 1
            break
    return no
No = 0
for a in per_A:
    No += f(list(a))
if No == 0:
    print("Yes")
else:
    print("No")

Submission status: Time Limit Exceeded

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

hats = A[:]

from collections import defaultdict
d = defaultdict(lambda: {})

for i, h1 in enumerate(hats):
    hats2 = hats[i+1:]
    for j, h2 in enumerate(hats2):
        h3 = h1^h2
        d[h1][h3] = h2
        d[h3][h1] = h2

#print(d)
        
def dfs(top, now1, now2, count):
    if now2 == top:
        #print(count)
        if count == N:
            return True
        else:
            return False

    if d[d[now1][now2]] == {}:
        return False
    
    now1, now2 = now2, now2 ^ d[now1][now2]

    return dfs(top, now1, now2, count+1)
    
ans = False
h1 = hats[0]
for h2 in d[h1]:
    if dfs(h1, h1, h2, 2):
        ans = True
        break
        
if ans:
    print('Yes')
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

import itertools

N = int(input())

a = np.array(list(map(int, input().split())))

zikken1 = list(itertools.combinations(a,2))
for i3, aa in enumerate(zikken1):
    zikken1[i3] = list(aa)
counter = 0
for z1 in zikken1:
    for i in range(N-2):
        z1.append(z1[i]^z1[i+1])
    if sorted(z1) == np.sort(a).tolist():
        counter = 1
if counter == 1:
    print("Yes")
else:
    print("No")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
xor = []
for i in range(len(a)):
	for k in range(i,len(a)):
		xor.append(int(bin(a[i]^a[k]),0))
xor = list(set(xor))
for i in range(len(a)):
	if i not in xor:
		print("No")
		exit()
print("Yes")

Submission status: Time Limit Exceeded

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

if n==3:
  print('Yes') if a[2]==(a[0]^a[1]) else print('No')
  exit()

if n==4:
  print('No')
  exit()
  
def j(n,a):
#  print(a)
  for i in range(n-1):
    if a[i]!=a[i-1]^a[i+1]:
#      print(a[i],a[i-1],a[i+1])
      return False
  if a[n-1]!=a[n-2]^a[0]:
#    print(a[n-1],a[n-2],a[0])
    return False
  return True

def mp(n,m=0):
  if n==m:
    _a=[a[_-1] for _ in pe]
    r=j(n,_a)
    if r: 
      print('Yes')
      exit()
    
  else:
    for x in range(1,n+1):
      if x in pe: continue
      pe.append(x)
      mp(n,m+1)
      pe.pop()

pe=[]
mp(n)
print('No')

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
inp = list(map(int, input().split()))


clist = list(itertools.permutations(inp))



lenC = len(clist)

Noflag = 0
for i in range(lenC):
  Noflag=0
  for j in range(n):
    if j == 0:
      if clist[i][n-1]^clist[i][j+1] != clist[i][j]:
        Noflag=1
        break
    elif j==n-1:
      if clist[i][j-1]^clist[i][0] != clist[i][j]:
        Noflag=1
        break
    else:
      if clist[i][j-1]^clist[i][j+1] != clist[i][j]:
        Noflag=1
        break
  if Noflag==0:
    break

if Noflag ==0:
  print("Yes")
else:
  print("No")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

a = list(map(int, input().split()))

c = [a[0]]

a.remove(c[0])

result = 'No'
for i in a:
    a_list = a.copy()
    a_list.remove(i)
    d = c[0]
    f = i
    while True:
        g = d ^ f
        if g in a_list:
            a_list.remove(g)
            d = f
            f = g
        else:
            break
            
        if len(a_list) == 0:
            if d ^ f == c[0]:
                result = 'Yes'
            break
    if result == 'Yes':
        break
print(result)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations
n=int(input())
N=list(map(int,input().split()))
ans=[]
for i in list(permutations(N)):
    flag=1
    i=list(i)
    i.insert(0,i[-1])
    i.append(i[1])
    for j in range(1,n):
        if i[j]!=i[j-1]^i[j+1]:
            flag=0
            break
    if flag==1:break

if flag:print("Yes")
else:print("No")

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
a = list(map(int ,input().split()))

def get_ans(a, N):
    ans = 'Yes'
    
    a.extend(a[:2])
    for x in range(N):
        if int(bin(a[x]^a[x+2]), 0) != a[x+1]:
            ans = 'No'
            break
    if ans == 'Yes':
        print(ans)
        return True   

    for l in itertools.permutations(a):
        a_tmp = list(l)
        if a_tmp[0] != a[0]:
            continue
        a_tmp.extend(a_tmp[:2])

        for x in range(N):
            if int(bin(a_tmp[x]^a_tmp[x+2]), 0) != a_tmp[x+1]:
                ans = 'No'
                break
        if ans == 'Yes':
            print(ans)
            return True
    print(ans)
    return False

ret = get_ans(a, N)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[ai for ai in map(int,input().split())]
CA={}
for a in A:
    if a in CA:
        CA[a]+=1
    else:
        CA[a]=1
CA_KEYS=sorted(CA.keys())

for Ai in CA_KEYS:
    CA_Tmp={ai:0 for ai in CA_KEYS}
    crnt=A[0]
    next=Ai
    CA_Tmp[crnt]=1
    if CA_Tmp[next] > CA[next]:
        continue
    Ans=True
    for j in range(1, N):
        if next in CA:
            CA_Tmp[next]+=1
            if CA_Tmp[next] > CA[next]:
                Ans=False
                break
            crnt,next=next,crnt^next
        else:
            Ans=False
            break
    if Ans==True:
        if next!=A[0]:
            Ans=False
        else:
            break


print("Yes" if Ans==True else "No")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

a = list(map(int, input().split()))

c = [a[0]]

a.remove(c[0])

result = 'No'
for i in a:
    a_list = a.copy()
    a_list.remove(i)
    d = c[0]
    f = i
    while True:
        g = d ^ f
        if g in a_list:
            a_list.remove(g)
            d = f
            f = g
        else:
            break
            
        if len(a_list) == 0:
            if d ^ f == c[0]:
                result = 'Yes'
            break

print(result)

Submission status: Time Limit Exceeded

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

hats = A[:]

from collections import defaultdict
d = defaultdict(lambda: {})

for i, h1 in enumerate(hats):
    hats2 = hats[i+1:]
    for j, h2 in enumerate(hats2):
        h3 = h1^h2
        d[h1][h3] = h2
        d[h3][h1] = h2

#print(d)
        
def dfs(top, now1, now2, count):
    if now2 == top:
        #print(count)
        if count == N:
            return True
        else:
            return False

    now1, now2 = now2, now2 ^ d[now1][now2]

    return dfs(top, now1, now2, count+1)
    
ans = False
h1 = hats[0]
for h2 in d[h1]:
    if dfs(h1, h1, h2, 2):
        ans = True
        
if ans:
    print('Yes')
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input()) 
i = list(map(int, input().split()))
flag = 0
for v in itertools.permutations(i):
  vl = list(v)
  vl.append(vl[0])
  vl.insert(0, vl[-2])
  cnt = 0
  for j in range(len(vl)-2):
    if vl[j] ^ vl[j+2] == vl[j+1]:
      cnt += 1
      continue
    else:
      break
  if cnt == (len(vl) -2):
    print('Yes')
    flag = 1
    break
if flag == 0: print('No')

Submission status: Time Limit Exceeded

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

f = []

for i in range(1,n):
    for j in range(1,n):
        if not i == j:
            if a[0] ^ a[j] == a[i]:
                b = [-1] * n
                b[0] = a[0]
                b[1] = a[i]
                b[2] = a[j]
                f.append(b)

for i in range(3,n):
    for j in range(len(f)):
        s = set(a)
        for k in range(i):
            s.discard(f[j][k])
        s = list(s)

        count = 0
        for k in range(n-i):
            if f[j][i-2] ^ s[k] == f[j][i-1]:
                if count <= 0:
                    f[j][i] = s[k]
                    count += 1
                else:
                    b = f[j]
                    b[i] = s[k]
                    f.append(b)

ans_flag = False
for i in range(len(f)):
    if f[i][n-2] ^ f[i][0] == f[i][n-1]:
        ans_flag = True
        break

if ans_flag:
    print('Yes')
else:
    print('No')

Submission status: Time Limit Exceeded

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

table = {}
for i in range(len(A)):
    a = A[i]
    B = A.copy()
    B.pop(i)
    for j in range(len(B)):
        for k in range(1, len(B)):
            if A[i] == B[j]^B[k]:
                if A[i] not in table.keys():
                    table[a] = set()
                b1 = min([B[j], B[k]])
                b2 = max([B[j], B[k]])
                table[a].add( (b1, b2) )

def locate(i, a, current, rest):
    global N, table
    if a not in table.keys():
        return False
    else:
        #print("###############")
        #print("i={}".format(i))
        #print(current)
        #print("###############\n")
        L = (i - 1 + N) % N
        R = (i + 1 + N) % N
        if i==N-1 and current[L] != -1 and current[R] != -1:
            if a == current[L]^current[R]:
                return True
            else:
                return False
        else:
            current_ = current.copy()
            current_[i] = a
            for j in range(len(rest)):
                rest_ = rest.copy()
                next_a = rest_.pop(j)
                need_search = False
                for t1,t2 in table[a]:
                    if i < N-2:
                        if t1 == next_a:
                            if t2 in rest_:
                                need_search = True
                                break
                        if t2 == next_a:
                            if t1 in rest_:
                                need_search = True
                                break
                    else:
                        if t1 == next_a or t2 == next_a:
                            need_search = True
                            break
                if need_search:
                    if locate(i+1, next_a, current_, rest_) == True:
                        return True


for i in range(len(A)):
    current = [-1] * N
    rest = A.copy()
    a = rest.pop(i)

    ret = locate(0, a, current, rest)
    if ret == True:
        print("Yes")
        exit()

print("No")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
xor = []
for i in range(len(a)):
	for k in range(i,len(a)):
		xor.append(int(bin(a[i]^a[k]),0))
for i in a:
	if i not in xor:
		print("No")
		exit()
print("Yes")

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input()) # The number of camel
a = [int(i) for i in input().split()] # Hat's number

combs = list(itertools.permutations([i for i in range(1, n)]))

check = True
for comb in combs:
    tmp = [0]
    tmp.extend(comb)
    for i in range(len(tmp)):
        idx = i
        if check:
            pre = idx - 1
            if pre < 0:
                pre = n - 1
            
            if a[tmp[idx]] != a[tmp[pre]]^a[tmp[(idx + 1)%n]]:
                check = False
        else:
            break

    if not check:
        break

if check:
    print("Yes")
else:
    print("No")

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools

input = sys.stdin.readline

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

one_value = a.pop()
# print(one_value)

permutation_list = list(itertools.permutations(a))
# print(permutation_list)


new_permutation_list=[]

for value in permutation_list:
    tmp=list(value)
    tmp.insert(0,one_value)
    new_permutation_list.append(tmp)

# print(new_permutation_list)

# for i in range(len(new_permutation_list)):
#     for j in range(len(new_permutation_list[0])):
#         new_permutation_list[i][j]=bin(new_permutation_list[i][j])

flag_global=False
for i in range(len(new_permutation_list)):
    flag_local=True
    for j in range(len(new_permutation_list[0])):
        if j == len(new_permutation_list[0])-1:
            if new_permutation_list[i][j - 1] ^ new_permutation_list[i][0] != new_permutation_list[i][j]:
                flag_local = False
                break
            continue

        if new_permutation_list[i][j-1] ^ new_permutation_list[i][j+1] != new_permutation_list[i][j]:
            flag_local=False
            break

    if flag_local == True:
        flag_global=True
        break

if flag_global:
    print("Yes")
else:
    print("No")

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
a = list(map(int ,input().split()))

def get_ans(a, N):
    ans = 'Yes'
    for l in itertools.permutations(a):
        a_tmp = list(l)
        if a_tmp[0] != a[0]:
            continue
        a_tmp.extend(a_tmp[:2])

        for x in range(N):
            if int(bin(a_tmp[x]^a_tmp[x+2]), 0) != a_tmp[x+1]:
                ans = 'No'
                break
        if ans == 'Yes':
            print(ans)
            return True
    print(ans)
    return False

ret = get_ans(a, N)