给定一个大小为N的数组arr[]和一个素数P ,任务是计算数组的元素,使得模 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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。