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
import copy

n = input("")
n = int(n)
num = input("").split(" ")
for i in range(len(num)):
    num[i] = int(num[i])

ans = "No"
all = 0
for i in range(n):
    stop = 0
    num2 = copy.copy(num)
    num2.remove(num2[i])
    com_list = list(itertools.combinations(num2, 2))
    for j in range(len(com_list)):
        list(com_list[j])
        a = com_list[j][0]
        b = com_list[j][1]
        if int(bin(a^b), 2) == num[i]:
            all += 1
            break
        else:
            stop += 1
    if stop == len(com_list):
        break
if all == n:
    ans = "Yes"

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def doMaths(one, two):
    i = max(len(one), len(two))
    ans = ""
    diffone = i - len(one)
    difftwo = i - len(two)
    nonzero = False
    for i2 in range(0, i):
        if len(one) < i - i2:
            ans += two[i2]
            nonzero = True
            continue
        if len(two) < i - i2:
            ans += one[i2]
            nonzero = True
            continue
        maths = (int(one[i2 - diffone]) + int(two[i2 - difftwo]))%2
        if not nonzero:
            if not maths == 0:
                ans += str(maths)
                nonzero = True
        else:
            ans += str(maths)
    return ans

n = int(input())
hats = input()
hatnums = hats.split(" ")
hatnums2 = []
for hat in hatnums:
    hatnums2.append(bin(int(hat))[2:])
for hatnum in hatnums2:
    for hatnum2 in hatnums2:
        if hatnum2 != hatnum:
            if not doMaths(hatnum, hatnum2) in hatnums2:
                print('No')
                exit()
print('Yes')

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
a = list(int(i) for i in input().split())

for k in range(3, N+1):
    for p in itertools.permutations(a, k):
        cnt=0
        for i in range(k):
            if i == 0:
                if p[k - 1] ^ p[i + 1] != p[i]:
                    break
            elif i == k - 1:
                if p[i - 1] ^ p[0] != p[i]:
                    if len(p) -1 == cnt:
                        print("No")
                        exit()
                    break
                else:
                    a = (set(a) - set(p))
                    a.add(p[i - 1])
                    a.add(p[0])
                    if k == N:
                        print("Yes")
                        exit()
            else:
                if p[i - 1] ^ p[i + 1] != p[i]:
                    break
        cnt =cnt+1
print("No")

Submission status: Time Limit Exceeded

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

import itertools

def xorcircle(s,m):
    for t in range(m):
        if s[t-1] ^ s[t+1] != s[t]:
            return False
        else:
            return True
        
l = list(itertools.permutations(a))
m = len(l)

for i in range(m):
    if xorcircle(l[i],n) == True:
        print('Yes')
        break
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
import itertools
import copy

n = input("")
n = int(n)
num = input("").split(" ")
for i in range(len(num)):
    num[i] = int(num[i])

ans = "No"
all = 0
stop = 0
for i in range(n):
    num2 = copy.copy(num)
    num2.remove(num2[i])
    com_list = list(itertools.combinations(num2, 2))
    for j in range(len(com_list)):
        list(com_list[j])
        a = com_list[j][0]
        b = com_list[j][1]
        if int(bin(a^b), 2) == num[i]:
            all += 1
            break
        else:
            stop += 1
    if stop == len(com_list):
        break
if all == n:
    ans = "Yes"

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N = int(input())
A = deque(map(int, input().split()))
first = A.popleft()


def round(a):
        B = list(A)
        eval = 0
        pre = first
        post = a
        B.remove(a)
        while len(B) != 0:
                c = pre ^ post
                try:
                        B.remove(c)
                        pre = post
                        post = c
                except:
                        return 0
                        eval = 1
                        break
        
        if eval == 0 and post ^ first == a:
                return 1
        elif eval == 0:
                return 0

ans = 0

for i in range(0, N - 1):
        ans = round(A[i])
        if ans == 1:
                print('Yes')
                break
                

if ans == 0:
        print('No')

Submission status: Time Limit Exceeded

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


find = False
# 2こめ
for i, x in enumerate(li[1:]):
    result = [li[0]]
    
    result.append(x)
    not_used = li[1:]
    not_used.pop(i)
    
    # 3こめ~
    for j in range(n - 2):
#         print(result)
        nex = result[-1]^result[-2]
