-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSieve.cpp
More file actions
30 lines (27 loc) · 805 Bytes
/
Sieve.cpp
File metadata and controls
30 lines (27 loc) · 805 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
#include <bits/stdc++.h>
using namespace std;
vector<int> calc_prime(int n){ // O(n log n)
vector<int> prime(n+1, 1);
for(int i=2; i<=n; i++) if(prime[i] == i)
for(int j=i+i; j<=n; j+=i)
prime[j] = false;
return prime;
}
vector<int> calc_phi(int n){ // O(n log n)
vector<int> phi(n+1);
for(int i=0; i<=n; i++) phi[i] = i;
for(int i=2; i<=n; i++) if(phi[i] == i)
for(int j=i; j<=n; j+=i)
phi[j] -= phi[j] / i;
return phi;
}
vector<int> calc_mobius(int n){ // O(n log n)
vector<int> mobius(n+1, 1), prime(n+1, 1);
for(int i=2, j; i<=n; i++) if(prime[i])
for(mobius[i]=-1, j=i+i; j<=n; j+=i){
if((j/i)%i) mobius[j] *= -1;
else mobius[j] = 0;
prime[j] = false;
}
return mobius;
}