Problem:


Score : 500 points

Problem Statement

   #nck {
      width: 30px;
      height: auto;
   }

A cheetah and a cheater are going to play the game of Nim.
In this game they use N piles of stones.
Initially the i-th pile contains a_i stones.
The players take turns alternately, and the cheetah plays first.
In each turn, the player chooses one of the piles, and takes one or more stones from the pile.
The player who can't make a move loses.

However, before the game starts, the cheater wants to cheat a bit to make sure that he can win regardless of the moves by the cheetah.
From each pile, the cheater takes zero or one stone and eats it before the game.
In case there are multiple ways to guarantee his winning, he wants to minimize the number of stones he eats.

Compute the number of stones the cheater will eat.
In case there is no way for the cheater to win the game even with the cheating, print -1 instead.

Constraints

1 ≤ N ≤ 10^5

2 ≤ a_i ≤ 10^9

Input

The input is given from Standard Input in the following format:

N
a_1
:
a_N

Output

Print the answer.

Sample Input 1

3
2
3
4

Sample Output 1

3

The only way for the cheater to win the game is to take stones from all piles and eat them.

Sample Input 2

3
100
100
100

Sample Output 2

-1



Sample input:
3
2
3
4


Sample output:
3

Submission status: Runtime Error

Solution code:
n=int(raw_input())
a=[int(raw_input()) for _ in xrange(n)]
mxlba=0
for i in xrange(n):
    mxlba=max(mxlba,len(bin(a[i])[2:]))
cnt=[0]*(mxlba+5)
for i in xrange(n):
    bit=bin(a[i])[2:]
    lb=len(bit)
    for j in xrange(lb-1,-1,-1):
        if bit[j]=="1":
            cnt[lb-1-j]+=1
            break

axor=0
for i in xrange(n):
    axor^=a[i]
if axor==0:
    print(0)
    exit()
axor=bin(axor)[2:]
la=len(axor)
ans=0
i=-1
while i<la:
    i+=1
    if axor[i]=="1":
        if cnt[i]==0:
            print(-1)
            exit()
        else:
            ans+=1
            cnt[i]-=1
            if la-i-1==0:
                break
            tmp=int("1"*(la-i-1),2)
            axor=int(axor,2)^tmp
            axor=bin(axor)[2:]

print(ans)

Submission status: Runtime Error

Solution code:
n=int(raw_input())
a=[int(raw_input()) for _ in xrange(n)]
mxlba=0
for i in xrange(n):
    mxlba=max(mxlba,len(bin(a[i])[2:]))
cnt=[0]*(mxlba+5)
for i in xrange(n):
    bit=bin(a[i])[2:]
    lb=len(bit)
    for j in xrange(lb-1,-1,-1):
        if bit[j]=="1":
            cnt[lb-1-j]+=1
            break

axor=0
for i in xrange(n):
    axor^=a[i]

axor=bin(axor)[2:]
la=len(axor)
ans=0
i=-1
while i<la:
    i+=1
    if axor[i]=="1":
        if cnt[i]==0:
            print(-1)
            exit()
        else:
            ans+=1
            cnt[i]-=1
            if la-i-1==0:
                break
            tmp=int("1"*(la-i-1),2)
            axor=int(axor,2)^tmp
            axor=bin(axor)[2:]

print(ans)

Submission status: Runtime Error

Solution code:
n=int(raw_input())
a=[int(raw_input()) for _ in xrange(n)]
mxlba=0
for i in xrange(n):
    mxlba=max(mxlba,len(bin(a[i])[2:]))
cnt=[0]*mxlba
for i in xrange(n):
    bit=bin(a[i])[2:]
    lb=len(bit)
    for j in xrange(lb-1,-1,-1):
        if bit[j]=="1":
            cnt[lb-1-j]+=1
            break

axor=0
for i in xrange(n):
    axor^=a[i]

axor=bin(axor)[2:]
la=len(axor)
ans=0
i=-1
while i<la:
    i+=1
    if axor[i]=="1":
        if cnt[i]==0:
            print(-1)
            exit()
        else:
            ans+=1
            cnt[i]-=1
            if la-i-1==0:
                break
            tmp=int("1"*(la-i-1),2)
            axor=int(axor,2)^tmp
            axor=bin(axor)[2:]

print(ans)

Submission status: Runtime Error

Solution code:
n=int(raw_input())
a=[int(raw_input()) for _ in xrange(n)]
mxlba=0
for i in xrange(n):
    mxlba=max(mxlba,len(bin(a[i])[2:]))
cnt=[0]*mxlba
for i in xrange(n):
    bit=bin(a[i])[2:]
    lb=len(bit)
    for j in xrange(lb-1,-1,-1):
        if bit[j]=="1":
            cnt[lb-1-j]+=1
            break

axor=0
for i in xrange(n):
    axor^=a[i]

axor=bin(axor)[2:]
ans=0
i=-1
while i<mxlba:
    i+=1
    if axor[i]=="1":
        if cnt[i]==0:
            print(-1)
            exit()
        else:
            ans+=1
            cnt[i]-=1
            if mxlba-i-1==0:
                break
            tmp=int("1"*(mxlba-i-1),2)
            axor=int(axor,2)^tmp
            axor=bin(axor)[2:]
print(ans)

Submission status: Runtime Error

Solution code:
n=int(raw_input())
a=[int(raw_input()) for _ in xrange(n)]
cnt=[0]*len(bin(max(a))[2:])
for i in xrange(n):
    bit=bin(a[i])[2:]
    lb=len(bit)
    for j in xrange(lb-1,-1,-1):
        if bit[j]=="1":
            cnt[lb-1-j]+=1
            break

axor=0
for i in xrange(n):
    axor^=a[i]

axor=bin(axor)[2:]
mx=len(bin(max(a))[2:])
ans=0
i=-1    
while i<mx:
    i+=1
    if axor[i]=="1":
        if cnt[i]==0:
            print(-1)
            exit()
        else:
            ans+=1
            cnt[i]-=1
            if mx-i-1==0:
                break
            tmp=int("1"*(mx-i-1),2)
            axor=int(axor,2)^tmp
            axor=bin(axor)[2:]
print(ans)

Submission status: Runtime Error

Solution code:
import math as m
import functools


def int_reverse(num):
    num_len = len(bin(num))-2
    f = 2**num_len-1
    return num ^ f

n = int(input())
A = [int(input()) for i in range(n)]
lsbs = [[i for i in range(int(m.log2(a))+1) if (a>>i)&1][0] for a in A]
nim = functools.reduce(lambda x,y:x^y, A)

cnt = 0
if nim == 0:
    ans = 0
else:
    for lsb in reversed(sorted(lsbs)):
        if int(m.log2(nim)) == lsb:
            nim = int_reverse(nim)
            cnt += 1

    ans = cnt if(nim == 0) else -1
print(ans)

Submission status: Runtime Error

Solution code:
import math as m
import functools


def int_reverse(num):
    num_len = len(bin(num))-2
    f = 2**num_len-1
    return num ^ f

n = int(input())
A = [int(input()) for i in range(n)]
lsbs = [[i for i in range(int(m.log2(a))+1) if (a>>i)&1][0] for a in A]
nim = functools.reduce(lambda x,y:x^y, A)

cnt = 0
for lsb in reversed(sorted(lsbs)):
    if int(m.log2(nim)) == lsb:
        nim = int_reverse(nim)
        cnt += 1

ans = cnt if(nim == 0) else -1
print(ans)