-
Notifications
You must be signed in to change notification settings - Fork 0
/
125.cpp
69 lines (59 loc) · 1013 Bytes
/
125.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
63
64
65
66
67
68
69
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long ULL;
#define MAX_SQ 100000000
vector<ULL> squares;
void loadSquares() {
ULL i = 0;
ULL sq = 0;
while((sq = i*i) < MAX_SQ) {
squares.push_back(sq);
i++;
}
}
bool mayBeWritten(ULL a) {
int k = 0;
int i = 0;
ULL sum = 0;
while(true) {
if(sum == a && (i - k) >= 1) return true;
if(sum > a) {
if(k < i) {
sum -= squares[k];
k++;
} else return false;
} else {
if(i < squares.size()) {
i++;
sum += squares[i];
} else return false;
}
// sum = 0;
// for(int j = k; j < i; ++j) sum += squares[j];
}
return false;
}
ULL reverse(ULL a) {
ULL r = 0;
while(a > 0) {
r = 10 * r + a % 10;
a /= 10;
}
return r;
}
bool isPalindrom(ULL a) {
return a == reverse(a);
}
int main() {
loadSquares();
ULL sum = 0;
for(ULL i = 2; i < MAX_SQ; ++i) {
if(isPalindrom(i) && mayBeWritten(i)) {
cout << i << endl;
sum += i;
}
}
cout << "Result: " << sum;
return 0;
}