-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sum of divisors.cpp
68 lines (65 loc) · 1.52 KB
/
Sum of divisors.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
#define int long long
#define MAX 100000001
char stat[100000001];
vector<long long >prime;
template <typename T>
T modpow(T base, T exp, T modulus) {
base %= modulus;
T result = 1;
while (exp > 0) {
if (exp & 1) result = (result * base) % modulus;
base = (base * base) % modulus;
exp >>= 1;
}
return result;
}
/*------bitwise sieve-----*/
void generate () {
long long sqrtn = sqrt ( MAX );
long long i, j;
prime.push_back ( 2 );
for ( i = 3; i <= sqrtn; i += 2 ) {
if ( !( stat[i/8] & ( 1 << ( i % 8 ) ) ) ) {
for ( j = i * i; j <= MAX; j += 2 * i ) {
stat[j/8] |= ( 1 << (j%8) );
}
}
}
for ( i = 3; i <= MAX; i += 2 ) {
if ( !( stat[i/8] & ( 1 << ( i % 8 ) ) ) )
{
prime.push_back(i);
}
}
}
/*----sum of divisors-----*/
vector<pair<ll,ll> >div_count;
long long SOD(long long n) {
ll root=sqrt(n);
ll ans=1;
for(ll i=0; i<(ll)prime.size() && prime[i]<=root; i++)
{
if(n%prime[i]==0)
{
ll cnt=0;
while(n%prime[i]==0) {
cnt++;
n/=prime[i];
}
div_count.push_back({prime[i],cnt});
root=sqrt(n);
}
}
if(n>1)
{
div_count.push_back({n,1LL});
}
for(ll i=0;i<div_count.size();i++)
{
ll j=modpow(div_count[i].first,div_count[i].second+1LL,100000000000000000LL);
j--;
j=j/(div_count[i].first-1LL);
ans*=j;
}
return ans;
}