-
Notifications
You must be signed in to change notification settings - Fork 0
/
PE061.hpp
61 lines (60 loc) · 1.89 KB
/
PE061.hpp
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
#include <bits/stdc++.h>
using namespace std;
int PE061(int num) {
int res = 0, from = pow(10, num - 1), to = pow(10, num);
vector<vector<int>> con(pow(10, num >> 1));
function<bool(int)> isTriangle = [] (int num) {
int idx = sqrt(num << 1);
return idx * (idx + 1) >> 1 == num;
};
function<bool(int)> isSquare = [] (int num) {
int idx = sqrt(num);
return idx * idx == num;
};
function<bool(int)> isPentagon = [] (int num) {
int idx = (sqrt(num * 24 + 1) + 1) / 6;
return idx * (idx * 3 - 1) >> 1 == num;
};
function<bool(int)> isHexagon = [] (int num) {
int idx = (sqrt(num * 8 + 1) + 1) / 4;
return idx * (idx * 2 - 1) == num;
};
function<bool(int)> isHeptagon = [] (int num) {
int idx = (sqrt(num * 40 + 9) + 3) / 10;
return idx * (idx * 5 - 3) >> 1 == num;
};
function<bool(int)> isOctagon = [] (int num) {
int idx = (sqrt(num * 12 + 4) + 2) / 6;
return idx * (idx * 3 - 2) == num;
};
for (int i = from; i < to; i ++) {
if (isTriangle(i) || isSquare(i) || isPentagon(i) || isHexagon(i) || isHeptagon(i) || isOctagon(i)) {
con[i / (int)pow(10, num >> 1)].push_back(i % (int)pow(10, num >> 1));
}
}
from = pow(10, (num >> 1) - 1);
to = pow(10, num >> 1);
for (int i = from; i < to; i ++) {
for (int j: con[i]) {
for (int k: con[j]) {
for (int l: con[k]) {
for (int m: con[l]) {
for (int n: con[m]) {
if (find(begin(con[n]), end(con[n]), i) != end(con[n]) && min({i, j, k, l, m, n}) == i) {
vector<int> lst = {i * to + j, j * to + k, k * to + l, l * to + m, m * to + n, n * to + i};
do {
if (isTriangle(lst[0]) + isSquare(lst[1]) + isPentagon(lst[2]) + isHexagon(lst[3]) + isHeptagon(lst[4]) + isOctagon(lst[5]) == 6) {
res += accumulate(begin(lst), end(lst), 0);
goto END;
}
} while (next_permutation(begin(lst), end(lst)));
END:;
}
}
}
}
}
}
}
return res;
}