-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathprimitive_root.cpp
More file actions
48 lines (38 loc) · 849 Bytes
/
primitive_root.cpp
File metadata and controls
48 lines (38 loc) · 849 Bytes
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
48
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int powmod(int x, int n, int MOD){
if (n == 0) return 1;
if (n%2 == 0){
int y = powmod(x, n/2, MOD);
return (y*(ll)y)%MOD;
}
return (powmod(x, n-1, MOD)*(ll)x)%MOD;
}
// p must be prime
int proot(int p){
int phi = p-1;
int n = phi;
vector<int>fact;
for(int i = 2;i*i <= n;i++){
if (n%i == 0){
fact.push_back(i);
while(n%i == 0) n /= i;
}
}
if (n > 1) fact.push_back(n);
for(int a = 2;a <= p;a++){
bool ok = true;
for(int i = 0;i < fact.size() && ok;i++){
ok &= powmod(a, phi/fact[i], p) != 1;
}
if (ok) return a;
}
return -1;
}
int main(){
// 90441961
int x;cin>>x;
cout << proot(x) << endl;
return 0;
}