-
Notifications
You must be signed in to change notification settings - Fork 0
/
rumour.py
71 lines (65 loc) · 1.82 KB
/
rumour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''
Problem Link https://csacademy.com/ieeextreme-practice/task/9ca8fafd184f553a903734761546a224/
'''
from math import floor, log, ceil
def get_same_level_distance(level, a, b):
distance = 0
while (a!=b):
a = a//2
b = b//2
distance += 2
return distance
''' LOL NOT NEEDED
def _get_same_level_distance(level, a, b):
start_of_level = 2**level
end_of_level = 2**(level+1)-1
length_of_level = end_of_level-start_of_level
#0...7
mid = length_of_level/2
a -= start_of_level
b -= start_of_level
distance = level*2
def cal():
nonlocal a, mid, b, distance, start_of_level, end_of_level
#print(mid, start_of_level, end_of_level)
if (start_of_level >= end_of_level):
return
if (a < mid and b < mid):
distance -=2
end_of_level = floor(mid)
mid = (start_of_level+end_of_level)/2
elif (a > mid and b > mid):
distance -=2
end_of_level = ceil(mid)
mid = (start_of_level+end_of_level)/2
else:
return
cal()
cal()
return distance
'''
q = int(input())
for _ in range(q):
a, b = map(int, input().split())
if (a == b):
print(0)
continue
a_level, b_level = floor(log(a, 2)), floor(log(b, 2))
b_distance = 0
a_distance = 0
if (a_level < b_level):
while (b_level != a_level):
b_distance+=1
b_level = b_level-1
b = b//2
elif (b_level < a_level):
while (b_level != a_level):
a_distance+=1
a_level = a_level-1
a = a//2
if (a == b):
print(a_distance+b_distance)
continue
same_level_distance = get_same_level_distance(a_level, a, b)
print(a_distance+b_distance+same_level_distance)
exit(0)