#         print(nex, not_used)
        if nex in not_used:
            idx = not_used.index(nex)
            result.append(nex)
            not_used.pop(idx)
        else:
            break
    if len(result) != n:
        continue
    else:
        print("Yes")
        find = True
        break

if not find:
    print("No")

Submission status: Time Limit Exceeded

Solution code:
import sys

N = int(input())
S = sys.stdin.readline()
a = list(map(int, S.split()))

afirst = a[0]

for i in range(N - 1):
    acopy = list(a)
    acopy.pop(0)
    anext = acopy.pop(i)
    while len(acopy) > 0:
        anext ^= afirst
        afirst ^= anext
        if anext in acopy:
            acopy.remove(anext)
        else:
            break
    if len(acopy) == 0:
        anext ^= afirst
        if anext == a[0]:
            print("Yes")
            exit()

print("No")

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

combs = list(itertools.permutations([i for i in range(n)]))
combs = [comb for comb in combs if comb[0] == 0]

check = True
for comb in combs:
    for i in range(n):
        if check:
            idx = i
            pre = i - 1
            if pre < 0:
                pre = n - 1
            
            nex = idx + 1
            if n == nex:
                nex = 0

            if a[comb[idx]] != a[comb[pre]]^a[comb[nex]]:
                check = False
        else:
            break
    else:
        continue
    break

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
import copy

n = input("")
n = int(n)
num = input("").split(" ")
for i in range(len(num)):
    num[i] = int(num[i])

ans = "No"
all = 0
for i in range(n):
    num2 = copy.copy(num)
    num2.remove(num2[i])
    com_list = list(itertools.combinations(num2, 2))
    for j in range(len(com_list)):
        list(com_list[j])
        a = com_list[j][0]
        b = com_list[j][1]
        if int(bin(a^b), 2) == num[i]:
            all += 1
            break
if all == n:
    ans = "Yes"

