a01sa01to's competitive programming library. Requires C++20 or higher with GCC. This documentation is automatically generated by online-judge-tools/verification-helper
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;
#include "../../../library/fastio.hpp"
#include "../../../library/math/enum-primes.hpp"
#define PROBLEM "https://judge.yosupo.jp/problem/enumerate_primes"
int main() {
int n, a, b;
asalib::FastIO >> n >> a >> b;
vector<int> primes = asalib::math::enum_primes(n);
vector<int> ans;
for (int i = b; i < primes.size(); i += a) ans.push_back(primes[i]);
asalib::FastIO << primes.size() << ' ' << ans.size() << '\n';
rep(i, ans.size()) asalib::FastIO << ans[i] << " \n"[i + 1 == ans.size()];
return 0;
}
#line 1 "tests/math/enum-primes/libchecker.test.cpp"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;
#line 2 "library/fastio.hpp"
#line 5 "library/fastio.hpp"
#include <concepts>
#line 11 "library/fastio.hpp"
#include <type_traits>
using namespace std;
// TODO: 何も入力がない場合にも対応する?
namespace asalib {
namespace _internal {
class FastIO {
private:
using uint = unsigned int;
static constexpr uint BUFFER_SIZE = 1 << 20;
static constexpr uint MAX_TOKEN_SIZE = 64;
// ===== Read ===== //
private:
array<char, BUFFER_SIZE> read_buffer;
array<char, BUFFER_SIZE>::iterator read_ptr;
uint read_size = 0;
inline void load() {
// まだ読んでないデータを前に持ってくる
memcpy(read_buffer.begin(), read_ptr, read_size - (read_ptr - read_buffer.begin()));
read_size -= read_ptr - read_buffer.begin();
read_ptr = read_buffer.begin();
// stdin から読み込み
read_size += fread(read_buffer.begin() + read_size, 1, BUFFER_SIZE - read_size, stdin);
}
inline void skip_space() {
// 制御文字 + space
// DEL (127 = 0x7F) がコーナーケースだがまあ使わんやろ
while (*read_ptr <= ' ') ++read_ptr;
}
template<typename T>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
inline void _read_uint(T& x) {
x = 0;
while (true) {
uint64_t v;
memcpy(&v, read_ptr, 8);
// '0' -> 0 処理
// こっちを先にやることで制御文字系もはじく
v -= 0x30'30'30'30'30'30'30'30;
// ASCII 範囲外
if (v & 0x80'80'80'80'80'80'80'80) break;
// 桁ごとの数字から数値に
v = (v * 10 + (v >> 8)) & 0x00ff00ff00ff00ff;
v = (v * 100 + (v >> 16)) & 0x0000ffff0000ffff;
v = (v * 10000 + (v >> 32)) & 0x00000000ffffffff;
x = 1'0000'0000 * x + v;
read_ptr += 8;
}
while (true) {
uint32_t v;
memcpy(&v, read_ptr, 4);
v -= 0x30'30'30'30;
if (v & 0x80'80'80'80) break;
v = (v * 10 + (v >> 8)) & 0x00ff00ff;
v = (v * 100 + (v >> 16)) & 0x0000ffff;
x = 1'0000 * x + v;
read_ptr += 4;
}
while (true) {
uint16_t v;
memcpy(&v, read_ptr, 2);
v -= 0x3030;
if (v & 0x8080) break;
v = (v * 10 + (v >> 8)) & 0x00ff;
x = 100 * x + v;
read_ptr += 2;
}
if (*read_ptr > ' ') x = 10 * x + (*read_ptr++ & 0x0f);
}
inline void read_commonop() {
// そろそろ限界なら読み込み
if ((read_ptr - read_buffer.begin()) + MAX_TOKEN_SIZE >= BUFFER_SIZE) load();
skip_space();
}
public:
FastIO& operator>>(char& x) {
read_commonop();
x = *read_ptr++;
return *this;
}
FastIO& operator>>(string& x) {
read_commonop();
x.clear();
while (*read_ptr > ' ') {
x += *read_ptr++;
if (read_ptr == read_buffer.end()) load();
}
return *this;
}
template<typename T>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
FastIO& operator>>(T& x) {
read_commonop();
constexpr bool is_signed = is_signed_v<T> || is_same_v<T, __int128_t>;
if constexpr (is_signed) {
if (*read_ptr == '-') {
++read_ptr;
_read_uint(x);
x = -x;
return *this;
}
}
_read_uint(x);
return *this;
}
// ===== Write ===== //
private:
array<char, BUFFER_SIZE> write_buffer;
array<char, BUFFER_SIZE>::iterator write_ptr;
static constexpr uint MAX_PRECOMPUTE_NUM = 1'0000;
static constexpr uint MAX_PRECOMPUTE_NUM_DIGIT = 4;
static constexpr array<char, MAX_PRECOMPUTE_NUM * MAX_PRECOMPUTE_NUM_DIGIT> digits = [] {
array<char, MAX_PRECOMPUTE_NUM * MAX_PRECOMPUTE_NUM_DIGIT> digits {};
for (uint i = 0; i < MAX_PRECOMPUTE_NUM; ++i) {
uint x = i;
for (int j = MAX_PRECOMPUTE_NUM_DIGIT - 1; j >= 0; --j) {
digits[i * MAX_PRECOMPUTE_NUM_DIGIT + j] = '0' + (x % 10);
x /= 10;
}
}
return digits;
}();
template<typename T>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
static constexpr array<T, numeric_limits<T>::digits10 + 1> Pow10 = [] {
array<T, numeric_limits<T>::digits10 + 1> pow10 {};
pow10[0] = 1;
for (uint i = 1; i < pow10.size(); ++i) pow10[i] = pow10[i - 1] * 10;
return pow10;
}();
inline void write_commonop() {
// そろそろ限界なら書き込む
if ((write_ptr - write_buffer.begin()) + MAX_TOKEN_SIZE >= BUFFER_SIZE) flush();
}
inline void putchar(const char& x) { *write_ptr++ = x; }
template<typename T, int NumDig>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
inline void _write_uint_top(const T& x) {
// leading-zero を書き込まないようにする
if constexpr (NumDig > 1) {
if (x < Pow10<T>[NumDig - 1]) {
_write_uint_top<T, NumDig - 1>(x);
return;
}
}
copy_n(digits.begin() + (x + 1) * MAX_PRECOMPUTE_NUM_DIGIT - NumDig, NumDig, write_ptr);
write_ptr += NumDig;
}
template<typename T, int NumDig>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
inline void _write_uint(const T& x) {
if constexpr (NumDig >= 0) {
if constexpr (NumDig > MAX_PRECOMPUTE_NUM_DIGIT) _write_uint<T, NumDig - MAX_PRECOMPUTE_NUM_DIGIT>(x / MAX_PRECOMPUTE_NUM);
copy_n(digits.begin() + x % MAX_PRECOMPUTE_NUM * MAX_PRECOMPUTE_NUM_DIGIT, MAX_PRECOMPUTE_NUM_DIGIT, write_ptr);
write_ptr += MAX_PRECOMPUTE_NUM_DIGIT;
}
}
template<typename T, int NumDig>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
inline void _write_uint_root(const T& x) {
if constexpr (is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>) {
// 128bit 除算はおそいので 64bit 除算を使う
if (x < Pow10<T>[16]) {
_write_uint_root<uint64_t, NumDig>(x);
}
else if (x < Pow10<T>[32]) {
_write_uint_root<uint64_t, NumDig>(x / Pow10<T>[16]);
_write_uint<uint64_t, 16>(x % Pow10<T>[16]);
}
else {
_write_uint_root<uint64_t, NumDig>(x / Pow10<T>[32]);
_write_uint<uint64_t, 16>(x % Pow10<T>[32] / Pow10<T>[16]);
_write_uint<uint64_t, 16>(x % Pow10<T>[16]);
}
return;
}
if constexpr (NumDig < numeric_limits<T>::digits10) {
if (x >= Pow10<T>[NumDig]) {
_write_uint_root<T, NumDig + MAX_PRECOMPUTE_NUM_DIGIT>(x);
return;
}
}
_write_uint_top<T, MAX_PRECOMPUTE_NUM_DIGIT>(x / Pow10<T>[NumDig - MAX_PRECOMPUTE_NUM_DIGIT]);
if constexpr (NumDig > MAX_PRECOMPUTE_NUM_DIGIT) _write_uint<T, NumDig - MAX_PRECOMPUTE_NUM_DIGIT>(x % Pow10<T>[NumDig - MAX_PRECOMPUTE_NUM_DIGIT]);
}
public:
inline void flush() {
fwrite(write_buffer.begin(), 1, write_ptr - write_buffer.begin(), stdout);
write_ptr = write_buffer.begin();
}
FastIO& operator<<(const char& x) {
write_commonop();
putchar(x);
return *this;
}
FastIO& operator<<(const string& x) {
write_commonop();
uint idx = 0;
while (idx < x.size()) {
const uint siz = min(BUFFER_SIZE - (uint) (write_ptr - write_buffer.begin()), (uint) x.size() - idx);
copy_n(x.begin() + idx, siz, write_ptr);
write_ptr += siz;
idx += siz;
if (write_ptr == write_buffer.end()) flush();
}
return *this;
}
template<typename T>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
FastIO& operator<<(const T& x) {
write_commonop();
constexpr bool is_signed = is_signed_v<T> || is_same_v<T, __int128_t>;
if constexpr (is_signed) {
if (x < 0) {
putchar('-');
_write_uint_root<T, MAX_PRECOMPUTE_NUM_DIGIT>(-x);
return *this;
}
}
_write_uint_root<T, MAX_PRECOMPUTE_NUM_DIGIT>(x);
return *this;
}
// ===== Common ====== //
public:
FastIO() {
read_ptr = read_buffer.begin();
write_ptr = write_buffer.begin();
load();
}
~FastIO() { flush(); }
};
} // namespace _internal
_internal::FastIO FastIO;
} // namespace asalib
#line 2 "library/math/enum-primes.hpp"
#line 4 "library/math/enum-primes.hpp"
#include <bit>
#line 7 "library/math/enum-primes.hpp"
using namespace std;
namespace asalib {
namespace math {
template<integral T>
constexpr vector<T> enum_primes(T n) {
using u8 = char;
static_assert(sizeof(u8) == 1, "u8 is not 1 byte");
// 2, 3, 5 の倍数を除外する -> LCM=30 なので余りの候補: 1, 7, 11, 13, 17, 19, 23, 29
constexpr array<u8, 8> rem = { 1, 7, 11, 13, 17, 19, 23, 29 };
// c0[i] = rem[i + 1] - rem[i]
constexpr array<u8, 8> c0 = { 6, 4, 2, 4, 2, 4, 6, 2 };
// c1[i][j] = (rem[i] * rem[j + 1] / 30) - (rem[i] * rem[j] / 30)
constexpr array<array<u8, 8>, 8> c1 = { {
{ 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 0, 1, 1, 1, 1 },
{ 2, 2, 0, 2, 0, 2, 2, 1 },
{ 3, 1, 1, 2, 1, 1, 3, 1 },
{ 3, 3, 1, 2, 1, 3, 3, 1 },
{ 4, 2, 2, 2, 2, 2, 4, 1 },
{ 5, 3, 1, 4, 1, 3, 5, 1 },
{ 6, 4, 2, 4, 2, 4, 6, 1 },
} };
// rr2idx[i][j] = rem[i] * rem[j] % 30 のインデックス
constexpr array<array<u8, 8>, 8> rr2idx = { {
{ 0, 1, 2, 3, 4, 5, 6, 7 },
{ 1, 5, 4, 0, 7, 3, 2, 6 },
{ 2, 4, 0, 6, 1, 7, 3, 5 },
{ 3, 0, 6, 5, 2, 1, 7, 4 },
{ 4, 7, 1, 2, 5, 6, 0, 3 },
{ 5, 3, 7, 1, 6, 0, 4, 2 },
{ 6, 2, 3, 7, 0, 4, 5, 1 },
{ 7, 6, 5, 4, 3, 2, 1, 0 },
} };
vector<T> primes = { 2, 3, 5 };
if (n <= 5) [[unlikely]] {
while (!primes.empty() && primes.back() > n) primes.pop_back();
return primes;
}
const T siz = n / 30 - (n % 30 == 0);
vector<u8> is_prime(siz + 1, 0b11111111);
if (n % 30) {
is_prime.back() = 0;
rep(i, 8) {
if (n % 30 >= rem[i]) is_prime.back() |= 1 << i;
}
}
is_prime[0] &= 0b11111110; // 1 は素数ではない
for (T i = 0; i <= siz; ++i) {
for (u8 bit = is_prime[i]; bit;) {
const u8 idx = countr_zero(unsigned(bit));
bit ^= 1 << idx;
const T p = 30 * i + rem[idx];
primes.push_back(p);
if (p > n / p) continue; // p * p > n
// https://qiita.com/peria/items/54499b9ce9d5c1e93e5a
T i2 = i * (30 * i + 2 * rem[idx]) + rem[idx] * rem[idx] / 30;
u8 idx2 = idx;
for (; i2 <= siz;) {
is_prime[i2] &= ~(1 << rr2idx[idx][idx2]);
i2 += i * c0[idx2] + c1[idx][idx2];
idx2 = (idx2 + 1) & 7; // = (idx2 + 1) % 8
}
}
}
return primes;
}
} // namespace math
} // namespace asalib
#line 9 "tests/math/enum-primes/libchecker.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/enumerate_primes"
int main() {
int n, a, b;
asalib::FastIO >> n >> a >> b;
vector<int> primes = asalib::math::enum_primes(n);
vector<int> ans;
for (int i = b; i < primes.size(); i += a) ans.push_back(primes[i]);
asalib::FastIO << primes.size() << ' ' << ans.size() << '\n';
rep(i, ans.size()) asalib::FastIO << ans[i] << " \n"[i + 1 == ans.size()];
return 0;
}