In the deepest vault of the Cryptography Bureau, the hardest challenge awaits: the discrete logarithm problem. Given a prime p, a base a, and a target b, find x such that a^x Ôëí b (mod p). This problem is the foundation of Diffie-Hellman key exchange and many cryptographic protocols. "The baby-step giant-step algorithm solves this in O(sqrt(p)) time," the Bureau Chief says. "Compute baby steps: a^j for j = 0 to m-1 where m = ceil(sqrt(p)). Then compute giant steps: b * (a^(-m))^i for i = 0 to m-1. When a baby step matches a giant step, x = i*m + j." Given a prime p, integers a and b, find the smallest non-negative x such that a^x Ôëí b (mod p). If no such x exists, output -1. Constraints: 2 <= p <= 10^9 (prime), 1 <= a, b < p Input: 5 2 3 Output: 3 Input: 11 2 5 Output: 4
Constraints:
2 <= p <= 10^9 (prime), 1 <= a, b < p
Tags:
