proconlib

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub KodamaD/proconlib

:heavy_check_mark: math/prime_sieve.cpp

Depends on

Verified with

Code

#pragma once
#include <cassert>
#include <numeric>
#include <utility>
#include <vector>
#include "../utility/auto_realloc.cpp"
#include "../utility/rep.cpp"

struct PrimeSieve {
    static inline const auto min_prime = auto_realloc([](const int n) {
        std::vector<int> ret(n);
        std::iota(ret.begin(), ret.end(), (int)0);
        std::vector<int> list;
        for (const int i : rep(2, n)) {
            if (ret[i] == i) list.push_back(i);
            for (const int p : list) {
                if (p * i >= n || p > ret[i]) break;
                ret[p * i] = p;
            }
        }
        return ret;
    });
    static bool is_prime(const int n) {
        if (n <= 1) return false;
        return min_prime[n] == n;
    }
    template <class T> static std::vector<std::pair<T, int>> factorize(T x) {
        assert(x > 0);
        std::vector<std::pair<T, int>> ret;
        while (x != 1) {
            const int p = min_prime[x];
            ret.emplace_back((T)p, 0);
            while (min_prime[x] == p) {
                ret.back().second++;
                x /= p;
            }
        }
        return ret;
    }
};
#line 2 "math/prime_sieve.cpp"
#include <cassert>
#include <numeric>
#include <utility>
#include <vector>
#line 2 "internal/enable_avx2.cpp"

#ifdef ENABLE_AVX2
#define TARGET_AVX2 __attribute__((target("avx2")))
#else
#define TARGET_AVX2
#endif
#line 2 "utility/int_alias.cpp"
#include <cstdint>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
#line 4 "utility/countl_zero.cpp"

TARGET_AVX2 constexpr int countl_zero(u64 x) {
#ifdef __GNUC__
    return x == 0 ? 64 : __builtin_clzll(x);
#else
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    x |= x >> 32;
    return 64 - countr_zero(~x);
#endif
}
#line 4 "utility/bit_width.cpp"

TARGET_AVX2 constexpr int bit_width(const u64 x) { return 64 - countl_zero(x); }
#line 5 "utility/ceil_log2.cpp"

TARGET_AVX2 constexpr int ceil_log2(const u64 x) {
#ifdef __GNUC__
    return x == 0 ? 0 : bit_width(x - 1);
#else
    int e = 0;
    while (((u64)1 << e) < x) ++e;
    return e;
#endif
}
#line 6 "utility/auto_realloc.cpp"

template <class F> class AutoReallocation {
    using R = typename decltype(std::declval<F>()(0))::value_type;

    F func;
    mutable std::vector<R> data;

  public:
    explicit AutoReallocation(F&& f) : func(std::forward<F>(f)), data() {}

    void reserve(const int size) const {
        if ((int)data.size() < size) data = func(((int)1 << ceil_log2(size)));
    }
    R operator[](const int i) const {
        assert(i >= 0);
        reserve(i + 1);
        return data[i];
    }
};

template <class F> decltype(auto) auto_realloc(F&& f) { return AutoReallocation<F>(std::forward<F>(f)); }
#line 2 "utility/rep.cpp"
#include <algorithm>

class Range {
    struct Iter {
        int itr;
        constexpr Iter(const int pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr int operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr Range(const int first, const int last) noexcept : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

constexpr Range rep(const int l, const int r) noexcept { return Range(l, r); }
constexpr Range rep(const int n) noexcept { return Range(0, n); }
#line 8 "math/prime_sieve.cpp"

struct PrimeSieve {
    static inline const auto min_prime = auto_realloc([](const int n) {
        std::vector<int> ret(n);
        std::iota(ret.begin(), ret.end(), (int)0);
        std::vector<int> list;
        for (const int i : rep(2, n)) {
            if (ret[i] == i) list.push_back(i);
            for (const int p : list) {
                if (p * i >= n || p > ret[i]) break;
                ret[p * i] = p;
            }
        }
        return ret;
    });
    static bool is_prime(const int n) {
        if (n <= 1) return false;
        return min_prime[n] == n;
    }
    template <class T> static std::vector<std::pair<T, int>> factorize(T x) {
        assert(x > 0);
        std::vector<std::pair<T, int>> ret;
        while (x != 1) {
            const int p = min_prime[x];
            ret.emplace_back((T)p, 0);
            while (min_prime[x] == p) {
                ret.back().second++;
                x /= p;
            }
        }
        return ret;
    }
};
Back to top page