forked from wandering007/ProjectEuler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP62.cpp
More file actions
134 lines (128 loc) · 2.42 KB
/
P62.cpp
File metadata and controls
134 lines (128 loc) · 2.42 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
#include<fstream>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<algorithm>
#include<math.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iomanip>
#define MAXN 100000
#define BASE 15
#define LL long long
#define eps 1e-6
#define inf 0x3f3f3f3f
using namespace std;
int digit[16];
typedef struct
{
LL val;
int id;
}node;
node rec[MAXN];
LL cube(int n)
{
return (LL)n * n * n;
}
void ToString(LL n)
{
while(n)
{
digit[++digit[0]] = n % 10;
n /= 10;
}
return;
}
LL ToInt()
{
LL res = 0LL;
while(digit[0])
{
res = res * 10 + digit[digit[0]];
digit[0]--;
}
return res;
}
void Append(int f)//f对应的结果放入rec[f]
{
LL n = cube(f);
ToString(n);
sort(digit + 1, digit + digit[0] + 1);//排序化成最大值,回避出现最高位为零的情况,作为标志很完美
LL res = ToInt();
rec[f].val = res;
rec[f].id = f;
return;
}
//another implementation
/*
LL Weight(int k)//BASE^k
{
LL res = 1LL;
while(k--)
{
res *= BASE;
}
return res;
}
void Append(int f)//f对应的结果放入rec[f]
{
LL n = cube(f);
memset(digit, 0, sizeof(digit));
while(n)
{
digit[n % 10]++;
n /= 10;
}
LL res = 0LL;
for(int i = 0; i <= 9; i++)
res += digit[i] * Weight(i);//转化成15进制的十进制表示
rec[f].val = res;
rec[f].id = f;
return;
}
*/
bool cmp(node a, node b)
{
if(a.val < b.val)
return true;
if(a.val == b.val)
return a.id < b.id;
return false;
}
int main()
{
double duration;
clock_t start = clock();
for(int i = 1; i < MAXN; i++)
Append(i);
sort(rec + 1, rec + MAXN, cmp);
int cnt, Min = MAXN;
for(int i = 1; i < MAXN; i++)
{
cnt = 1;
while(i + 1 < MAXN && rec[i].val == rec[i + 1].val)
{
cnt++;
i++;
}
if(5 == cnt)
{
if(rec[i - 4].id < Min)
{
Min = rec[i - 4].id;
}
}
}
printf("%d ^ 3 = %I64d\n", Min, cube(Min));
duration = (double)(clock() - start) / CLOCKS_PER_SEC;
printf("time cost: %lf\n", duration);
return 0;
}