Submitting solution... Submission: SUCCESSFUL. Completed in: 9 hrs, 33 mins, 36 secs.
29 lines
593 B
Python
29 lines
593 B
Python
def solution(h, q):
|
|
return [converter(h, n) for n in q]
|
|
|
|
def converter(h, n):
|
|
if n == 2**h - 1:
|
|
return -1
|
|
return reduce(n)
|
|
|
|
def reduce(n):
|
|
b = bin(n)[2:]
|
|
l = len(b)
|
|
c = b.count('1')
|
|
if l == c: # 2**l - 1
|
|
return 2**(l+1) - 1
|
|
fold = 2**(l-1) - 1
|
|
m = n - fold
|
|
if m == fold: # 2 * (2**(l-1) - 1)
|
|
return reduce(m)
|
|
return reduce(m) + fold
|
|
|
|
tests = [
|
|
([3, [7, 3, 5, 1]], [-1, 7, 6, 3]),
|
|
([5, [19, 14, 28]], [21, 15, 29]),
|
|
]
|
|
|
|
for i, o in tests:
|
|
result = solution(*i)
|
|
print (i, result == o, result, o)
|