firiexp's Library

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

View the Project on GitHub firiexp/library

:heavy_check_mark: 原始根(Primitive Root)
(math/prime/primitive_root.cpp)

説明

素数 p に対する原始根を 1 つ返す。 p - 1 の素因数を使って候補を判定する。 計算量はおおむね $O(sqrt(p) + k log p)$。

できること

使い方

p は素数を仮定する。 返る値は最小とは限らないが、mod p の乗法群を生成する。

Depends on

Verified with

Code

#include "primefactor_ll.cpp"
ll primitive_root(ll m) {
    if (m == 2) return 1;
    auto divs = prime_factor(m - 1);
    divs.erase(unique(divs.begin(), divs.end()), divs.end());
    auto mod_pow = [&](ll x, ll n) {
        ull a = x, r = 1, mod = m;
        while (n > 0) {
            if (n & 1) r = (u128)r * a % mod;
            a = (u128)a * a % mod;
            n >>= 1;
        }
        return (ll)r;
    };
    for (ll g = 2;; g++) {
        bool ok = true;
        for (auto &&d : divs) {
            if (mod_pow(g, (m - 1) / d) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}

/**
 * @brief 原始根(Primitive Root)
 */
#line 1 "math/prime/miller_rabin.cpp"
using u128 = __uint128_t;

struct mod64 {
    unsigned long long n;
    static unsigned long long mod, inv, r2;
    mod64() : n(0) {}
    mod64(unsigned long long x) : n(init(x)) {}
    static unsigned long long init(unsigned long long w) {
        return reduce(u128(w) * r2);
    }
    static void set_mod(unsigned long long m) {
        mod = inv = m;
        for (int i = 0; i < 5; ++i) inv *= 2 - inv * m;
        r2 = -u128(m) % m;
    }
    static unsigned long long reduce(u128 x) {
        unsigned long long y =
            static_cast<unsigned long long>(x >> 64)
            - static_cast<unsigned long long>((u128(static_cast<unsigned long long>(x) * inv) * mod) >> 64);
        return (long long)y < 0 ? y + mod : y;
    }
    mod64& operator*=(mod64 x) {
        n = reduce(u128(n) * x.n);
        return *this;
    }
    mod64 operator*(mod64 x) const {
        return mod64(*this) *= x;
    }
    mod64& operator+=(mod64 x) {
        n += x.n - mod;
        if((long long)n < 0) n += mod;
        return *this;
    }
    mod64 operator+(mod64 x) const {
        return mod64(*this) += x;
    }
    unsigned long long val() const {
        return reduce(n);
    }
};

unsigned long long mod64::mod, mod64::inv, mod64::r2;

bool suspect(unsigned long long a, unsigned long long s, unsigned long long d, unsigned long long n){
    if(mod64::mod != n) mod64::set_mod(n);
    mod64 x(1), xx(a), one(1), minusone(n - 1);
    while(d > 0){
        if(d & 1) x *= xx;
        xx *= xx;
        d >>= 1;
    }
    if (x.n == one.n) return true;
    for (unsigned long long r = 0; r < s; ++r) {
        if(x.n == minusone.n) return true;
        x *= x;
    }
    return false;
}

template<class T>
bool miller_rabin(T m){
    unsigned long long n = m;
    if (n <= 1 || (n > 2 && n % 2 == 0)) return false;
    if (n == 2 || n == 3 || n == 5 || n == 7) return true;
    if (n % 3 == 0 || n % 5 == 0 || n % 7 == 0) return false;
    unsigned long long d = n - 1, s = 0;
    while (!(d & 1)) { ++s; d >>= 1; }
    static constexpr unsigned long long small[] = {2, 7, 61};
    static constexpr unsigned long long large[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
    if(n < 4759123141ULL) {
        for (auto p : small) {
            if(p >= n) break;
            if(!suspect(p, s, d, n)) return false;
        }
    } else {
        for (auto p : large) {
            if(p >= n) break;
            if(!suspect(p, s, d, n)) return false;
        }
    }
    return true;
}

/**
 * @brief Miller-Rabin素数判定
 */
#line 2 "math/prime/primefactor_ll.cpp"

template<typename T>
struct ExactDiv {
    T t, i, val;
    ExactDiv() {}
    ExactDiv(T n) : t(T(-1) / n), i(mul_inv(n)) , val(n) {};
    T mul_inv(T n) {
        T x = n;
        for (int i = 0; i < 5; ++i) x *= 2 - n * x;
        return x;
    }
    bool divide(T n) const {
        if(val == 2) return !(n & 1);
        return n * this->i <= this->t;
    }
};

vector<ExactDiv<ull>> get_prime(int n){
    if(n <= 1) return vector<ExactDiv<ull>>();
    vector<bool> is_prime(n+1, true);
    vector<ExactDiv<ull>> prime;
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= n; ++i) {
        if(is_prime[i]) prime.emplace_back(i);
        for (auto &&j : prime){
            ull v = (ull)i * j.val;
            if(v > (ull)n) break;
            is_prime[v] = false;
            if(j.divide(i)) break;
        }
    }
    return prime;
}
const auto primes = get_prime(50000);

mt19937_64 rng(0x8a5cd789635d2dffULL);

template<class T>
T pollard_rho2(T n) {
    ull nn = n;
    if ((nn & 1) == 0) return 2;
    uniform_int_distribution<ull> ra(1, nn - 1);
    mod64::set_mod(nn);
    while(true){
        ull c_ = ra(rng), g = 1, r = 1, m = 500;
        while(c_ == nn - 2) c_ = ra(rng);
        mod64 y(ra(rng)), xx(0), c(c_), ys(0), q(1);
        while(g == 1){
            xx.n = y.n;
            for (ull i = 0; i < r; ++i) {
                y *= y; y += c;
            }
            ull k = 0; g = 1;
            while(k < r && g == 1){
                ull lim = min(m, r - k);
                for (ull i = 0; i < lim; ++i) {
                    ys.n = y.n;
                    y *= y; y += c;
                    ull xxx = xx.val(), yyy = y.val();
                    q *= mod64(xxx > yyy ? xxx - yyy : yyy - xxx);
                }
                g = gcd<ull>(q.val(), nn);
                k += m;
            }
            r *= 2;
        }
        if(g == nn) g = 1;
        while (g == 1){
            ys *= ys; ys += c;
            ull xxx = xx.val(), yyy = ys.val();
            g = gcd<ull>(xxx > yyy ? xxx - yyy : yyy - xxx, nn);
        }
        if (g != nn && miller_rabin(g)) return (T)g;
    }
}

template<class T>
void prime_factor_impl(T n, vector<T> &res, bool trial){
    if(trial) {
        for (auto &&i : primes) {
            while (i.divide(n)){
                res.emplace_back(i.val);
                n /= i.val;
            }
        }
    }
    if(n == 1) return;
    if(miller_rabin(n)) {
        res.emplace_back(n);
        return;
    }
    T x = pollard_rho2(n);
    prime_factor_impl(x, res, false);
    prime_factor_impl(n / x, res, false);
}

template<class T>
vector<T> prime_factor(T n){
    vector<T> res;
    prime_factor_impl(n, res, true);
    sort(res.begin(),res.end());
    return res;
}

/**
 * @brief 素因数分解(Pollard Rho)
 */
#line 2 "math/prime/primitive_root.cpp"
ll primitive_root(ll m) {
    if (m == 2) return 1;
    auto divs = prime_factor(m - 1);
    divs.erase(unique(divs.begin(), divs.end()), divs.end());
    auto mod_pow = [&](ll x, ll n) {
        ull a = x, r = 1, mod = m;
        while (n > 0) {
            if (n & 1) r = (u128)r * a % mod;
            a = (u128)a * a % mod;
            n >>= 1;
        }
        return (ll)r;
    };
    for (ll g = 2;; g++) {
        bool ok = true;
        for (auto &&d : divs) {
            if (mod_pow(g, (m - 1) / d) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}

/**
 * @brief 原始根(Primitive Root)
 */
Back to top page