给定大小为N且质数为P的数组arr [] ,任务是对数组的元素进行计数,以使元素在模P下的模乘逆数等于元素本身。
例子:
Input: arr[] = {1, 6, 4, 5}, P = 7
Output: 2
Explanation:
Modular multiplicative inverse of arr[0](=1) under modulo P(= 7) is arr[0](= 1) itself.
Modular multiplicative inverse of arr1](= 6) under modulo P(= 7) is arr[1](= 6) itself.
Therefore, the required output is 2.
Input: arr[] = {1, 3, 8, 12, 12}, P = 13
Output: 3
天真的方法:最简单的方法是解决此问题,即遍历数组并打印数组元素的数量,以使元素在模P下的模乘逆数等于元素本身。
时间复杂度: O(N * log P)
辅助空间: O(1)
高效方法:为了优化上述方法,该思想基于以下观察结果:
If X and Y are two numbers such that (X × Y) % P = 1, then Y is modulo inverse of X.
Therefore, If Y is X itself, then (X × X) % P must be 1.
请按照以下步骤解决问题:
- 初始化一个变量,例如cntElem,以存储满足给定条件的元素的数量。
- 遍历给定的数组,并检查(arr [i] * arr [i])%P是否等于1 。如果发现是真的,则将cntElem的计数增加1 。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to get the count
// of elements that satisfy
// the given condition.
int equvInverse(int arr[],
int N, int P)
{
// Stores count of elements
// that satisfy the condition
int cntElem = 0;
// Traverse the given array.
for (int i = 0; i < N; i++) {
// If square of current
// element is equal to 1
if ((arr[i] * arr[i]) % P
== 1) {
cntElem++;
}
}
return cntElem;
}
// Driver Code
int main()
{
int arr[] = { 1, 6, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
int P = 7;
cout << equvInverse(arr, N, P);
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG{
// Function to get the count
// of elements that satisfy
// the given condition.
static int equvInverse(int[] arr,
int N, int P)
{
// Stores count of elements
// that satisfy the condition
int cntElem = 0;
// Traverse the given array.
for(int i = 0; i < N; i++)
{
// If square of current
// element is equal to 1
if ((arr[i] * arr[i]) % P == 1)
{
cntElem++;
}
}
return cntElem;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 6, 4, 5 };
int N = arr.length;
int P = 7;
System.out.println(equvInverse(arr, N, P));
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program to implement
# the above approach
# Function to get the count
# of elements that satisfy
# the given condition.
def equvInverse(arr, N, P):
# Stores count of elements
# that satisfy the condition
cntElem = 0
# Traverse the given array.
for i in range(0, N):
# If square of current
# element is equal to 1
if ((arr[i] * arr[i]) % P == 1):
cntElem = cntElem + 1
return cntElem
# Driver Code
if __name__ == "__main__":
arr = [ 1, 6, 4, 5 ]
N = len(arr)
P = 7
print(equvInverse(arr, N, P))
# This code is contributed by akhilsaini
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to get the count
// of elements that satisfy
// the given condition.
static int equvInverse(int[] arr, int N, int P)
{
// Stores count of elements
// that satisfy the condition
int cntElem = 0;
// Traverse the given array.
for(int i = 0; i < N; i++)
{
// If square of current
// element is equal to 1
if ((arr[i] * arr[i]) % P == 1)
{
cntElem++;
}
}
return cntElem;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 6, 4, 5 };
int N = arr.Length;
int P = 7;
Console.WriteLine(equvInverse(arr, N, P));
}
}
// This code is contributed by akhilsaini
Javascript
2
时间复杂度: O(N)
辅助空间: O(1)