
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count number of solutions of x^2 = 1 (mod p) in given range in C++
We are given with integers x and p. The goal is to find the number of solutions of the equation −x2=1 ( mod p ) such that x lies in range [1,N].
We will do this by traversing from 1 to N and take each number as x check if (x*x)%p==1. If yes then increment the count.
Let’s understand with examples.
Input − n=5, p=2
Output − Number of Solutions − 3
Explanation − Between the range 1 to 5.
12=1%2=1, count=1 22=4%2=0, count=1 32=9%2=1, count=2 42=16%2=0, count=2 52=25%2=1, count=3 Total number of solutions=3.
Input − n=3, p=4
Output − Number of Solutions − 2
Explanation − Between the range 1 to 3.
12=1%4=1, count=1 22=4%4=0, count=1 32=9%4=1, count=2 Total number of solutions=2
Approach used in the below program is as follows
We take two variables n and p.
Function solutionsCount(int n,int p) takes both parameters n and p and returns the number of solutions for equation: x2%p==1 (or x2=1 ( mod p ) ).
Starting from x=1 to x=n, check if x*x==1, if yes increment count.
At the end of the loop, count will have the number of solutions.
Return count as result.
Example
#include<bits/stdc++.h> using namespace std; int solutionsCount(int n, int p){ int count = 0; for (int x=1; x<=n; x++){ if ((x*x)%p == 1) { ++count; } } return count; } int main(){ int n = 8, p = 3; cout<<"Number of solutions :"<<solutionsCount(n, p); return 0; }
Output
If we run the above code it will generate the following output −
Number of solutions : 6
- Related Articles
- Count number of smallest elements in given range in C++
- In the following, determine whether the given values are solutions of the given equation or not: $x^2\ +\ x\ +\ 1\ =\ 0,\ x\ =\ 0,\ x\ =\ 1$
- In the following, determine whether the given values are solutions of the given equation or not:$x^2\ –\ 3x\ +\ 2\ =\ 0,\ x\ =\ 2,\ x\ =\ –1$
- Find \( p(0), p(1) \) and \( p(2) \) for each of the following polynomials:(i) \( p(y)=y^{2}-y+1 \)(ii) \( p(t)=2+t+2 t^{2}-t^{3} \)(iii) \( p(x)=x^{3} \)(iv) \( p(x)=(x-1)(x+1) \)
- Find the value of \( k \), if \( x-1 \) is a factor of \( p(x) \) in each of the following cases:(i) \( p(x)=x^{2}+x+k \)(ii) \( p(x)=2 x^{2}+k x+\sqrt{2} \)(iii) \( p(x)=k x^{2}-\sqrt{2} x+1 \)(iv) \( p(x)=k x^{2}-3 x+k \)
- Verify whether the following are zeroes of the polynomial, indicated against them.(i) \( p(x)=3 x+1, x=-\frac{1}{3} \)(ii) \( p(x)=5 x-\pi, x=\frac{4}{5} \)(iii) \( p(x)=x^{2}-1, x=1,-1 \)(iv) \( p(x)=(x+1)(x-2), x=-1,2 \)(v) \( p(x)=x^{2}, x=0 \)(vi) \( p(x)=l x+m, x=-\frac{m}{l} \)(vii) \( p(x)=3 x^{2}-1, x=-\frac{1}{\sqrt{3}}, \frac{2}{\sqrt{3}} \)(viii) \( p(x)=2 x+1, x=\frac{1}{2} \)
- Find the zeros of the polynomial \( x^{2}+x-p(p+1) \).
- If $p(x) = x^2 - 2\sqrt{2}x+1$, then find the value of $p(2\sqrt{2})$.
- If $p(x)=x^{2}-2 \sqrt{2} x+1$, then what is the value of $p(2 \sqrt{2})$
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++
- Find value of y mod (2 raised to power x) in C++
- Use the Factor Theorem to determine whether \( g(x) \) is a factor of \( p(x) \) in each of the following cases:(i) \( p(x)=2 x^{3}+x^{2}-2 x-1, g(x)=x+1 \)(ii) \( p(x)=x^{3}+3 x^{2}+3 x+1, g(x)=x+2 \)(iii) \( p(x)=x^{3}-4 x^{2}+x+6, g(x)=x-3 \)
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n in C++
- Program to find count of numbers having odd number of divisors in given range in C++
- In the following, determine whether the given values are solutions of the given equation or not: $x^2\ –\ \sqrt{2}x\ –\ 4\ =\ 0,\ x\ =\ -\sqrt{2},\ x\ =\ -2\sqrt{2}$
