Number of solutions
Easy Accuracy: 9.49% Submissions: 718 Points: 2

Your task is to find the number of integral solutions to x2 = 1 (mod p) in the closed interval [1, b].

Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of one line. There are two space separated integers in this line which denote b and p.

Output:
Corresponding to each test case, in a new line, print the number of integral solution to x2 = 1 (mod p) in the closed interval [1, b].

Constraints:
1 ≤ T ≤ 200
1 ≤ b ≤ 10
1 ≤ p ≤ 105

Example:
Input:
3
5 7
8 6
4 3

Output:
1
3
3

Explanation
In the interval [1,5] there is only one solution to x2 = 1(mod 7), x = 1
In the interval [1,8] there are three solutions to x2 = 1(mod 6), x = 1,5,7
In the interval [1,4] there are three solutions to x2 = 1(mod 3), x = 1,2,4

 

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.