-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sieve.cpp
38 lines (36 loc) · 1004 Bytes
/
Sieve.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
void EratosthenesSieve(const int n, vector <ll> &primes) {
vector <ll> numbers(n + 1);
for (ll i = 2; i <= n; i++) {
if (!numbers[i]) {
primes.PB(i);
for (ll j = i * i; j <= n; j += i)numbers[j] = 1;
}
}
}
void EulerSieve(const ll n, vector <ll> &primes, vector <ll> &phi, vector <ll> &mu) {
phi = vector<ll>(n + 1);
phi[1] = 1;
mu = vector<ll>(n + 1);
mu[1] = 1;
vector <ll> isPrime(n + 1, 1);
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
primes.PB(i);
phi[i] = i - 1;
mu[i] = -1;
}
for (const auto &j: primes) {
ll num = 1LL * j * i;
if (num > n)break;
isPrime[num] = 0;
if (i % j == 0) {
phi[num] = phi[i] * j;
mu[num] = 0;
break;
} else {
phi[num] = phi[i] * (j - 1);
mu[num] = -mu[i];
}
}
}
}