p
#pragma once #include "Mylib/Number/Mod/mod_pow.cpp" namespace haar_lib { constexpr int primitive_root(int p) { int pf[30] = {}; int k = 0; { int n = p - 1; for (int64_t i = 2; i * i <= p; ++i) { if (n % i == 0) { pf[k++] = i; while (n % i == 0) n /= i; } } if (n != 1) pf[k++] = n; } for (int g = 2; g <= p; ++g) { bool ok = true; for (int i = 0; i < k; ++i) { if (mod_pow(g, (p - 1) / pf[i], p) == 1) { ok = false; break; } } if (not ok) continue; return g; } return -1; } } // namespace haar_lib
#line 2 "Mylib/Number/Mod/mod_pow.cpp" #include <cstdint> namespace haar_lib { constexpr int64_t mod_pow(int64_t n, int64_t p, int64_t m) { int64_t ret = 1; while (p > 0) { if (p & 1) (ret *= n) %= m; (n *= n) %= m; p >>= 1; } return ret; } } // namespace haar_lib #line 3 "Mylib/Number/Prime/primitive_root.cpp" namespace haar_lib { constexpr int primitive_root(int p) { int pf[30] = {}; int k = 0; { int n = p - 1; for (int64_t i = 2; i * i <= p; ++i) { if (n % i == 0) { pf[k++] = i; while (n % i == 0) n /= i; } } if (n != 1) pf[k++] = n; } for (int g = 2; g <= p; ++g) { bool ok = true; for (int i = 0; i < k; ++i) { if (mod_pow(g, (p - 1) / pf[i], p) == 1) { ok = false; break; } } if (not ok) continue; return g; } return -1; } } // namespace haar_lib