Given two integers n and p, find the number of integral solutions to x2 = 1 (mod p) in the closed interval [1, n].
Input : n = 10, p = 5 Output : 4 There are four integers that satisfy the equation x2 = 1. The numbers are 1, 4, 6 and 9. Input : n = 15, p = 7 Output : 5 There are five integers that satisfy the equation x2 = 1. The numbers are 1, 8, 15, 6 and 13.
One simple solution is to go through all numbers from 1 to n. For every number, check if it satisfies the equation. We can avoid going through the whole range. The idea is based on the fact that if a number x satisfies the equation, then all numbers of the form x + i*p also satisfy the equation. We traverse for all numbers from 1 to p and for every number x that satisfies the equation, we find the count of numbers of the form x + i*p. To find the count, we first find the largest number for given x and then add (largest-number – x)/p to the result.
Below is the implementation of the idea.
This article is contributed by Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Count digits in a factorial | Set 2
- Write a program to reverse digits of a number
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Minimum number of jumps to reach end
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Print all combinations of points that can compose a given number
- Count of Binary Digit numbers smaller than N
- Check if a number is multiple of 5 without using / and % operators
- Count numbers that don't contain 3
- Given a number, find the next smallest palindrome
- Count the number of possible triangles
- Select a random number from stream, with O(1) space
- Program to convert a given number to words