-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum of Divisors.cpp
More file actions
65 lines (52 loc) · 1.01 KB
/
Sum of Divisors.cpp
File metadata and controls
65 lines (52 loc) · 1.01 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
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
#define MAX 10000000
ull spf[MAX+5];
void sieve(ull n)
{
ull lim = sqrt(n+1);
spf[1] = 1;
for(ull i = 2; i <= n; i += 2)
spf[i] = 2;
for(ull i = 3; i <= n; i += 2)
{
if(!spf[i])
{
spf[i] = i;
if(i <= lim)
for(ull j = i*i; j <= n; j += i*2)
spf[j] = i;
}
}
}
map<ull, int> getFactorization(ull x)
{
sieve(x);
map<ull, int> ret;
while(x != 1)
{
++ret[ spf[x] ];
x = x / spf[x];
}
return ret;
}
ull sumOfDivisors(ull n)
{
if(n == 0)
return 0;
ull sum = 1, cnt, factor;
map<ull, int> factors = getFactorization(n);
for(auto it : factors)
{
factor = it.first;
cnt = it.second + 1;
sum *= ( ((ull) pow(factor, cnt)) - 1 ) / (factor - 1);
}
return sum;
}
int main()
{
cout << sumOfDivisors(MAX) << endl;
return 0;
}