forked from wandering007/ProjectEuler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP14.cpp
More file actions
53 lines (53 loc) · 1 KB
/
Copy pathP14.cpp
File metadata and controls
53 lines (53 loc) · 1 KB
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
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<math.h>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#define MAXN 1000000
#define LL long long
#define eps 1e-8
#define inf 0x3f3f3f3f
using namespace std;
int chain[1000005];
int vis[1000005]= {1,1};
int step(LL n)//n可能会超出int范围
{
if(n<=MAXN)
{
if(vis[n])
return chain[n];
}
LL k;
int res;
if(n%2)
k = 3*n+1;//这一步可能会导致k(或n)很大
else k=n/2;
res=step(k)+1;
if(n<=MAXN)
{
vis[n] = 1;
chain[n] = res;
}
return res;
}
int main()
{
int max_step,temp,rec = 1;
for(LL i=1; i<=MAXN; i++)
{
temp = step(i);
if(temp>max_step)
{
rec = i;
max_step = temp;
}
}
printf("The starting number is %d.\nThe longest chain's length is %d.\n",rec,max_step);
return 0;
}