math/linear_sieve.hpp
Verified with
Code
#ifndef LINEAR_SIEVE_HPP
#define LINEAR_SIEVE_HPP
#include <cassert>
#include <map>
#include <vector>
struct linear_sieve {
explicit linear_sieve(int n) : lpf(n + 1) {
for (auto i = 2; i <= n; ++i) {
if (lpf[i] == 0) {
lpf[i] = i;
primes.push_back(i);
}
for (auto p : primes) {
if (lpf[i] < p || n < 1LL * i * p) {
break;
}
lpf[i * p] = p;
}
}
}
std::map<int, int> factorize(int x) const { // O(log x)
assert(1 <= x && x < int(lpf.size()));
std::map<int, int> f;
while (1 < x) {
++f[lpf[x]];
x /= lpf[x];
}
return f;
}
bool is_prime(int x) const { // O(1)
assert(1 <= x && x < int(lpf.size()));
return lpf[x] == x;
}
std::vector<int> lpf;
std::vector<int> primes;
};
#endif // LINEAR_SIEVE_HPP
#line 1 "math/linear_sieve.hpp"
#include <cassert>
#include <map>
#include <vector>
struct linear_sieve {
explicit linear_sieve(int n) : lpf(n + 1) {
for (auto i = 2; i <= n; ++i) {
if (lpf[i] == 0) {
lpf[i] = i;
primes.push_back(i);
}
for (auto p : primes) {
if (lpf[i] < p || n < 1LL * i * p) {
break;
}
lpf[i * p] = p;
}
}
}
std::map<int, int> factorize(int x) const { // O(log x)
assert(1 <= x && x < int(lpf.size()));
std::map<int, int> f;
while (1 < x) {
++f[lpf[x]];
x /= lpf[x];
}
return f;
}
bool is_prime(int x) const { // O(1)
assert(1 <= x && x < int(lpf.size()));
return lpf[x] == x;
}
std::vector<int> lpf;
std::vector<int> primes;
};
Back to top page