-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng0frctn.cpp
38 lines (34 loc) · 940 Bytes
/
ng0frctn.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
#include "vector"
#include "algorithm"
#include "iostream"
using namespace std;
//I suppose a lazy approach would be to traverse the tree - logn time per query
//Orrr you could try mathematically deriving a formula for the nth term
string binary(long long n){
string s = "";
while(n > 0){
s += (n % 2) + '0';
n /= 2;
}
reverse(s.begin(), s.end());
return s;
}
int main(){
//Let's traverse the tree - convert n into binary numbers; that gives you the actual path to traverse
while(true){
long long n;
cin >> n;
if(n == 0) break;
string s = binary(n);
long num = 1, dem = 1; //root
for(int i = 1; i < s.size(); i++){
if(s[i] == '0'){
dem += num;
}
else{
num += dem;
}
}
cout << num << "/" << dem << endl;
}
}