📜  在给定范围内计算x ^ 2 = 1(mod p)的解数

📅  最后修改于: 2021-04-29 07:03:44             🧑  作者: Mango

给定两个整数n和p,请求出封闭区间[1,n]中x 2 = 1(mod p)的积分解的数量。

例子:

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.   

一种简单的解决方案是遍历从1到n的所有数字。对于每个数字,请检查其是否满足方程式。我们可以避免遍及整个范围。该想法基于这样一个事实,即如果数字x满足方程式,则x + i * p形式的所有数字也都满足方程式。我们遍历从1到p的所有数字,对于满足方程的每个数字x,我们发现x + i * p形式的数字计数。为了找到计数,我们首先找到给定x的最大数字,然后将(largest-number – x)/ p加到结果中。

以下是该想法的实现。

C++
// C++ program to count number of values
// that satisfy x^2  = 1 mod p where x lies
// in range [1, n]
#include
using namespace std;
typedef long long ll;
 
int findCountOfSolutions(int n, int p)
{
    // Initialize result
    ll ans = 0;
 
    // Traverse all numbers smaller than
    // given number p. Note that we don't
    // traverse from 1 to n, but 1 to p
    for (ll x=1; x n)
                last -= p;
 
            // Add count of numbers of the form
            // x + p*i. 1 is added for x itself.
            ans += ((last-x)/p + 1);
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    ll n = 10, p = 5;
    printf("%lld\n", findCountOfSolutions(n, p));
    return 0;
}


Java
// Java program to count
// number of values that
// satisfy x^2 = 1 mod p
// where x lies in range [1, n]
import java.io.*;
 
class GFG
{
static int findCountOfSolutions(int n,
                                int p)
{
    // Initialize result
    int ans = 0;
 
    // Traverse all numbers
    // smaller than given
    // number p. Note that
    // we don't traverse from
    // 1 to n, but 1 to p
    for (int x = 1; x < p; x++)
    {
        // If x is a solution,
        // then count all numbers
        // of the form x + i*p
        // such that x + i*p is
        // in range [1,n]
        if ((x * x) % p == 1)
        {
            // The largest number
            // in the form of x +
            // p*i in range [1, n]
            int last = x + p * (n / p);
            if (last > n)
                last -= p;
 
            // Add count of numbers
            // of the form x + p*i.
            // 1 is added for x itself.
            ans += ((last - x) / p + 1);
        }
    }
    return ans;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 10;
    int p = 5;
    System.out.println(
               findCountOfSolutions(n, p));
}
}
 
// This code is contributed by ajit


Python3
# Program to count number of
# values that satisfy x^2 = 1
# mod p where x lies in range [1, n]
 
def findCountOfSolutions(n, p):
     
    # Initialize result
    ans = 0;
 
    # Traverse all numbers smaller
    # than given number p. Note
    # that we don't traverse from
    # 1 to n, but 1 to p
    for x in range(1, p):
         
        # If x is a solution, then
        # count all numbers of the
        # form x + i*p such that
        # x + i*p is in range [1,n]
        if ((x * x) % p == 1):
             
            # The largest number in the
            # form of x + p*i in range
            # [1, n]
            last = x + p * (n / p);
            if (last > n):
                last -= p;
 
            # Add count of numbers of
            # the form x + p*i. 1 is
            # added for x itself.
            ans += ((last - x) / p + 1);
    return int(ans);
 
# Driver code
n = 10;
p = 5;
print(findCountOfSolutions(n, p));
     
# This code is contributed by mits


C#
// C# program to count
// number of values that
// satisfy x^2 = 1 mod p
// where x lies in range [1, n]
using System;
 
class GFG
{
static int findCountOfSolutions(int n,
                                int p)
{
    // Initialize result
    int ans = 0;
 
    // Traverse all numbers
    // smaller than given
    // number p. Note that
    // we don't traverse from
    // 1 to n, but 1 to p
    for (int x = 1; x < p; x++)
    {
        // If x is a solution,
        // then count all numbers
        // of the form x + i*p
        // such that x + i*p is
        // in range [1,n]
        if ((x * x) % p == 1)
        {
            // The largest number
            // in the form of x +
            // p*i in range [1, n]
            int last = x + p * (n / p);
            if (last > n)
                last -= p;
 
            // Add count of numbers
            // of the form x + p*i.
            // 1 is added for x itself.
            ans += ((last - x) / p + 1);
        }
    }
    return ans;
}
 
// Driver code
static public void Main ()
{
    int n = 10;
    int p = 5;
    Console.WriteLine(
            findCountOfSolutions(n, p));
}
}
 
// This code is contributed by ajit


PHP
 $n)
                $last -= $p;
 
            // Add count of numbers of
            // the form x + p*i. 1 is
            // added for x itself.
            $ans += (($last - $x) / $p + 1);
        }
    }
    return $ans;
}
 
// Driver code
$n = 10;
$p = 5;
echo findCountOfSolutions($n, $p);
     
// This code is contributed by ajit
?>


Javascript


输出:

4