#P5668. 【模板】N 次剩余

【模板】N 次剩余

Description

You need to solve the equation xnk(modm)x^n\equiv k\pmod m, where x[0,m1]x\in [0,m-1].

Input Format

The first line contains a positive integer TT, the number of test cases.

Each test case contains three positive integers n,m,kn, m, k.

Output Format

For each test case, output one or two lines:

The first line contains the number of distinct solutions cc.

If c0c\neq 0, then output a second line with cc integers, in increasing order, listing all possible solutions separated by spaces.

It is guaranteed that ci106\sum c_i \le 10^6.

2
3 531441 330750
5 304128 1
27
264 19947 39630 59313 78996 98679 118362 138045 157728 177411 197094 216777 236460 256143 275826 295509 315192 334875 354558 374241 393924 413607 433290 452973 472656 492339 512022
5
1 82945 138241 165889 193537

Hint

For 100%100\% of the testdata, 1T1001\le T\le 100, 1n1091\le n\le 10^9, 0k<m1090\le k \lt m\le 10^9.

Let the unique factorization of mm be m=i=1spiqim=\prod_{i=1}^s p_i^{q_i}. It is guaranteed that the number of solutions to xnk(modpiqi)x^n\equiv k\pmod{p_i^{q_i}} in [0,piqi)[0,p_i^{q_i}) is 106\le 10^6.

Translated by ChatGPT 5