-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler totient.cpp
More file actions
51 lines (43 loc) · 1.15 KB
/
Euler totient.cpp
File metadata and controls
51 lines (43 loc) · 1.15 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
// Euler's totient functino (phi[n]) as well as Pillai's arithmetical function ( F[n] )
/*
Pillai's arithmetical function: https://en.wikipedia.org/wiki/Pillai%27s_arithmetical_function
Problem: https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/akash-and-gcd-1-15/description/
*/
const int MAXE=1e5+5;
const int N=1e6+5;
int phi[MAXE],F[MAXE];
void compute()
{
int i, j, k, ans;
for(i = 0;i < MAXE;++i)
phi[i] = i;
for(i = 2;i < MAXE;++i)
{
if(phi[i] == i)
{
phi[i] = i - 1;
for(j = 2*i;j < MAXE;j += i)
phi[j] -= (phi[j] / i);
}
}
for(i = 1;i < MAXE;++i)
{
for(j = i, k = 1;j < MAXE;j += i, k++)
{
F[j] += i*phi[k];
}
}
}
//Computing Euler's totient function for all natural numbers <= N in O(N*log N):
void euler_totient()
{
for (int i = 1; i <= N; ++i)
{
phi[i] += i;
for (int j = 2 * i; j <= N; j += i)
{
phi[j] -= phi[i];
}
}
}
//in linear time using linear sieve: https://codeforces.com/blog/entry/10119