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 firstname.lastname@example.org. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find 'N' number of solutions with the given inequality equations
- Find the number of solutions to the given equation
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Value of the series (1^3 + 2^3 + 3^3 + ... + n^3) mod 4 for a given n
- How to compute mod of a big number?
- Find number of solutions of a linear equation of n variables
- Number of non-negative integral solutions of a + b + c = n
- Number of integral solutions of the equation x1 + x2 +.... + xN = k
- Number of non-negative integral solutions of sum equation
- Number of solutions to Modular Equations
- Number of integral solutions for equation x = b*(sumofdigits(x)^a)+c
- Program to find number of solutions in Quadratic Equation
- Number of solutions of n = x + n ⊕ x
- Number of solutions for x < y, where a <= x <= b and c <= y <= d and x, y are integers
- Number of solutions for the equation x + y + z <= n
- Find power of power under mod of a prime
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find value of y mod (2 raised to power x)
- Divide two integers without using multiplication, division and mod operator | Set2
- Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2