print(ans)

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 list(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'
                print(ans)
                return 'False'
    print(ans)
    return True

ret = get_ans(a, N)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
a = list(int(i) for i in input().split())

for k in range(3, N+1):
    for p in itertools.permutations(a, k):
        for i in range(k):
            if i == 0:
                if p[k - 1] ^ p[i + 1] != p[i]:
                    break
            elif i == k - 1:
                if p[i - 1] ^ p[0] != p[i]:
                    break
                else:
                    a = (set(a) - set(p))
                    a.add(p[i - 1])
                    a.add(p[0])
                    if k == N:
                        print("Yes")
                        exit()
            else:
                if p[i - 1] ^ p[i + 1] != p[i]:
                    break
print("No")

Submission status: Time Limit Exceeded

Solution code:
import itertools

def checkXOR(a, b, c):
  if bin(b) == bin(a ^ c):
    return False
  else:
    return True
    
n = int(input())
a = list(map(int, input().split()))
ff = False
for p in list(itertools.permutations(a)):
  for i in range(0, n):
    if i == 0:
      ff = checkXOR(p[n-1],p[i],p[i+1])
    elif i == n-1:
      ff = checkXOR(p[i-1],p[i],p[0])
    else:
      ff = checkXOR(p[i-1],p[i],p[i+1])
    if ff:
      break
  if ff:
    break

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

def check(a):
    for i in range(n):
        if i == 0:
            if a[i+1]^a[n-1] != a[i]:
                break
        elif i == n-1:
            if a[i-1]^a[0] != a[i]:
                break
        else:
            if a[i-1]^a[i+1] != a[i]:
                break
    else:
        return True
    return False
        
n = int(input())
a = [int(i) for i in input().split()]
b = list(itertools.permutations(a))
for x in b:
    if check(x) == True:
        print('Yes')
        break
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
a = list(map(int, input().split()))
first = a.pop(0)
aa = list(itertools.permutations(a))

answer = "No"
for i in range(len(aa)):
  alist = list(aa[i])
  alist.insert(0, first)
  for j in range(len(alist) - 1):
    if alist[j] == (alist[j - 1] ^ alist[j + 1]):
      answer = "Yes"
      break
  if answer == "Yes":
    break
    
print(answer)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
f=0
for i in a:
  for j in a:
    if(i!=j):
      x=i^j
      if(x not in a):
        f=1
        break
if(f==0):
  print('Yes')
else:
  print('No')

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a= list(map(int, input().split()))
a.sort()
b=[None]*N
b[0]=a[0]
A=0
for i in range(1,N):
  b[1]=a[i]
  for j in range(2,N):
    b[j]=b[j-1]^b[j-2]
    if b[j] not in a:
      break
  else:
    b.sort()
    if a==b:
      A=1
      break
if A==1:
  print("Yes")
else:
  print("No")

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
a = list(map(int, input().split()))
first = a.pop(0)
aa = list(itertools.permutations(a))

answer = "Yes"
for i in range(len(aa)):
  alist = list(aa[i])
  alist.insert(0, first)
  for j in range(len(alist) - 1):
    if alist[j] != (alist[j - 1] ^ alist[j + 1]):
      answer = "No"
      break
  if answer == "No":
    break
    
print(answer)

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
    ans.append(flag)

if 1 in ans:print("Yes")
else:print("No")

Submission status: Time Limit Exceeded

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

for i in range(n-1):
    for j in range(i+1,n):
        if a[i]^a[j] in a:
            pass
        else:
            result = False

a_unique = list(set(a))
if len(a_unique) > 3:
    result = False

if result == True:
    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 list(itertools.permutations(a)):
        a_tmp = list(l)
        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'
                print(ans)
                return 'False'
    print(ans)
    return True

ret = get_ans(a, N)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools

n=input()
n=int(n)
a=list(map(int,input().split()))
a_list=list(itertools.permutations(a))
flag=False
for i in range(math.factorial(len(a))):
  for j in range(n):
    if j==1:
      if a_list[i][j-1]==a_list[i][j]^a_list[i][n-1]:
        continue
      else:
        break
    elif j==n-1:
      if a_list[i][n-1]==a_list[i][n-2]^a_list[i][0]:
        flag=True
        break
      else:
        break
    else:
      if a_list[i][j-1]==a_list[i][j]^a_list[i][j-2]:
        continue
      else:
        break
  if flag==True:
    print('Yes')
    break
if flag==False:
  print('No')

Submission status: Time Limit Exceeded

Solution code:
import itertools
import sys

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

def q(line):
  flg = True
  for i in range(len(line)):
    max = i + 1
    min = i - 1
    if i + 1 > n - 1:
      max = 0
    elif i - 1 < 0:
      min = n - 1
    wa = line[min] ^ line[max]
    if line[i] != wa:
      flg = False
  return flg



for v in itertools.permutations(line,n):
  flg = q(list(v))
  if flg:
    print('Yes')
    sys.exit()

print('No')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque, defaultdict
from copy import copy

def main():
    N = int(input())
    _ = input().split()
    D_ = defaultdict(int)
    a_line = []
    for i, a in enumerate(_):
        a_ = int(a)
        D_[a_] += 1
        if i == 0:
            start = a_
            D_[start] -= 1
        a_line += [a_]

    Q = []
    for a in a_line:
        D = copy(D_)
        D[a] -= 1
        Q += [(start, a, D, N-2)]
    Q = deque(Q)

    while len(Q):
        a, b, D, count = Q.popleft()
        c = a ^ b
        d_ = D.get(c, None)
        if d_ is None or d_ == 0:
            continue
        D[d_] -= 1
        count -= 1
        if count == 0:
            print('Yes')
            return
        Q.append((b, c, D, count))

    print('No')
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
N = int(input())
a = list(map(int,input().split()))
for i in itertools.permutations(a):
    ans = "Yes"
    for j in range(1,N):
        if abs(a[j] - a[j-2]) != a[j - 1]:
            ans = "No"
            break
    if ans == "Yes":
        print(ans)
        exit()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N=int(input())
an=[int(x) for x in map(int,input().split()[:N])]

def judgeXOR(v):
    for x in range(N):
        if x==0:
            if (v[N-1] ^ v[x+1] ) != (x+1):
                return False
        elif x==(N-1):
            if (v[0] ^ v[x-1]) !=(x+1):
                return False
        else:
            if (v[x-1] ^ v[x+1])!=(x+1):
                return False
    return True

for v in itertools.permutations(an,N):
    if v[0] !=an[0]:
        continue
    if judgeXOR(v):
        print("Yes")
        exit()

print("No")

Submission status: Time Limit Exceeded

Solution code:
import itertools
import copy

def main():
    n = int(input())
    d_list = [int(i) for i in input().rstrip().split(' ')]
    a_list = list(itertools.permutations(d_list))
    flg = False
    
    for a in a_list:
        flg_2 = True
        for i in range(len(a)):
            if i == 0:
                calc = a[-1] ^ a[i+1]
            elif i == len(d_list)-1:
                calc = a[i-1] ^ a[0]
            else:
                calc = a[i-1] ^ a[i+1]
            if a[i] != calc:
                flg_2 = False
                break
        if flg_2:
            print('Yes')
            return
        
    print("No")
    return

main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
a = list(map(int, input().split()))
first = a.pop(0)
aa = list(itertools.permutations(a))

answer = "Yes"
for i in range(len(aa)):
    alist = list(aa[i])
    alist.insert(0, first)
    for j in range(len(alist) - 1):
      if alist[j] != (alist[j - 1] ^ alist[j + 1]):
        answer = "No"
        break
      if answer == "No":
        break
    
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys
N=int(input())
a=list(map(int,input().split()))
b,c,d=0,1,0
while True:
    while True:
        if a[b]%(2**c)>=2**(c-1):
            d+=1
        b+=1
        if b==N:
            if d%2==1:
                print('No')
                sys.exit()
            else:
                break
    b,d=0,0
    c+=1
    if c==30:
        break
print('Yes')

Submission status: Time Limit Exceeded

Solution code:
# A - XOR Circle
import itertools
import sys

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

pattern = list(itertools.permutations(seq))
for i in range(0, N):
    for j in range(0, len(pattern[i])):
        if (pattern[i][j-2] ^ pattern[i][j]) != pattern[i][j-1]:
            print('No')
            sys.exit(0)
print('Yes')

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
def solve():
    N = int(input())
    A = dict()
    for a in map(int, input().split()):
        A[a] = A.get(a,0) + 1
    a = list(A)[0]
    if A[a] == 1:
        A.pop(a)
    else:
        A[a] -= 1

    for b in A:
        B = A.copy()
        c_, c = a, b
        while c in B:
            if B[c] == 1:
                B.pop(c)
            else:
                B[c] -= 1
            c_, c = c, c_^c
        if B == dict() and c == a:
            return 'Yes'
    return 'No'
if __name__ == '__main__':
    print(solve())

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
N = int(input())
a = list(map(int,input().split()))
for i in itertools.permutations(a):
    ans = "Yes"
    for j in range(1,N):
        if abs(a[j] - a[j-2]) != a[j - 1]:
            ans = "No"
    if ans == "Yes":
        print(ans)
        exit()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations
n=int(input())
N=list(map(int,input().split()))
flag=1
ans=[]
for i in list(permutations(N)):
    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]:
            continue
        else:flag=0
    ans.append(flag)

