Problem:
 Score : 600 points    Problem Statement There are N piles of stones. The i -th pile has A_i stones.  Aoki and Takahashi are about to use them to play the following game:   Starting with Aoki, the two players alternately do the following operation:  Operation: Choose one pile of stones, and remove one or more stones from it.    When a player is unable to do the operation, he loses, and the other player wins.   When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.  In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1 -st pile to the 2 -nd pile before the game begins.  If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.      Constraints  2 \leq N \leq 300  1 \leq A_i \leq 10^{12}         Input Input is given from Standard Input in the following format:  N  A_1  \ldots  A_N       Output Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.        Sample Input 1 2
5 3      Sample Output 1 1  Without moving stones, if Aoki first removes 2 stones from the 1 -st pile, Takahashi cannot win in any way.  If Takahashi moves 1 stone from the 1 -st pile to the 2 -nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.       Sample Input 2 2
3 5      Sample Output 2 -1  It is not allowed to move stones from the 2 -nd pile to the 1 -st.       Sample Input 3 3
1 1 2      Sample Output 3 -1  It is not allowed to move all stones from the 1 -st pile.       Sample Input 4 8
10 9 8 7 6 5 4 3      Sample Output 4 3       Sample Input 5 3
4294967297 8589934593 12884901890      Sample Output 5 1  Watch out for overflows.

