Discrete Root
The problem of finding a discrete root is defined as follows. Given a prime and two integers and , find all for which:
The algorithm
We will solve this problem by reducing it to the discrete logarithm problem.
Let's apply the concept of a primitive root modulo . Let be a primitive root modulo . Note that since is prime, it must exist, and it can be found in plus time of factoring .
We can easily discard the case where . In this case, obviously there is only one answer: .
Since we know that is a prime and any number between 1 and can be represented as a power of the primitive root, we can represent the discrete root problem as follows:
where
This, in turn, can be rewritten as
Now we have one unknown , which is a discrete logarithm problem. The solution can be found using Shanks' baby-step giant-step algorithm in (or we can verify that there are no solutions).
Having found one solution , one of solutions of discrete root problem will be .
Finding all solutions from one known solution
To solve the given problem in full, we need to find all solutions knowing one of them: .
Let's recall the fact that a primitive root always has order of , i.e. the smallest power of which gives 1 is . Therefore, if we add the term to the exponential, we still get the same value:
Hence, all the solutions are of the form:
.
where is chosen such that the fraction must be an integer. For this to be true, the numerator has to be divisible by the least common multiple of and . Remember that least common multiple of two numbers ; we'll get
.
This is the final formula for all solutions of the discrete root problem.
Implementation
Here is a full implementation, including procedures for finding the primitive root, discrete log and finding and printing all solutions.
int gcd(int a, int b) {
return a ? gcd(b % a, a) : b;
}
int powmod(int a, int b, int p) {
int res = 1;
while (b > 0) {
if (b & 1) {
res = res * a % p;
}
a = a * a % p;
b >>= 1;
}
return res;
}
// Finds the primitive root modulo p
int generator(int p) {
vector<int> fact;
int phi = p-1, n = phi;
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 res = 2; res <= p; ++res) {
bool ok = true;
for (int factor : fact) {
if (powmod(res, phi / factor, p) == 1) {
ok = false;
break;
}
}
if (ok) return res;
}
return -1;
}
// This program finds all numbers x such that x^k = a (mod n)
int main() {
int n, k, a;
scanf("%d %d %d", &n, &k, &a);
if (a == 0) {
puts("1\n0");
return 0;
}
int g = generator(n);
// Baby-step giant-step discrete logarithm algorithm
int sq = (int) sqrt (n + .0) + 1;
vector<pair<int, int>> dec(sq);
for (int i = 1; i <= sq; ++i)
dec[i-1] = {powmod(g, i * sq * k % (n - 1), n), i};
sort(dec.begin(), dec.end());
int any_ans = -1;
for (int i = 0; i < sq; ++i) {
int my = powmod(g, i * k % (n - 1), n) * a % n;
auto it = lower_bound(dec.begin(), dec.end(), make_pair(my, 0));
if (it != dec.end() && it->first == my) {
any_ans = it->second * sq - i;
break;
}
}
if (any_ans == -1) {
puts("0");
return 0;
}
// Print all possible answers
int delta = (n-1) / gcd(k, n-1);
vector<int> ans;
for (int cur = any_ans % delta; cur < n-1; cur += delta)
ans.push_back(powmod(g, cur, n));
sort(ans.begin(), ans.end());
printf("%d\n", ans.size());
for (int answer : ans)
printf("%d ", answer);
}