Geeksforgeeks

Error

Given two integers **b **and **p**. Find the number of integral solutions to x^{2} = 1 (mod p) in the closed interval [1, b].

**Example 1:**

Input:b =5,p =7Output:1Explanation:1^{2}= 1%7 = 1. 1 is the only possible solution in the range [1,5].

**Example 2:**

Input:b =8,p =6Output:3Explanation:There are 3 possible solutions. 1^{2}= 1%6 = 1 5^{2}= 25%6 = 1 7^{2}= 49%6 = 1

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **noOfSol()** which takes 2 Integers b, and p as input and returns count of numbers in the range [1,b] that give a reamainder of 1 when the squared number is divided with p.

**Expected Time Complexity:** O(p)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 <= b <= 10^{9}

1 <= p <= 10^{5}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

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

YesLoading...

Number of solutions

...