We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
# The XOR is simple enough. The trick is that they haven't given us the# numbers to XOR, but their position in the list. To find the actual# numbers, determine the number of numbers up to N bits long in the list.# Count 0, since we can add any new bit to zero.# Then there are 2 numbers with up to 1 bit, 3 with up to 2 bits, etc.# To add the numbers with N bits, add the number of numbers with N-2 bits# or less to the previous total (the numbers for N-1 bits).# The result is the fibonacci sequence, since I just keep adding the last# two numbers. And so the number I want is the Zeckendorf representation# of the given index, reinterpreted as binary (repeatedly subtracting the# largest Fibonacci number available from the index)## I've got one case here that is failing, probably just barely failing the time limit.# Let's try precalculating the powers as the small speedup needed.defzeck(n):res=0foriinrange(f-1,-1,-1):ifn>=fib[i]:n-=fib[i]res+=p2[i]returnresn=int(input())nums=list(map(int,input().strip().split()))fib=[1,2,3]whilefib[-1]<10**18:fib.append(fib[-1]+fib[-2])f=len(fib)p2=[]forjinrange(f):p2.append(2**j)modulus=10**9+7sol=0forjinrange(n):sol=sol^zeck(nums[j])print(sol%modulus)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Chandrima and XOR
You are viewing a single comment's thread. Return to all comments →
Python3 code