-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phi(Euler Totient).cpp
57 lines (51 loc) · 1.18 KB
/
Phi(Euler Totient).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
//Use one of these, run time is almost same
int phi[N];
void Totient(int n){
CLR(phi);
int i, j, x;
for (i = 1; i <= n; i++){
phi[i] += i;
for (j = (i << 1); j <= n; j += i){
phi[j] -= phi[i];
}
}
}
void Totient(int n){
CLR(phi);
int i, j, x;
phi[1] = 1;
for (i = 2; i <= n; i++){
if (!phi[i]){
phi[i] = i - 1;
for (j = (i << 1); j <= n; j += i){
x = phi[j];
if (!x) x = j;
x = (x / i) * (i - 1);
phi[j] = x;
}
}
}
}
int phi[N];
short P[N] = {0};
void Generate(int n){
int i, j, k, d, x;
P[0] = P[1] = 1;
for (i = 4; i <= n; i++, i++) P[i] = 2;
for (i = 3; i * i <= n;){
d = i << 1;
for (j = (i * i); j <= n; j += d) P[j] = i;
do{
i++;
} while (P[++i]);
}
phi[1] = 1;
for (i = 2; i <= n; i++){
if (!P[i]) phi[i] = i - 1;
else{
k = i / P[i];
if (!(k % P[i])) phi[i] = phi[k] * P[i];
else phi[i] = phi[k] * (P[i] - 1);
}
}
}