-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprime.cpp
executable file
·49 lines (49 loc) · 1.13 KB
/
prime.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
// Sieve of Eratosthenes
// https://youtu.be/UTVg7wzMWQc?t=2774
struct Sieve {
int n;
vector<int> f, primes;
Sieve(int n=1):n(n), f(n+1) {
f[0] = f[1] = -1;
for (ll i = 2; i <= n; ++i) {
if (f[i]) continue;
primes.push_back(i);
f[i] = i;
for (ll j = i*i; j <= n; j += i) {
if (!f[j]) f[j] = i;
}
}
}
bool isPrime(int x) { return f[x] == x;}
vector<int> factorList(int x) {
vector<int> res;
while (x != 1) {
res.push_back(f[x]);
x /= f[x];
}
return res;
}
vector<P> factor(int x) {
vector<int> fl = factorList(x);
if (fl.size() == 0) return {};
vector<P> res(1, P(fl[0], 0));
for (int p : fl) {
if (res.back().first == p) {
res.back().second++;
} else {
res.emplace_back(p, 1);
}
}
return res;
}
vector<pair<ll,int>> factor(ll x) {
vector<pair<ll,int>> res;
for (int p : primes) {
int y = 0;
while (x%p == 0) x /= p, ++y;
if (y != 0) res.emplace_back(p,y);
}
if (x != 1) res.emplace_back(x,1);
return res;
}
};