Seongbeom Park 22762bbc16 finish extra/ion-flux-relabeling
Submitting solution...
Submission: SUCCESSFUL. Completed in: 9 hrs, 33 mins, 36 secs.
2022-05-06 04:25:06 +09:00

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)