#P6091. 【模板】原根

【模板】原根

Description

Given an integer nn, find all of its primitive roots.

To reduce the amount of output, an output parameter dd is given. Suppose nn has cc primitive roots, and in increasing order they are g1,,gcg_1,\ldots,g_c. You only need to output $g_d,g_{2d},\ldots,g_{\lfloor\frac{c}{d}\rfloor\times d}$ in order.


If you do not know the definition of a primitive root, you may look it up yourself or read the definition below:

A positive integer gg is a primitive root of a positive integer nn if and only if 1gn11\leq g\leq n-1, and the multiplicative order of gg modulo nn is φ(n)\varphi(n).

Input Format

This problem contains multiple test cases.

The first line: an integer TT, the number of test cases.

The next TT lines: each line contains two integers n,dn,d, representing one query.

Output Format

For each test case:

Output an integer cc in the first line, which is the number of primitive roots of nn. In the second line, output cd\lfloor\dfrac{c}{d}\rfloor numbers as required in the statement.

Note: even if cd=0\lfloor\dfrac{c}{d}\rfloor=0, you still need to output an empty line.

6
2 1
4 1
25 2
36 1
9 6
18 1

1
1 
1
3 
8
3 12 17 23 
0

2

2
5 11 

Hint

[Sample Explanation]

For test cases 1,2,4,61,2,4,6, all primitive roots of the given nn appear in the output.

For test case 33, the set of primitive roots of 2525 is {2,3,8,12,13,17,22,23}\{2,3,8,12,13,17,22,23\}.

For test case 55, the set of primitive roots of 99 is {2,5}\{2,5\}.

[Constraints]

For 100%100\% of the testdata, 1T101\leq T\leq 10, 2n1062\leq n\leq 10^6, 1d2001\leq d\leq 200. It is guaranteed that the total number of output integers does not exceed 10510^5.

Translated by ChatGPT 5