-
Notifications
You must be signed in to change notification settings - Fork 0
/
#021:Amicable_numbers.cpp
75 lines (67 loc) · 1.77 KB
/
#021:Amicable_numbers.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
70
71
72
73
74
75
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull ;
ull sumOfDivisors(int n)
{
int i = 2;
ull sum = 1;
for (; i < sqrt(n); i++)
{
if (!(n % i))
sum += i + n / i;
}
if (!(n % i))
sum += i;
return (sum);
}
int main() {
int q, n, j, k;
ull sum = 0, x[2];
vector<int> amicables, wait_list, v, v_sort, res;
cin >> q;
for (int i = 0; i < q; i++)
{
cin >> n;
v.push_back(n);
}
v_sort = v;
sort(v_sort.begin(), v_sort.end());
j = 220;
for (int i = 0; i < q; i++)
{
n = v_sort[i];
for (; j < n; j++)
{
sort (wait_list.begin(), wait_list.end());
for (k = 0; k < wait_list.size() && wait_list[k] < n; k++) {
sum += wait_list[k];
amicables.push_back(wait_list[k]);
}
if (k)
wait_list.erase(wait_list.begin(), wait_list.begin() + k);
if (find(amicables.begin(), amicables.end(), j) == amicables.end())
{
x[0] = sumOfDivisors(j);
x[1] = sumOfDivisors(x[0]);
if ((x[0] != x[1]) && (x[1] == j))
{
sum += x[1];
if (x[0] < n)
{
sum += x[0];
amicables.push_back(x[0]);
}
else
wait_list.push_back(x[0]);
}
}
}
res.push_back(sum);
}
for (int i = 0; i < q; i++)
cout << res[distance(v_sort.begin(), find(v_sort.begin(), v_sort.end(), v[i]))] << endl;
return 0;
}