P4195 [Template] Extended BSGS / exBSGS

Background

Source: SPOJ3105 Mod.

Description

Given $a,p,b$, find the smallest non-negative integer $x$ such that $a^x≡b \pmod p$.

Input Format

Each test file contains several test cases, with the guarantee that $\sum \sqrt p\le 5\times 10^6$. Each test case consists of one line with $3$ positive integers $a,p,b$. When $a=p=b=0$, it indicates the end of input.

Output Format

For each test case, output one line. If there is no solution, output `No Solution`. Otherwise, output the smallest non-negative integer solution.

Explanation/Hint

For $100\%$ of the testdata, $1\le a,p,b\le 10^9$ or $a=p=b=0$. 2021/5/14 strengthened by [SSerxhs](https://www.luogu.com.cn/user/29826). 2021/7/1 added [a set of hack testdata](https://www.luogu.com.cn/discuss/391666). Translated by ChatGPT 5