-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path023.cpp
59 lines (45 loc) · 1.02 KB
/
023.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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef unsigned long long ULL;
vector<ULL> excessiveNumbers;
ULL sumOfDivs(ULL n) {
ULL sum = 0;
for(int i = 1; i < n; ++i) {
if(n % i == 0) {
sum += i;
}
}
return sum;
}
bool isExNumber(ULL n) {
return sumOfDivs(n) > n;
}
void loadExcessiveNumbers() {
for(int i = 12; i < 28123; ++i) {
if(isExNumber(i)) {
excessiveNumbers.push_back(i);
}
}
}
bool isMayBeWritten(int n) {
if(n < 12) return false;
for(int i = 0; i < excessiveNumbers.size(); ++i) {
if(binary_search(excessiveNumbers.begin(), excessiveNumbers.end(),
n - excessiveNumbers[i])) return true;
}
return false;
}
int main() {
loadExcessiveNumbers();
cout << "Loaded excessive numbers: " << excessiveNumbers.size() << endl;
sort(excessiveNumbers.begin(), excessiveNumbers.end());
ULL sum = 0;
for(int i = 1; i <= 28123; ++i) {
if(!isMayBeWritten(i)) sum += i;
if(i % 1000 == 0) cout << "Loaded " << i << endl;
}
cout << "Result: " << sum;
return 0;
}