模板 快速求单个原根
设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根。(其中φ(m)表示m的欧拉函数)
给出1个质数P,找出P最小的原根。
ll m; ll fac[maxn], cnt; ll quickPower(ll a, ll b, ll M) { ll ans = 1ll; ll base = a; while (b) { if (b & 1) { ans *= base; ans %= M; } base *= base; base %= M; b >>= 1; } return ans; } void get_fac(ll x) { x--; ll m = sqrt(x) + 0.5; for (int i = 2; i <= m; i++) { if (x % i == 0) fac[cnt++] = i; while (x % i == 0) x /= i; } if (x > 1) fac[cnt++] = x; } int main() { scanf("%lld", &m); get_fac(m); for (ll g = 2; g < m; g++) { int f = 1; for (int j = 0; j < cnt; j++) { if (quickPower(g, (m - 1) / fac[j], m) == 1ll) { f = 0; break; } } if (f) { printf("%d", g); break; } } }