forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path100.cpp
62 lines (56 loc) · 959 Bytes
/
100.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
#include <bits/stdc++.h>
using namespace std;
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define SIZE 1000001
static unsigned short tbl[SIZE];
inline unsigned short cycle_len(register unsigned long n)
{
if (n < SIZE && tbl[n])
{
return tbl[n];
}
if (n & 1)
{
// odd n
if (n < SIZE)
{
// calc two steps at once
tbl[n] = 2 + cycle_len((3 * n + 1) >> 1);
return tbl[n];
}
else
{
return 2 + cycle_len((3 * n + 1) >> 1);
}
}
else
{
if (n < SIZE)
{
tbl[n] = 1 + cycle_len(n >> 1);// n/2
return tbl[n];
}
else
{
return 1 + cycle_len(n >> 1);
}
}
}
int main()
{
unsigned long fn = 0, sn = 0, i;
short out = 0, temp;
tbl[1] = 1;
while (scanf("%lu%lu", &fn, &sn) == 2)
{
unsigned long a = min(fn, sn), b = max(fn, sn);
for (int i = a; i <= b; i++)
{
out = max(out, cycle_len(i));
}
printf("%lu %lu %d\n", fn, sn, out);
out = 0;
}
return 0;
}