Discrete Logarithm
Time limit1sMemory limit128 MB
For each test case with prime P, base B, and target N, find the smallest L with B^L congruent to N mod P, or report no solution.
- Level
Hard8 of 10
- Topics
- Math, Number theory, Hash map, Brute force
- Solved
- No attempts yet
Problem
Given a prime (), an integer (), and an integer (), write a program that computes the discrete logarithm of with base modulo .
That is, find an integer satisfying the following condition:
Input
The input consists of several test cases. Each test case is a single line containing , , and separated by spaces. The input continues until the end of file.
Output
For each test case, print the discrete logarithm of on its own line. If more than one satisfies the condition, print the smallest such value. If no such exists, print no solution.
Hint
To solve this problem you need Fermat's little theorem, which is rarely used in ACM-ICPC contests. See Fermat's little theorem for details.