-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlinear_seive.cpp
More file actions
30 lines (26 loc) · 803 Bytes
/
linear_seive.cpp
File metadata and controls
30 lines (26 loc) · 803 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
// Problem - http://acm.hdu.edu.cn/showproblem.php?pid=1695
// Soln - https://ideone.com/8a0xzV
const int MAXN = 1e5 + 10;
int M[MAXN],phi[MAXN];
int is_composite[MAXN];
vector<int> prime;
void linear_seive_with_mobius_and_phi(){
M[1] = 1,phi[1] = 1;
for(int i=2;i<MAXN;i++){
if(!is_composite[i]){
prime.push_back(i);
M[i] = -1,phi[i] = i-1;
}
for(int j=0;j<prime.size() && i*prime[j] < MAXN;j++){
is_composite[i*prime[j]] = true;
if(i%prime[j] == 0){
M[i*prime[j]] = 0;
phi[i*prime[j]] = phi[i]*prime[j];
break;
}else {
M[i*prime[j]] = M[i]*M[prime[j]];
phi[i*prime[j]] = phi[i]*phi[prime[j]];
}
}
}
}