Count number of solutions of x^2 = 1 (mod p) in given range
Last Updated :
01 Sep, 2022
Given two integers n and p, find the number of integral solutions to x2 = 1 (mod p) in the closed interval [1, n].
Examples:
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.
C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int findCountOfSolutions( int n, int p)
{
ll ans = 0;
for (ll x=1; x<p; x++)
{
if ((x*x)%p == 1)
{
ll last = x + p * (n/p);
if (last > n)
last -= p;
ans += ((last-x)/p + 1);
}
}
return ans;
}
int main()
{
ll n = 10, p = 5;
printf ( "%lld\n" , findCountOfSolutions(n, p));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findCountOfSolutions( int n,
int p)
{
int ans = 0 ;
for ( int x = 1 ; x < p; x++)
{
if ((x * x) % p == 1 )
{
int last = x + p * (n / p);
if (last > n)
last -= p;
ans += ((last - x) / p + 1 );
}
}
return ans;
}
public static void main (String[] args)
{
int n = 10 ;
int p = 5 ;
System.out.println(
findCountOfSolutions(n, p));
}
}
|
Python3
def findCountOfSolutions(n, p):
ans = 0 ;
for x in range ( 1 , p):
if ((x * x) % p = = 1 ):
last = x + p * (n / p);
if (last > n):
last - = p;
ans + = ((last - x) / p + 1 );
return int (ans);
n = 10 ;
p = 5 ;
print (findCountOfSolutions(n, p));
|
C#
using System;
class GFG
{
static int findCountOfSolutions( int n,
int p)
{
int ans = 0;
for ( int x = 1; x < p; x++)
{
if ((x * x) % p == 1)
{
int last = x + p * (n / p);
if (last > n)
last -= p;
ans += ((last - x) / p + 1);
}
}
return ans;
}
static public void Main ()
{
int n = 10;
int p = 5;
Console.WriteLine(
findCountOfSolutions(n, p));
}
}
|
PHP
<?php
function findCountOfSolutions( $n , $p )
{
$ans = 0;
for ( $x = 1; $x < $p ; $x ++)
{
if (( $x * $x ) % $p == 1)
{
$last = $x + $p * ( $n / $p );
if ( $last > $n )
$last -= $p ;
$ans += (( $last - $x ) / $p + 1);
}
}
return $ans ;
}
$n = 10;
$p = 5;
echo findCountOfSolutions( $n , $p );
?>
|
Javascript
<script>
function findCountOfSolutions(n, p)
{
let ans = 0;
for (let x = 1; x < p; x++)
{
if ((x * x) % p == 1)
{
let last = x + p * (n / p);
if (last > n)
last -= p;
ans += ((last - x) / p + 1);
}
}
return ans;
}
let n = 10;
let p = 5;
document.write(findCountOfSolutions(n, p));
</script>
|
Output:
4
Time Complexity: O(p)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...