#P4195. 【模板】扩展 BSGS / exBSGS

    ID: 3125 远端评测题 3000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>O2优化枚举,暴力素数判断,质数,筛法块状链表,块状数组,分块

【模板】扩展 BSGS / exBSGS

Description

Given a,p,ba,p,b, find the smallest non-negative integer xx such that axb(modp)a^x≡b \pmod p.

Input Format

Each test file contains several test cases, with the guarantee that p5×106\sum \sqrt p\le 5\times 10^6.

Each test case consists of one line with 33 positive integers a,p,ba,p,b.

When a=p=b=0a=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.

5 58 33
2 4 3
0 0 0
9
No Solution

Hint

For 100%100\% of the testdata, 1a,p,b1091\le a,p,b\le 10^9 or a=p=b=0a=p=b=0.

2021/5/14 strengthened by SSerxhs.
2021/7/1 added a set of hack testdata.

Translated by ChatGPT 5