#P6610. [Code+#7] 同余方程

[Code+#7] 同余方程

Problem Description

These are some naive quadratic congruence equations.


Given several groups of positive integers pp and xx, find the number of solution pairs (a,b)(a,b) to the equation a2+b2≡x(modp)a^2 + b^2 \equiv x {\pmod p} with respect to aa and bb modulo p\boldsymbol p, where pp is odd and has no square factors.

Input Format

The first line contains a positive integer nn, indicating the number of queries.

The next nn lines each contain two positive integers pp and xx separated by a space. It is guaranteed that 0≤x≤p−10 \le x \le p - 1, pp is odd, and for any odd prime q∣pq \mid p, we have q2∤pq^2 \nmid p.

Output Format

Output nn lines. The ii-th line contains a positive integer, indicating the number of solution pairs for the ii-th equation.

1
5 0
9

Hint

Sample Explanation

The 99 solution pairs are $(a,b) = (0,0),(1,2),(1,3),(2,1),(2,4),(3,1),(3,4),(4,2),(4,3)$.

Subtasks

Each test point is worth 55 points.

For all testdata, n≤105n \le 10^5, p≤107p \le 10^7, and 2∤p2 \nmid p, ∀\forall odd primes q∣p,q2∤pq \mid p, q^2 \nmid p, 0≤x≤p−10 \le x \le p - 1.

Test Point ID n≤n \le p≤p \le Additional Property
11 55 100100 pp is an odd prime
22 1010 10310^3
33
44 5050 10410^4 pp is an odd prime
55 100100
66 5050
77 100100
88
99 10310^3 10610^6 pp is an odd prime
1010
1111
1212 10510^5 pp is an odd prime
1313
1414
1515
1616
1717 10710^7
1818
1919
2020

Translated by ChatGPT 5