原始根
(number-theory/primitive-root.hpp)
- View this file on GitHub
- Last update: 2026-07-28 20:47:27+09:00
- Include:
#include "number-theory/primitive-root.hpp"
素数を法とする原始根を求める.
-
PrimitiveRoot(p):素数 $p$ の原始根をひとつ返す.$2\leq p\leq 10^{18}$ とする.
$p-1$ を Pollard’s rho algorithm で素因数分解する.$p-1$ の相異なる素因数を $q_1,q_2,\dots,q_k$ とすると,$g$ が原始根であることは
\[g^{(p-1)/q_i}\not\equiv 1\pmod p\quad(1\leq i\leq k)\]と同値である.小さい $g$ から順にこの条件を判定する.
計算量は $p-1$ の素因数分解に依存する.
Depends on
Miller-Rabin 素数判定
(number-theory/miller-rabin.hpp)
Pollard's rho algorithm
(number-theory/pollard-rho.hpp)
Verified with
Code
#pragma once
#include "number-theory/miller-rabin.hpp"
#include "number-theory/pollard-rho.hpp"
long long PrimitiveRoot(long long p) {
assert(p >= 2 && MillerRabin::is_prime(p));
if (p == 2) return 1;
auto factors = PollardRho::factorize(p - 1);
for (long long g = 2;; g++) {
bool ok = true;
for (auto [q, e] : factors)
if (MillerRabin::internal::power_mod(g, (p - 1) / q, p) == 1) {
ok = false;
break;
}
if (ok) return g;
}
}
/**
* @brief 原始根
* @docs docs/number-theory/primitive-root.md
*/#line 2 "number-theory/primitive-root.hpp"
#line 2 "number-theory/miller-rabin.hpp"
namespace MillerRabin {
using u64 = uint64_t;
using u128 = __uint128_t;
namespace internal {
u64 multiply_mod(u64 a, u64 b, u64 mod) { return u128(a) * b % mod; }
u64 power_mod(u64 a, u64 n, u64 mod) {
u64 ret = 1;
while (n) {
if (n & 1) ret = multiply_mod(ret, a, mod);
a = multiply_mod(a, a, mod);
n >>= 1;
}
return ret;
}
}; // namespace internal
bool is_prime(long long n) {
if (n < 2) return false;
u64 x = n;
for (u64 p : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
if (x % p == 0) return x == p;
}
int s = __builtin_ctzll(x - 1);
u64 d = (x - 1) >> s;
for (u64 a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
if (a % x == 0) continue;
u64 y = internal::power_mod(a % x, d, x);
if (y == 1 || y == x - 1) continue;
bool composite = true;
for (int r = 1; r < s; r++) {
y = internal::multiply_mod(y, y, x);
if (y == x - 1) {
composite = false;
break;
}
}
if (composite) return false;
}
return true;
}
}; // namespace MillerRabin
/**
* @brief Miller-Rabin 素数判定
* @docs docs/number-theory/miller-rabin.md
*/
#line 2 "number-theory/pollard-rho.hpp"
#line 4 "number-theory/pollard-rho.hpp"
namespace PollardRho {
using ll = long long;
using u64 = uint64_t;
namespace internal {
u64 random() {
static u64 x = 0x243f6a8885a308d3ULL;
x ^= x << 7;
x ^= x >> 9;
return x;
}
u64 find_factor(u64 n) {
if (n % 2 == 0) return 2;
if (n % 3 == 0) return 3;
while (true) {
u64 y = random() % (n - 1) + 1;
u64 c = random() % (n - 1) + 1;
u64 m = 128, g = 1, r = 1, q = 1, x = 0, z = 0;
auto f = [&](u64 v) {
return (MillerRabin::internal::multiply_mod(v, v, n) + c) % n;
};
while (g == 1) {
x = y;
for (u64 i = 0; i < r; i++) y = f(y);
for (u64 k = 0; k < r && g == 1; k += m) {
z = y;
for (u64 i = 0; i < min(m, r - k); i++) {
y = f(y);
u64 d = x > y ? x - y : y - x;
q = MillerRabin::internal::multiply_mod(q, d, n);
}
g = gcd(q, n);
}
r <<= 1;
}
if (g == n) {
do {
z = f(z);
u64 d = x > z ? x - z : z - x;
g = gcd(d, n);
} while (g == 1);
}
if (g != n) return g;
}
}
void factorize(u64 n, vector<u64>& factors) {
if (n == 1) return;
if (MillerRabin::is_prime(n)) {
factors.push_back(n);
return;
}
u64 d = find_factor(n);
factorize(d, factors);
factorize(n / d, factors);
}
}; // namespace internal
vector<pair<ll, int>> factorize(ll n) {
assert(n >= 1);
vector<u64> factors;
internal::factorize(n, factors);
sort(factors.begin(), factors.end());
vector<pair<ll, int>> ret;
for (u64 p : factors) {
if (ret.empty() || ret.back().first != (ll)p)
ret.emplace_back(p, 1);
else
ret.back().second++;
}
return ret;
}
vector<ll> divisors(ll n) {
vector<ll> ret{1};
for (auto [p, e] : factorize(n)) {
size_t size = ret.size();
ll q = 1;
while (e--) {
q *= p;
for (size_t i = 0; i < size; i++) ret.push_back(ret[i] * q);
}
}
sort(ret.begin(), ret.end());
return ret;
}
}; // namespace PollardRho
/**
* @brief Pollard's rho algorithm
* @docs docs/number-theory/pollard-rho.md
*/
#line 5 "number-theory/primitive-root.hpp"
long long PrimitiveRoot(long long p) {
assert(p >= 2 && MillerRabin::is_prime(p));
if (p == 2) return 1;
auto factors = PollardRho::factorize(p - 1);
for (long long g = 2;; g++) {
bool ok = true;
for (auto [q, e] : factors)
if (MillerRabin::internal::power_mod(g, (p - 1) / q, p) == 1) {
ok = false;
break;
}
if (ok) return g;
}
}
/**
* @brief 原始根
* @docs docs/number-theory/primitive-root.md
*/