-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypnos.cpp
46 lines (45 loc) · 880 Bytes
/
hypnos.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
#include<cstdio>
#include<cstring>
int brk(int i) {
if(i == 0) {
return 0;
} else {
int l = i % 10;
return (l*l) + brk(i/10);
}
}
int isBreakable(int i, int* array) {
if(array[i] != -1) {
return array[i];
} else {
array[i] = -2;
int val = isBreakable(brk(i), array);
if(val == -2) {
array[i] = val;
} else {
array[i] = val + 1;
}
return array[i];
}
}
int main() {
int array[811];
memset(array, -1, sizeof(array));
array[1] = 1;
int n;
scanf("%d", &n);
int x = n;
if(n >= 811) {
x = brk(n);
}
int val = isBreakable(x, array);
if(val == -2) {
printf("-1\n");
} else {
if(n >= 811) {
printf("%d\n", val);
} else {
printf("%d\n", val -1);
}
}
}