if 1 in ans:print("Yes")
else:print("No")

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    N = int(input())
    a_list = list(map(int, input().split()))

    # a[0] からスタート
    found = False
    start = a_list[0]
    second = 0
    third = 0
    for i in range(1, N):
        for j in range(i+1, N):
            ai = a_list[i]
            aj = a_list[j]
            b = start ^ ai
            if b == aj:
                found = True
                second = j
                third = i
                break
        if found == True:
            break

    if found == False:
        print("No")
        return

    # これで、0, second, third が決まったので、あとは順次決める
    hat_list = [a_list[0], a_list[second], a_list[third]]

    a_list[0] = None
    a_list[second] = None
    a_list[third] = None

    dprint(hat_list)

    cnt = 3
    while True:
        # N個作ったら終了
        if cnt == N:
            break

        left = hat_list[-2]
        center = hat_list[-1]

        # 次の数字
        right = left ^ center

        # 検索
        try:
            rid = a_list.index(right)
        except:
            dprint("break", right, a_list)
            print("No")
            return

        dprint("found", right, rid, a_list)

        a_list[rid] = None
        hat_list.append(right)
        cnt += 1
        dprint("next", cnt, hat_list, a_list)

    # 最後に周期条件
    center = hat_list[0]
    right = hat_list[1]
    left = hat_list[-1]

    if right ^ left == center:
        print("Yes")
    else:
        dprint("break", center, right, left)
        print("No")


solve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
flag = False

