#nt1002. 指数方程2
指数方程2
Index Equation 2
Problem Description
Answer queries. Each query gives a number ; output the smallest non-negative integer satisfying .
Input Format
The first line contains one integer . The following lines each contain three integers .
Output Format
One integer: the smallest non-negative integer solution; if there is no solution, output -1.
Sample Input
3
2 0 1024
123 456 789
114514 1919810 504289861
Sample Output
10
49
2
Constraints
For all data, it is guaranteed that , , .
Hint
- This asks for the discrete logarithm. Use the Baby-step Giant-step (BSGS) algorithm, accounting for the case (a solution exists iff is not coprime to in a way that eventually yields 0, or simply check small first) and extend it to the general (non-coprime) case with the extended BSGS method.