primitive_root.hpp¶
求素数模或支持模数的原根;用于 NTT、离散对数和乘法群生成元问题。
\[
\displaystyle g^{p-1}\equiv 1\pmod p,\; g^{(p-1)/q}\not\equiv1
\]
Complexity: Time: Expected factorization time plus O(c omega(p-1) log p) for c tested candidates. Space: O(omega(p-1)).
AC 记录:primitive_root。
Implementation¶
当前头文件,省略 include guard;依赖见 #include。
/// @complexity Time: Expected factorization time plus O(c omega(p-1) log p) for c tested candidates.
/// Space: O(omega(p-1)).
#include "noya/factorize.hpp"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <numeric>
#include <vector>
namespace noya {
/// @brief Return the smallest primitive root modulo a prime.
inline std::uint64_t primitive_root(std::uint64_t p) {
assert(is_prime(p));
if (p == 2) {
return 1;
}
std::vector<std::uint64_t> div;
for (auto [fct, exp] : factorize(p - 1)) {
(void)exp;
div.push_back(fct);
}
for (std::uint64_t can = 2; can < p; can++) {
bool gnr = true;
for (std::uint64_t dvs : div) {
if (factorize_internal::power_mod(can, (p - 1) / dvs, p) == 1) {
gnr = false;
break;
}
}
if (gnr) {
return can;
}
}
assert(false);
return 0;
}
/// @brief Return every primitive root modulo a prime in increasing order.
inline std::vector<std::uint64_t> primitive_roots(std::uint64_t p) {
std::uint64_t gen = primitive_root(p);
std::vector<std::uint64_t> res;
if (p == 2) {
return {1};
}
for (std::uint64_t exp = 1; exp < p; exp++) {
if (std::gcd(exp, p - 1) == 1) {
res.push_back(factorize_internal::power_mod(gen, exp, p));
}
}
std::sort(res.begin(), res.end());
return res;
}
} // namespace noya
#ifndef NOYA_PRIMITIVE_ROOT_HPP
#define NOYA_PRIMITIVE_ROOT_HPP 1
/// @complexity Time: Expected factorization time plus O(c omega(p-1) log p) for c tested candidates.
/// Space: O(omega(p-1)).
#include "noya/factorize.hpp"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <numeric>
#include <vector>
namespace noya {
/// @brief Return the smallest primitive root modulo a prime.
inline std::uint64_t primitive_root(std::uint64_t p) {
assert(is_prime(p));
if (p == 2) {
return 1;
}
std::vector<std::uint64_t> div;
for (auto [fct, exp] : factorize(p - 1)) {
(void)exp;
div.push_back(fct);
}
for (std::uint64_t can = 2; can < p; can++) {
bool gnr = true;
for (std::uint64_t dvs : div) {
if (factorize_internal::power_mod(can, (p - 1) / dvs, p) == 1) {
gnr = false;
break;
}
}
if (gnr) {
return can;
}
}
assert(false);
return 0;
}
/// @brief Return every primitive root modulo a prime in increasing order.
inline std::vector<std::uint64_t> primitive_roots(std::uint64_t p) {
std::uint64_t gen = primitive_root(p);
std::vector<std::uint64_t> res;
if (p == 2) {
return {1};
}
for (std::uint64_t exp = 1; exp < p; exp++) {
if (std::gcd(exp, p - 1) == 1) {
res.push_back(factorize_internal::power_mod(gen, exp, p));
}
}
std::sort(res.begin(), res.end());
return res;
}
} // namespace noya
#endif // NOYA_PRIMITIVE_ROOT_HPP
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <numeric>
#include <utility>
#include <vector>
/// @complexity Time: Expected factorization time plus O(c omega(p-1) log p) for c tested candidates.
/// Space: O(omega(p-1)).
/// @complexity Time: O(log^3 n) primality testing; Pollard-rho factorization is expected about O(n^(1/4)).
/// Space: O(log n) recursion and factors.
namespace noya {
namespace factorize_internal {
using u64 = std::uint64_t;
using u128 = unsigned __int128;
inline u64 multiply_mod(u64 a, u64 b, u64 mod) {
return u64(u128(a) * b % mod);
}
inline u64 power_mod(u64 a, u64 exp, u64 mod) {
u64 res = 1;
while (exp > 0) {
if (exp & 1) {
res = multiply_mod(res, a, mod);
}
a = multiply_mod(a, a, mod);
exp >>= 1;
}
return res;
}
inline bool miller_rabin(u64 n) {
if (n < 2) {
return false;
}
for (u64 p :
std::array<u64, 12>{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
if (n % p == 0) {
return n == p;
}
}
int shf = __builtin_ctzll(n - 1);
u64 odd = (n - 1) >> shf;
for (u64 bas :
std::array<u64, 7>{2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
if (bas % n == 0) {
continue;
}
u64 val = power_mod(bas % n, odd, n);
if (val == 1 || val == n - 1) {
continue;
}
bool cmp = true;
for (int i = 1; i < shf; i++) {
val = multiply_mod(val, val, n);
if (val == n - 1) {
cmp = false;
break;
}
}
if (cmp) {
return false;
}
}
return true;
}
inline u64 splitmix64(u64 &st) {
u64 z = (st += 0x9e3779b97f4a7c15ULL);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
return z ^ (z >> 31);
}
inline u64 pollard_rho(u64 n) {
if (n % 2 == 0) {
return 2;
}
if (n % 3 == 0) {
return 3;
}
static u64 st = 0x123456789abcdef0ULL;
while (true) {
u64 y = splitmix64(st) % (n - 1) + 1;
u64 c = splitmix64(st) % (n - 1) + 1;
constexpr u64 blk = 128;
u64 g = 1;
u64 r = 1;
u64 q = 1;
u64 x = 0;
u64 sy = 0;
auto nxt = [&](u64 val) {
return u64((u128(multiply_mod(val, val, n)) + c) % n);
};
while (g == 1) {
x = y;
for (u64 i = 0; i < r; i++) {
y = nxt(y);
}
for (u64 off = 0; off < r && g == 1; off += blk) {
sy = y;
for (u64 i = 0; i < std::min(blk, r - off); i++) {
y = nxt(y);
u64 dif = x > y ? x - y : y - x;
q = multiply_mod(q, dif, n);
}
g = std::gcd(q, n);
}
r <<= 1;
}
if (g == n) {
do {
sy = nxt(sy);
u64 dif = x > sy ? x - sy : sy - x;
g = std::gcd(dif, n);
} while (g == 1);
}
if (g != n) {
return g;
}
}
}
inline void collect_factors(u64 n, std::vector<u64> &res) {
if (n == 1) {
return;
}
if (miller_rabin(n)) {
res.push_back(n);
return;
}
u64 fct = pollard_rho(n);
collect_factors(fct, res);
collect_factors(n / fct, res);
}
} // namespace factorize_internal
/// @brief Deterministic Miller-Rabin primality test for unsigned 64-bit
/// integers.
inline bool is_prime(std::uint64_t n) {
return factorize_internal::miller_rabin(n);
}
/// @brief Return the prime factors of n with multiplicity in increasing order.
inline std::vector<std::uint64_t> prime_factors(std::uint64_t n) {
assert(n >= 1);
std::vector<std::uint64_t> res;
factorize_internal::collect_factors(n, res);
std::sort(res.begin(), res.end());
return res;
}
/// @brief Return the prime factorization of n as (prime, exponent) pairs.
inline std::vector<std::pair<std::uint64_t, int>> factorize(std::uint64_t n) {
std::vector<std::pair<std::uint64_t, int>> res;
for (std::uint64_t p : prime_factors(n)) {
if (res.empty() || res.back().first != p) {
res.emplace_back(p, 1);
} else {
res.back().second++;
}
}
return res;
}
} // namespace noya
namespace noya {
/// @brief Return the smallest primitive root modulo a prime.
inline std::uint64_t primitive_root(std::uint64_t p) {
assert(is_prime(p));
if (p == 2) {
return 1;
}
std::vector<std::uint64_t> div;
for (auto [fct, exp] : factorize(p - 1)) {
(void)exp;
div.push_back(fct);
}
for (std::uint64_t can = 2; can < p; can++) {
bool gnr = true;
for (std::uint64_t dvs : div) {
if (factorize_internal::power_mod(can, (p - 1) / dvs, p) == 1) {
gnr = false;
break;
}
}
if (gnr) {
return can;
}
}
assert(false);
return 0;
}
/// @brief Return every primitive root modulo a prime in increasing order.
inline std::vector<std::uint64_t> primitive_roots(std::uint64_t p) {
std::uint64_t gen = primitive_root(p);
std::vector<std::uint64_t> res;
if (p == 2) {
return {1};
}
for (std::uint64_t exp = 1; exp < p; exp++) {
if (std::gcd(exp, p - 1) == 1) {
res.push_back(factorize_internal::power_mod(gen, exp, p));
}
}
std::sort(res.begin(), res.end());
return res;
}
} // namespace noya