#P10394. 接连不断!

接连不断!

Background

The little frog was locked in a small dark room in prison, suffering nonstop torture, both physically and mentally.

His mind is exhausted, and his body moves slowly.

Some things have been temporarily suppressed, but the more they are suppressed, the stronger the backlash will be when they rebound.

He needs a trigger.

Problem Description

The frog has m+1m + 1 undirected graphs, numbered 0m0 \sim m. The vertex set of each graph is VV, and VV contains nn vertices numbered 0n10 \sim n - 1. At the beginning, none of the graphs has any edges.

Next, in the graph with number xx, for every ii in [0,n1][0, n - 1], the frog will connect vertex ii and vertex (ix)modn(i \cdot x) \bmod n with an undirected edge.

He wants to know, among these m+1m + 1 graphs, how many graphs are connected. Please answer this question.

Notes:

  1. mod\bmod means modulo. amodba \bmod b is the remainder when aa is divided by bb.
  2. In a graph, if for any two different vertices x,yx, y in the vertex set there exists at least one path between them, then we call this graph connected.

Input Format

This problem contains multiple test cases.

The first line contains an integer TT, which is the number of test cases.

The next TT lines each contain two integers n,mn, m, with the same meaning as above.

Output Format

Output TT lines. Each line contains one integer, the answer for that test case.

3
3 2
4 4
5 5
1
3
2

5
4 3
4 4
4 5
4 6
4 7
2
3
3
4
4

3
1145 1499
12344 123456
114514 1919810
2
41
17

Hint

[Sample #1 Explanation]

Query 1:

Graph number Edge set Is the graph connected
00 (0,0),(1,0),(2,0)(0,0),(1,0),(2,0) Yes
11 (0,0),(1,1),(2,2)(0,0),(1,1),(2,2) No
22 (0,0),(1,2),(2,1)(0,0),(1,2),(2,1)

Query 2:

Graph number Edge set Is the graph connected
00 (0,0),(1,0),(2,0),(3,0)(0,0),(1,0),(2,0),(3,0) Yes
11 (0,0),(1,1),(2,2),(3,3)(0,0),(1,1),(2,2),(3,3) No
22 (0,0),(1,2),(2,0),(3,2)(0,0),(1,2),(2,0),(3,2) Yes
33 (0,0),(1,3),(2,2),(3,1)(0,0),(1,3),(2,2),(3,1) No
44 (0,0),(1,0),(2,0),(3,0)(0,0),(1,0),(2,0),(3,0) Yes

Query 3:

Graph number Edge set Is the graph connected
00 (0,0),(1,0),(2,0),(3,0),(4,0)(0,0),(1,0),(2,0),(3,0),(4,0) Yes
11 (0,0),(1,1),(2,2),(3,3),(4,4)(0,0),(1,1),(2,2),(3,3),(4,4) No
22 (0,0),(1,2),(2,4),(3,1),(4,3)(0,0),(1,2),(2,4),(3,1),(4,3)
33 (0,0),(1,3),(2,1),(3,4),(4,2)(0,0),(1,3),(2,1),(3,4),(4,2)
44 (0,0),(1,4),(2,3),(3,2),(4,1)(0,0),(1,4),(2,3),(3,2),(4,1)
55 (0,0),(1,0),(2,0),(3,0),(4,0)(0,0),(1,0),(2,0),(3,0),(4,0) Yes

So the answers are 1,3,21, 3, 2.


[Constraints]

This problem uses bundled testdata.

Subtask\text{Subtask} Constraints Score
11 n,m200n, m \le 200 1515
22 n,m3000n, m \le 3000 3030
33 n,m107n, m \le 10^7 1515
44 n3000,m1014n \le 3000, m \le 10^{14}
55 n,m1014n, m \le 10^{14} 2525
  • For 100%100\% of the testdata, it is guaranteed that 1T51 \le T \le 5 and 1n,m10141 \le n, m \le 10^{14}.

Translated by ChatGPT 5