for i in range(len(a)):
  ta = a.pop(i)
  sum_xor = 0
  for _a in a:
    sum_xor = sum_xor ^ _a
  if sum_xor==ta:
    flag = True
    break
  a.append(ta)

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
from collections import defaultdict, deque
import math
from math import factorial
import fractions
import re

BIGNUM = 10 ** 9 + 7

def convtobit(x):
    b = bin(x)
    bits = list(map(int, b[2:]))
    bits = [0] * (32 - len(bits)) + bits
    return tuple(bits)

def minus(x, y):
    b = []
    for xb, yb in zip(x, y):
        bit = 1 if xb != yb else 0
        b.append(bit)
    return tuple(b)

def main():
    #A, B, C, K = map(int, input().split())
    N = int(input())
    As = list(map(int, input().split()))

    # find N-th and 2nd number
    first = convtobit(As[0])
    As = As[1:]
    Adict = defaultdict(int)
    for a in As:
        b = convtobit(a)
        Adict[b] += 1

    assert convtobit(2) == tuple([0]*30 + [1, 0])
    assert convtobit(7) == tuple([0]*29 + [1, 1, 1])

    N_seconds = []
    for a in As:
        a = convtobit(a)
        wanted = minus(first, a)
        if wanted in Adict:
            if wanted != a or (wanted == a and Adict[a] >= 2):
                N_seconds.append((wanted, a))

    if not N_seconds:
        print("No")
        return

    for Nth, second in N_seconds:
        adict = dict(Adict)
        adict[Nth] -= 1
        adict[second] -= 1

        is_failure = False
        prev = (first, second)
        for i in range(N-3):
            next_th = minus(prev[1], prev[0])
            if next_th in adict and adict[next_th] > 0:
                adict[next_th] -= 1
                prev = (prev[1], next_th)
            else:
                is_failure = True
                break

        if not is_failure:
            next_th = minus(prev[1], prev[0])
            if next_th == Nth:
                print("Yes")
                return

    print("No")

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

def checkXOR(a, b, c):
  if bin(b) == bin(a ^ c):
    return False
  else:
    return True
    
n = int(input())
a = list(map(int, input().split()))
#print(n, a)
all = list(itertools.permutations(a))
ff = False
for p in range(0, len(all)):
  for i in range(0, n):
    if i == 0:
      ff = checkXOR(all[p][n-1],all[p][i],all[p][i+1])
    elif i == n-1:
      ff = checkXOR(all[p][i-1],all[p][i],all[p][0])
    else:
      ff = checkXOR(all[p][i-1],all[p][i],all[p][i+1])
    if ff:
      break
  if ff:
    break

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

Submission status: Time Limit Exceeded

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

def check(N, a):
    flag = True
    if( (a[-1] ^ a[1]) != a[0]):
        return False
    if( (a[-2] ^ a[0]) != a[-1]):
        return False
    for i in range(1, N-1):
        if( (a[i-1] ^ a[i+1]) != a[i]):
            return False
    return True

for ap in itertools.permutations(a):
    if check(N, list(ap)):
        print("Yes")
        sys.exit()
print("No")

Submission status: Time Limit Exceeded

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

import copy
hats = A[:]

import queue
q = queue.Queue()

for i, h1 in enumerate(hats):
    hats2 = hats[i+1:]
    for j, h2 in enumerate(hats2):
        hats3 = hats2[j+1:]
        for h3 in hats3:
            set_hats = set(hats)
            set_hats.discard(h1)
            set_hats.discard(h2)
            set_hats.discard(h3)
            q.put({'top': h1, 'three': [h1, h2, h3], 'set_hats': set_hats})
            q.put({'top': h1, 'three': [h1, h3, h2], 'set_hats': set_hats})
            
ans = 'No'
while not q.empty():
    data = q.get()
    #print(data)
    three = data['three']
    
    if three[0] ^ three[2] != three[1]:
        continue
       
    top = data['top']
    if top == None:
        ans = 'Yes'
        break
    
    set_hats = data['set_hats']
    if len(set_hats) == 0:
        three = three[1:] + [top]
        top = None
        q.put({'top': top, 'three': three, 'set_hats': set_hats})
    else:
        for s in set_hats:
            tmp = copy.copy(set_hats)
            tmp.discard(s)
            three = three[1:] + [s]
            q.put({'top': top, 'three': three, 'set_hats': tmp})
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

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


