QOJ.ac

QOJ

Time Limit: 1.2 s Memory Limit: 512 MB Total points: 100

#13993. 同余方程

统计

Background

These are just some simple quadratic congruence equations:)

Description

Provided several pairs of $p$ and $x$, we want to solve the congruence equation $a^2 + b^2 \equiv x \bmod p$ for $a$ and $b$. We want to know the number of solutions given that $a$ and $b$ are integers between $0$ and $p - 1$. Here we guarantee that $p$ is an odd number and is not divisible by any square numbers.

Input Format

Read from standard input.

The first line contains an integer $n$, representing the number of queries.

The following $n$ lines contains on each of them two integers $p$ and $x$ separated by a space.

We guarantee that $0 \leq x \leq p-1$, $p$ is an odd number, and for any odd prime $q \mid p$, we have $q^2 \nmid p$.

Output Format

Write to the standard output.

The output contains $n$ lines. The $i$ th line contains an integer representing the number of solutions of the $i$ th equation.

Sample

Input

1
5 0

Output

9

Explanation

The 9 solutions are $(a,b) = (0,0), (1,2), (1,3), (2,1), (2,4), (3,1), (3,4), (4,2), (4,3)$。

Sample 2

See ex_2.in and ex_2.ans in the attachments.

Subtasks

For all tasks, $n \leq 10^5$, $p \leq 10^6$,and $2 \nmid p$, $\forall$ odd prime $q \mid p$, $q^2 \nmid p$, $0 \leq x \leq p-1$.

subtasks$n$$p$extra restrictions
1$ ≤ 5 $$ < 10^{2} $p is an odd prime
2$ ≤ 10 $$ < 10^{3} $
3/
4$≤ 50 $$ < 10^{4} $p is an odd prime
5$ ≤ 10^{2} $
6$ ≤ 50 $/
7$ ≤ 10^{2} $
8
9$ ≤ 10^{3} $$ < 10^{6} $p is an odd prime
10/
11
12$ ≤ 10^{5} $p is an odd prime
13/
14
15
16
17$ < 10^{7} $
18
19
20

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.