-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdiscrete_root.cpp
More file actions
93 lines (73 loc) · 1.91 KB
/
discrete_root.cpp
File metadata and controls
93 lines (73 loc) · 1.91 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#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;
}
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 dlog(int a, int b, int m){
int n = (int)sqrt(m)+1;
int an = 1;
for(int i = 0;i < n;i++){
an = (an*(ll)a)%m;
}
unordered_map<int, int>values;
for(int p = 1, cur = an;p <= n;p++){
if (cur == 0) break;
if (values.count(cur) == 0) values[cur] = p;
cur = (cur*(ll)an)%m;
}
int c = b;
for(int q = 0;q <= n;q++){
if (values.count(c)){
int x = values[c]*n-q;
return x;
}
c = (c*(ll)a)%m;
}
return -1;
}
int main(){
// Find x such that x^k = a (mod n)
// n must be prime
int k, a, n;cin>>k>>a>>n;
// Primitive root of n
int g = proot(n);
// Finding one solution with discrete log
int y = dlog(powmod(g, k, n), a, n);
if (y == -1) return ((cout << "No solution" << endl), 0);
// Finding all solutions
vector<int>solutions;
int d = (n-1)/__gcd(n-1, k);
for(int cur = y % d;cur < n-1;cur += d){
solutions.push_back(powmod(g, cur, n));
}
sort(solutions.begin(), solutions.end());
cout << "List of x such that x^" << k << " = " << a << " (mod "<< n << ")" << endl;
for(auto s:solutions) cout << s << endl;
return 0;
}