-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10970.cpp
49 lines (46 loc) · 884 Bytes
/
P10970.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
#include <iostream>
bool readInts(int &a, int &b) {
a = b = 0;
bool aStarted = false;
bool aDone = false;
char w[20];
if(!std::cin.getline(w, 20))
return false;
for(int i = 0; i < 20; ++i) {
if(w[i] >= '0' && w[i] <= '9') {
if(aDone)
b = b*10 + (w[i]-'0');
else {
a = a*10 + (w[i]-'0');
aStarted = true;
}
}
else {
if(!isprint(w[i]))
break;
if(aStarted)
aDone = true;
}
}
return true;
}
int cuts(int *buf, int a, int b) {
if(a > b)
return cuts(buf, b, a);
if(a == 1)
return b-1;
int idx = (a-1)*300+b-1;
if(buf[idx] != -1)
return buf[idx];
return buf[idx] = cuts(buf, a, b/2) + cuts(buf, a, b-b/2) + 1;
}
int main() {
int N, M;
int buf[90000];
for(int i = 0; i < 90000; ++i)
buf[i] = -1;
while(readInts(N, M)) {
std::cout << cuts(buf, N,M) << std::endl;
}
return 0;
}