-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.cpp
66 lines (50 loc) · 800 Bytes
/
binary.cpp
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
#include <iostream>
#include <vector>
using namespace std;
void get_path(vector<int>& p_a, int a) {
while ( a != 1 ) {
p_a.push_back(a);
if ( a % 2 == 0 ) {
a = a/ 2;
} else {
a--;
a = a/ 2;
}
}
p_a.push_back(a);
/*
for ( int i = 0; i < p_a.size(); i++ ) {
cout << p_a[i] << " ";
}
cout << endl << endl;
*/
}
int shortest_path(int a, int b ) {
vector<int> p_a;
vector<int> p_b;
get_path(p_a, a);
get_path(p_b, b);
int i = p_a.size() - 1;
int j = p_b.size() - 1;
while ( (i >= 0 && j >= 0 ) && p_a[i] == p_b[j] ) {
i--;
j--;
}
return i + j + 2;
}
int main()
{
int t;
cin >> t;
while (t-- ) {
int a;
int b;
cin >> a >> b;
if ( a == b ) {
cout << 0 << endl;
} else {
cout << shortest_path(a, b) << endl;
}
}
return 0;
}