-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimitive_root.cpp
More file actions
47 lines (37 loc) · 1.1 KB
/
primitive_root.cpp
File metadata and controls
47 lines (37 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
-> "g" is a primitive root modulo n if and only if for any integer a such that gcd(a,n) = 1,
there exists an integer k such that: g^k = a (mod n).
-> ref: https://cp-algorithms.com/algebra/primitive-root.html
*/
int pw(int base, int exp, int mod = MOD) {
int res = 1;
base %= mod;
if (base < 0) base += mod;
for (; exp; exp /= 2) {
if (exp % 2)
res = mul(res, base, mod);
base = mul(base, base, mod);
}
return res;
}
int generator(int p) {
vi fact;
int phi = p - 1, n = phi; // factorizing phi(n) (for a prime "n", phi(n) = n - 1)
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
fact.pb(i);
while (n % i == 0)
n /= i;
}
}
if (n > 1)
fact.pb(n);
int len = sz(fact);
for (int res = 2; res <= p; ++res) { // checking for primitive root
bool ok = 1;
for (int i = 0; i < len && ok; ++i)
ok &= (pw(res, phi / fact[i], p) != 1);
if (ok) return res;
}
return -1;
}