-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.cpp
More file actions
executable file
·81 lines (69 loc) · 1.22 KB
/
Copy pathsol.cpp
File metadata and controls
executable file
·81 lines (69 loc) · 1.22 KB
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
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
using namespace std;
#define p first
#define q second
typedef pair<int, int> Rational;
Rational parseRational()
{
int p, q;
char _;
cin >> p >> _ >> q;
return {p, q};
}
void answer(int k, Rational r)
{
Rational root = {1, 1};
Rational tmp = r;
string sequence = "";
int height = 0;
while (tmp != root)
{
++height;
if (tmp.p > tmp.q)
{
tmp.p -= tmp.q;
sequence += 'r';
}
else
{
tmp.q -= tmp.p;
sequence += 'l';
}
}
reverse(sequence.begin(), sequence.end());
int pos = 1;
for (const auto &c : sequence)
{
if (c == 'l')
{
pos = 2 * pos - 1;
}
else
{
pos = 2 * pos;
}
}
long long nodes_above_height;
if (height == 0)
{
nodes_above_height = 0;
}
else
{
nodes_above_height = (2 << (height - 1)) - 1;
}
cout << k << " " << nodes_above_height + pos << endl;
}
int main()
{
int n;
cin >> n;
while (n--)
{
int k;
cin >> k;
Rational r = parseRational();
answer(k, r);
}
return 0;
}