Library

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

View the Project on GitHub Kuro-orzz/Library

:heavy_check_mark: NumberTheory/Math/Primitive_root.h

Depends on

Verified with

Code

#include "../../template.h"

#include "Pollard_rho.h"


// smallest primitive root of a PRIME p, that is g with order exactly p-1

// g works iff g^((p-1)/q) != 1 for every distinct prime q of p-1

// O(n^(1/4)) to factor p-1, then a few binPow per candidate

ll primitiveRoot(ll p) {
    if (p == 2) return 1;
    vector<ll> q = primeFactorFast(p - 1);
    unique(q);
    for (ll g = 2; ; g++) {
        bool ok = true;
        for (ll x : q) {
            if (binPow(g, (p-1) / x, p) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
#line 2 "template.h"

#include <bits/stdc++.h>

using namespace std;
 
#define ll long long
#define MOD (ll)(1e9+7)
#define all(x) (x).begin(),(x).end()
#define unique(x) x.erase(unique(all(x)), x.end())
#define INF32 ((1ull<<31)-1)
#define INF64 ((1ull<<63)-1)
#define inf (ll)1e18

#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll r) { return uniform_int_distribution<ll>(0, r - 1)(rng); }

const int mod = 998244353;

void solve();

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    // cin.exceptions(cin.failbit);

    // int t; cin >> t;

    // while(t--)

        solve();
    cerr << "\nTime run: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << '\n';
    return 0;
}
#line 2 "NumberTheory/Math/Binary_exponentiation.h"

using u128 = __uint128_t;
using i128 = __int128;

ll binMul(ll a, ll b, ll M) { return (i128)a * b % M; }

// long double trick

// require: mantissa 64 bit, x86 gcc/clang

ll binMul2(ll a, ll b, ll M) {
    ll q = (ll)((long double)a * b / M);
    ll r = (ll)((unsigned ll)a * b - (unsigned ll)q * M);
    return r < 0 ? r + M : (r >= M ? r - M : r);
}

ll binMul3(ll a, ll b, ll M) {
    unsigned long long ua = a % M, um = M, res = 0;
    while (b) {
        if (b & 1) { res += ua; if (res >= um) res -= um; }
        ua <<= 1; if (ua >= um) ua -= um;
        b >>= 1;
    }
    return res;
}

ll binPow(ll a, ll b, ll M) {
    a %= M;
    ll res = 1 % M;
    while (b) {
        if (b & 1) res = (i128)res * a % M;
        a = (i128)a * a % M;
        b /= 2;
    }
    return res;
}
#line 3 "NumberTheory/Math/MillerRabin.h"


bool millerTest(ll a, ll n, ll k, ll m) {
    ll mod = binPow(a, m, n);
    if (mod == 1 || mod == n - 1) return true;
    for (int l = 1; l < k; l++) {
        mod = (u128)mod * mod % n;
        if (mod == n - 1) return true;
    }
    return false;
}

// Miller rabin

bool MillerRabin0(ll n) {
    if (n < 4) return n == 2 || n == 3;
    if (n % 2 == 0) return false;
    ll k = 0, m = n - 1;
    while(m % 2 == 0) {
        m /= 2;
        k++;
    }
    for (int i = 0; i < 5; i++) {
        ll a = get_rand(n-3) + 2;
        if (!millerTest(a, n, k, m)) return false;
    }
    return true;
}

// Miller Rabin deterministic version

bool MillerRabin(ll n) {
    if (n <= 1) return false;
    ll k = 0, m = n-1;
    while (m % 2 == 0) {
        m /= 2;
        k++;
    }
    for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
        if (n == a) return true;
        if (!millerTest(a, n, k, m)) return false;
    }
    return true;
}
#line 3 "NumberTheory/Math/Pollard_rho.h"



// https://wiki.vnoi.info/algo/math/integer-factorization

// pollard's rho, brent variant

// the polynomial and the start point MUST be random, a fixed one can gets hacked

// return one non trivial divisor, require n composite

// expected O(n^(1/4))

ll pollardRho(ll n) {
    if (n % 2 == 0) return 2;
    while (true) {
        ll c = 1 + get_rand(n-1), x = get_rand(n), prod = 2, step = 30;
        auto f = [&](ll v) { return (ll)(((i128)v * v + c) % n); };
        ll y = f(x);
        while (step % 40 || __gcd(prod, n) == 1) {
            if (x == y) break;
            ll q = binMul(prod, x > y ? x - y : y - x, n);
            if (q) prod = q;
            x = f(x);
            y = f(f(y));
            step++;
        }
        ll d = __gcd(prod, n);
        if (d != 1 && d != n) return d;
    }
}

// same output as primeFactor (sorted, with multiplicity) but O(n^(1/4))

// use for 1 <= n <= 1e18

vector<ll> primeFactorFast(ll n) {
    if (n == 1) return {};
    if (MillerRabin(n)) return {n};
    ll d = pollardRho(n);
    vector<ll> l = primeFactorFast(d), r = primeFactorFast(n / d);
    l.insert(l.end(), all(r));
    sort(all(l));
    return l;
}
#line 3 "NumberTheory/Math/Primitive_root.h"

// smallest primitive root of a PRIME p, that is g with order exactly p-1

// g works iff g^((p-1)/q) != 1 for every distinct prime q of p-1

// O(n^(1/4)) to factor p-1, then a few binPow per candidate

ll primitiveRoot(ll p) {
    if (p == 2) return 1;
    vector<ll> q = primeFactorFast(p - 1);
    unique(q);
    for (ll g = 2; ; g++) {
        bool ok = true;
        for (ll x : q) {
            if (binPow(g, (p-1) / x, p) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
Back to top page