for i in range(1,N):

    b = [-1]*N
    c = a[:]

    b[0] = c[0]
    c[0] = -1

    b[1] = c[i]
    c[1] = -1

    for j in range(2,N):
        if b[j-2]^b[j-1] in c:
            n = c.index(b[j-2]^b[j-1])
            b[j] = c[n]
            c[n] = -1
        else:
            break

        if j == N-1:
            print("Yes")
            sys.exit()

print("No")

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
def solve():
    N = int(input())
    A = dict()
    for a in map(int, input().split()):
        A[a] = A.get(a,0)+1
    a = min(A.keys())
    A[a] -= 1
    if A[a] == 0:
        del A[a]

    for b in A.keys():
        B = A.copy()
        c_, c = a, b
        while c in B.keys():
            B[c] -= 1
            if B[c] == 0:
                del B[c]
            c_, c = c, c_^c
        if B == dict() and c == a:
            return 'Yes'
    return 'No'
if __name__ == '__main__':
    print(solve())

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
ans=[]
for i in range(n):
    for j in range(n):
        if i<j:
            c=a[i]^a[j]
            if a.count(c)>0:
                ans.append(c)
            else:break
ans=sorted(ans)
print("Yes" if ans==a else "No")

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder A
"""

import sys, math, random
import numpy as np

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column
# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']
# S.replace('ABC','X') # "testABCABC" -> "testXX"
# S=numpp.assaray(S)=="#"
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LLS(): return [list(sys.stdin.readline().rstrip()) for _ in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()



def main():
    N=I()
    A = list(map(int,input().split()))
    out=True


    # import itertools
    # A="123456"
    # for p in itertools.permutations(A):
    #     print(p)
    # exit()


    import itertools
    for p in itertools.permutations(A):
        if p[0]!=A[0]:
            print("No")
            exit()
        else:
            out=True
            for i in range(N):
                if i==0:
                    last=N-1
                else:
                    last=i-1
                if i==N-1:
                    ne=0
                else:
                    ne=i+1
                # print(last,ne)
                if p[last]^p[ne]!=p[i]:
                    out=False
                    break
            if out:
                print("Yes")
                exit()
                        # exit()

        # print(p)
        # if p[0]!='A': continue
    # for i in range(N):
    #     if i==0:
    #         last=N-1
    #     else:
    #         last=i-1
    #     if i==N-1:
    #         ne=0
    #     else:
    #         ne=i+1
    #     print(last,ne)
    #     if A[last]^A[ne]!=A[i]:
    #         print("No")
    #         # exit()
    # print("Yes")

    # A.append(A[0])
    # A.append(A[1])
    # for i in range(1,N+1):
    #     # print(i-1,i+1)
    #     if A[i-1]^A[i+1]!=A[i]:
    #         print("No")
    #         exit()
    # print("Yes")

    # print(A,B)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
N = input()

N = N.split(" ")
Ni = []
for i in N:
    Ni.append(int(i))

count = 0
for q in set(itertools.permutations(Ni,n)):
    list_xor = []
    for i in range(len(q)):
        if len(q) > i+1:
            xor = (q[i] ^ q[i+1])
        else:
            xor = (q[0] ^ q[i])
        list_xor.append(xor)

        flg = "No"
    for i in list(itertools.permutations(q)):
        if i == tuple(list_xor):
            flg = 'Yes'
            break
print(flg)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

for s in list(itertools.permutations(l)):
    if sum([s[(i-1)%n]^s[(i+1)%n]==s[i] for i in range(n)])==n:
        print('Yes')
        exit()
    else:pass
print('No')

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

b = []
for i in range(N):
    for j in range(i + 1, N):
        b.append(int(bin(a[i]^a[j]),0))
        
if set(a) == set(b):
  print('Yes')
else:
  print('No')

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a= list(map(int, input().split()))
a.sort()
b=[None]*N
b[0]=a[0]
A=0
for i in range(1,N):
  b[1]=a[i]
  for j in range(2,N):
    b[j]=b[j-1]^b[j-2]
  b.sort()
  if a==b:
    A=1
    break
if A==1:
  print("Yes")
else:
  print("No")

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n=int(input())
a=list(map(int,input().split())) 
comb = combinations(a, 2)
re=0
for i in list(comb): 
    if i[0]^i[1] not in a:
        re=0
        break
    else:
        re=1
if re==1:
    print("Yes")
else:
    print("No")