-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.py
More file actions
executable file
·56 lines (38 loc) · 1001 Bytes
/
Copy pathsol.py
File metadata and controls
executable file
·56 lines (38 loc) · 1001 Bytes
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
def computeDepth(n):
depth = 1
nodes = 1
while nodes < n:
depth += 1
nodes = treeSize(depth)
return depth
def treeSize(n):
return pow(2, n) - 1
def nodesAtDepth(n):
return treeSize(n) - treeSize(n - 1)
def sequenceToRational(sequence):
left = 1
right = 1
for char in sequence:
if char == "L":
right = left + right
else:
left = left + right
return f"{left}/{right}"
def solve(n):
depth = computeDepth(n)
sequence = ""
while depth > 1:
index = n - treeSize(depth - 1) - 1
if index < nodesAtDepth(depth) / 2:
sequence += "L"
n = n - nodesAtDepth(depth) // 2
else:
sequence += "R"
n = n - nodesAtDepth(depth)
depth -= 1
return sequenceToRational(sequence)
if __name__ == "__main__":
p = int(input())
for _ in range(p):
k, n = map(int, input().split())
print(k, solve(n))