Solution (Python):
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

    if N == 2:
        if A[0] == A[1]:
            print(0)
        elif A[0] < A[1]:
            print(-1)
        else:
            if (A[0] - A[1]) %2 == 0:
                print((A[0] -  A[1])//2)
            else:
                print(-1)
        exit()

    g1 = A[0]
    g2 = A[1]
    g3 = 0
    for i in range(2, N):
        g3 ^= A[i]
    n1 = g1.bit_length()
    n2 = g2.bit_length()
    n3 = g3.bit_length()
    NN = max(n1, n2, n3)
    ans = 0
    cnt = 0
    while True:
        cnt += 1
        if cnt == 10000:
            print(-1)
            exit()
        for i in range(NN):
            if (g1 >> i & 1) ^ (g2 >> i & 1) != (g3 >> i & 1):
                r1 = g1 % (1 << i) + 1
                r2 = (1<<i) - g2 % (1 << i)
                if r1 < r2:
                    g1 -= r1
                    g2 += r1
                    ans += r1
                    break
                elif r1 > r2:
                    g1 -= r2
                    g2 += r2
                    ans += r2
                    break
                else:
                    print(-1)
                    exit()
            if g1 ^ g2 == g3:
                if ans != A[0]:
                    print(ans)
                else:
                    print(-1)
                exit()


if __name__ == '__main__':
    main()

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

S = a[0] + a[1]
C = 0
for i in range(2, n):
    C ^= a[i]

D = S - C

if D < 0 or D % 2 == 1:
    print(-1)
    exit()

D //= 2

ans = 0

free_keta = []

for i in range(100):
    if (C & (1 << i)) != 0:
        free_keta.append(i)
        if (D & (1 << i)) != 0:
            print(-1)
            exit()
    else:
        ans += (D & (1 << i))

if ans > a[0]:
    print(-1)
    exit()
        
for keta in free_keta[::-1]:
    if ans + (1 << keta) <= a[0]:
        ans += (1 << keta)

if ans == 0:
    print(-1)
    exit()
    
print(a[0] - ans)

Solution (Python):
import sys
def input():return sys.stdin.readline()[:-1]
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
def LN(n):return [N() for i in range(n)]
def LL(n):return [L() for i in range(n)]
n=N()
l=L()
sx=0
for i in l[2:]:
    sx^=i
if sx>l[0]+l[1]:
    print(-1)
    quit()
if (l[0]+l[1]-sx)%2==1:
    print(-1)
    quit()
if sx^l[0]^l[1]==0:
    print(0)
    quit()
x=(l[0]+l[1]-sx)//2
y=x
if x&sx>0:
    print(-1)
    quit()
if x>l[0]:
    print(-1)
    quit()
for i in range(45,-1,-1):
    if sx>>i&1:
        if l[0]>=(x+(1<<i)):
            x+=(1<<i)
if x==0:
    print(-1)
    quit()
print(l[0]-x)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
xorValue = 0
sumValue = A[0] + A[1]
for i in range(2,N):
    xorValue ^= A[i]
if sumValue % 2 != xorValue % 2:
    print(-1)
else:
    Base = (sumValue - xorValue) // 2
    if Base > A[0]:
        print(-1)
    else:
        strXOR = bin(xorValue)[2:]
        minStoneMovement = A[0] - Base
        strBase = bin(Base)[2:]
        lenStrXOR = len(strXOR)
        cannotMove = False
        if Base < 0:
            cannotMove = True
        for i in range(-1,-1 * min(len(strXOR),len(strBase)) - 1,-1):
            if strBase[i] == '1' and strXOR[i] == '1':
                cannotMove = True
                break
        if not cannotMove:
            for i in range(lenStrXOR):
                if strXOR[i] == '1' and minStoneMovement >= 2 ** (lenStrXOR - 1 - i):
                    minStoneMovement -= 2 ** (lenStrXOR - 1 - i)
        if minStoneMovement == A[0] or cannotMove:
            print(-1)
        else:
            print(minStoneMovement)

Solution (Python):
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    aa=LI()
    x=0
    for a in aa[2:]:x^=a
    t=aa[0]+aa[1]-x
    if t<0 or t&1:
        print(-1)
        exit()
    t>>=1
    
    a0=a1=0
    for i in range(40,-1,-1):
        dt=t>>i&1
        dx=x>>i&1
        if (dt,dx)==(1,1):
            print(-1)
            exit()
        if (dt,dx)==(1,0):
            a0|=1<<i
            a1|=1<<i

    for i in range(40,-1,-1):
        dt=t>>i&1
        dx=x>>i&1
        if (dt,dx)==(0,1):
            if (a0|1<<i)<=aa[0]:a0|=1<<i
            else:a1|=1<<i

    
    if a0>0 and a0<=aa[0]:print(aa[0]-a0)
    else:print(-1)

main()

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
t = 0
for a in A[2:]:
    t ^= a
s = sum(A[:2])
if s < t or (s ^ t) & 1:
    print(-1)
    exit()

u = (s - t) // 2
a = A[0]
n = 0
for i in range(40)[::-1]:
    b, c = (t >> i) & 1, (u >> i) & 1
    if b == c == 1:
        print(-1)
        exit()
    if b == 0 and c == 1:
        n += 1 << i
for i in range(40)[::-1]:
    b, c = (t >> i) & 1, (u >> i) & 1
    if b == 1 and c == 0:
        if n + (1 << i) <= a:
            n += 1 << i

if n > a or n == 0:
    print(-1)
else:
    print(a - n)

Solution (Python):


import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,*a = map(int,read().split())

v = 0
for i in a[2:]:
    v ^= i

w = a[0]+a[1]

z = w-v
if z&1==1 or z<0:
    print(-1)
else:
    z //= 2
    """
    z = p&q
    v = p^q
    """
    if z&v:
        print(-1)
        exit()

    ans = z
    for i in range(60,-1,-1):
        m = 1<<i
        if v&m:
            if ans+m <= a[0]:
                ans += m
                

    if ans > a[0] or ans==0: print(-1)
    else: print(a[0]-ans)

Solution (Python):
import bisect

N=int(input())
A=list(map(int,input().split()))
x=0
for i in range(2,N):
    x^=A[i]

if (A[0]+A[1]-x)%2!=0:
    print(-1)
else:
    AND=(A[0]+A[1]-x)//2
    u=AND
    v=AND
    if AND&x!=0:
        print(-1)
        exit()
    t=[i for i in range(40) if x>>i &1==1]
    
    k=len(t)//2
    data1=[]
    data2=[]
    for i in range(2**k):
        temp=0
        for j in range(k):
            if i>>j&1==1:
                temp+=2**t[j]
        data1.append(temp)
    for i in range(2**(len(t)-k)):
        temp=0
        for j in range(len(t)-k):
            if i>>j&1==1:
                temp+=2**t[k+j]
        data2.append(temp)

    
    ans=-1
    val=float("inf")
    for test in data1:
        temp=test+u
        
        id=bisect.bisect_right(data2,A[0]-temp)
        if id!=0:
            temp+=data2[id-1]
            if val>A[0]-temp and A[0]>=temp and temp!=0:
                val=A[0]-temp
                ans=temp
        else:
            if val>A[0]-temp and A[0]>=temp and temp!=0:
                val=A[0]-temp
                ans=temp
        
        
    

    if val!=float("inf"):
        print(val)
    else:
        print(-1)

Solution (Python):
import sys
from functools import lru_cache

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *A = map(int, read().split())

a, b = A[:2]
xor = 0
for x in A[2:]:
    xor ^= x

INF = 10**13

@lru_cache(None)
def F(a, b, xor):
    if (a & 1) ^ (b & 1) != (xor & 1):
        return INF
    if xor == 0:
        return INF if a < b else (a - b) // 2
    
    x = 2 * F(a // 2, b // 2, xor // 2)
    
    y = 2 * F((a - 1) // 2, (b + 1) // 2, xor // 2) + 1
    x = min(x, y)
    if x >= INF:
        x = INF
    return x

x = F(a, b, xor)
if x >= a:
    x = -1
print(x)