Discrete logarithm modulo prime

Write codeHard40 min

Your task

Given a prime p and integers a, b, find the smallest non-negative integer x such that a^x is congruent to b modulo p, or return -1 if no such x exists. Assume p is prime and a is not a multiple of p. A linear scan over all exponents is too slow when p is large, so use the baby-step giant-step (meet-in-the-middle) algorithm running in O(sqrt(p)). Return 0 when b is congruent to 1 (since a^0 = 1).

Examples

discrete_log(a: int, b: int, p: int) -> int
Example 1
Inputdiscrete_log(2, 3, 5)
Output3