forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discrete_log.cpp
38 lines (31 loc) · 844 Bytes
/
discrete_log.cpp
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
#include <bits/stdc++.h>
using namespace std;
// returns any x such that a^x = b (mod m)
// O(m^0.5) complexity
int discrete_log(int a, int b, int m) {
assert(gcd(a, m) == 1);
int n = (int)sqrt(m) + 1;
int an = 1;
for (int i = 0; i < n; ++i)
an = ((long long)an * a) % m;
unordered_map<int, int> vals;
for (int i = 1, cur = an; i <= n; ++i) {
if (!vals.count(cur))
vals[cur] = i;
cur = ((long long)cur * an) % m;
}
for (int i = 0, cur = b; i <= n; ++i) {
if (vals.count(cur)) {
int res = (long long)vals[cur] * n - i;
if (res < m)
return res;
}
cur = ((long long)cur * a) % m;
}
return -1;
}
// usage example
int main() {
// 2^x = 3 (mod 5), x = 3
cout << discrete_log(2, 3, 5) << endl;
}