-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlinear_div_sum.cpp
More file actions
50 lines (39 loc) · 884 Bytes
/
linear_div_sum.cpp
File metadata and controls
50 lines (39 loc) · 884 Bytes
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
#include <bits/stdc++.h>
using namespace std;
/*
Build: O(n)
*/
#define ll long long
const int maxn = 1e7;
ll dsum[maxn+123];
ll lp[maxn+123];
void sieve(){
for(int i = 1;i <= maxn;i++) {
dsum[i] = i+1;
lp[i] = i;
}
dsum[1] = 1;
vector<ll>primes;
for(int i = 2;i <= maxn;i++){
if (dsum[i] == i+1) primes.push_back(i);
for(ll p:primes){
if (i*p > maxn) break;
if (i % p == 0){
ll n = i/lp[i];
lp[i*p] = lp[i]*p;
// use long long to avoid overflow here
dsum[i*p] = dsum[n]*(lp[i*p]*p-1)/(p-1);
break;
}
else {
dsum[i*p] = dsum[i]*dsum[p];
lp[i*p] = p;
}
}
}
}
int main(){
sieve();
cout << dsum[21613] << endl;
return 0;
}