-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10940.cpp
74 lines (72 loc) · 1.11 KB
/
P10940.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
67
68
69
70
71
72
73
74
#include <iostream>
int main() {
int N;
bool b[1000]; // Stack of decisions (even and odd steps)
while(true) {
std::cin >> N;
if(N == 0)
return 0;
// Build b:
int bi = 0;
while(N > 1) {
if(N % 2 == 0) {
N /= 2;
b[bi++] = true;
}
else {
N = N/2+1;
b[bi++] = false;
}
}
// Construct ret:
int ret = 0;
while(--bi >= 0) {
if(b[bi]) { // Was even:
N *= 2;
ret = 2*ret+1;
}
else {
N = (N-1)*2+1;
if(ret == 0)
ret = N-1;
else
ret = 2*(ret-1) + 1;
}
}
std::cout << 1+ret << std::endl;
}
}
/*
This is too slow:
int main() {
int N, a[500000];
while(true) {
std::cin >> N;
if(N == 0)
return 0;
int mult = 1;
int M = N;
while(M%2 == 0) {
M/=2;
mult*=2;
}
for(int i = 0; i < M; ++i) {
a[i] = mult*i+mult-1;
}
while(M > 1) {
if(M % 2 == 0) {
M /= 2;
for(int i = 0; i < M; ++i)
a[i] = a[2*i+1];
}
else {
a[0] = a[M-1];
M = M/2+1;
for(int i = 0; i < M-1; ++i)
a[i+1] = a[2*i+1];
}
}
std::cout << 1+a[0] << std::endl;
}
}
*/