给定一个长度为N的数组arr []和一个正整数M ,任务是找到所有与M的逆模存在的数组元素的按位XOR。
例子:
Input: arr[] = {1, 2, 3}, M = 4
Output: 2
Explanation:
Initialize the value xor with 0:
For element indexed at 0 i.e., 1, its mod inverse with 4 is 1 because (1 * 1) % 4 = 1 i.e., it exists. Therefore, xor = (xor ^ 1) = 1.
For element indexed at 1 i.e., 2, its mod inverse does not exist.
For element indexed at 2 i.e., 3, its mod inverse with 4 is 3 because (3 * 3) % 4 = 1 i.e., it exists. Therefore, xor = (xor ^ 3) = 2.
Hence, xor is 2.
Input: arr[] = {3, 6, 4, 5, 8}, M = 9
Output: 9
Explanation:
Initialize the value xor with 0:
For element indexed at 0 i.e., 3, its mod inverse does not exist.
For element indexed at 1 i.e., 6, its mod inverse does not exist.
For element indexed at 2 i.e., 4, its mod inverse with 9 is 7 because (4 * 7) % 9 = 1 i.e., it exists. Therefore, xor = (xor ^ 4) = 4.
For element indexed at 3 i.e., 5, its mod inverse with 9 is 2 because (5 * 2) % 9 = 1 i.e., it exists. Therefore, xor = (xor ^ 5) = 1.
For element indexed at 4 i.e., 8, its mod inverse with 9 is 8 because (8 * 8) % 9 = 1 i.e., it exists. Therefore, xor = (xor ^ 8) = 9.
Hence, xor is 9.
天真的方法:最简单的方法是打印存在j的数组中所有元素的XOR,其中(1 <= j
时间复杂度: O(N * M)
辅助空间: O(N)
高效方法:为了优化上述方法,其思想是使用以下属性:当且仅当M和X的GCD为1,即gcd(M,X)为1时,模M下存在任意数量X的模逆。 。请按照以下步骤解决问题:
- 用0初始化变量xor ,以存储在M下存在其模逆的所有元素的xor。
- 在[0,N – 1]范围内遍历数组。
- 如果 GCD(M,ARR [I])为1,则更新XOR为异或=(XOR ^ ARR [I])。
- 遍历后,将值xor作为所需结果打印。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the gcd of a & b
int gcd(int a, int b)
{
// Base Case
if (a == 0)
return b;
// Recursively calculate GCD
return gcd(b % a, a);
}
// Function to print the Bitwise XOR of
// elements of arr[] if gcd(arr[i], M) is 1
void countInverse(int arr[], int N, int M)
{
// Initialize xor
int XOR = 0;
// Traversing the array
for (int i = 0; i < N; i++) {
// GCD of M and arr[i]
int gcdOfMandelement
= gcd(M, arr[i]);
// If GCD is 1, update xor
if (gcdOfMandelement == 1) {
XOR ^= arr[i];
}
}
// Print xor
cout << XOR << ' ';
}
// Drive Code
int main()
{
// Given array arr[]
int arr[] = { 1, 2, 3 };
// Given number M
int M = 4;
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
countInverse(arr, N, M);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to return the gcd of a & b
static int gcd(int a, int b)
{
// Base Case
if (a == 0)
return b;
// Recursively calculate GCD
return gcd(b % a, a);
}
// Function to print the Bitwise XOR of
// elements of arr[] if gcd(arr[i], M) is 1
static void countInverse(int[] arr, int N, int M)
{
// Initialize xor
int XOR = 0;
// Traversing the array
for(int i = 0; i < N; i++)
{
// GCD of M and arr[i]
int gcdOfMandelement = gcd(M, arr[i]);
// If GCD is 1, update xor
if (gcdOfMandelement == 1)
{
XOR ^= arr[i];
}
}
// Print xor
System.out.println(XOR);
}
// Drive Code
public static void main(String[] args)
{
// Given array arr[]
int[] arr = { 1, 2, 3 };
// Given number M
int M = 4;
// Size of the array
int N = arr.length;
// Function Call
countInverse(arr, N, M);
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program for the above approach
# Function to return the gcd of a & b
def gcd(a, b):
# Base Case
if (a == 0):
return b
# Recursively calculate GCD
return gcd(b % a, a)
# Function to print the Bitwise XOR of
# elements of arr[] if gcd(arr[i], M) is 1
def countInverse(arr, N, M):
# Initialize xor
XOR = 0
# Traversing the array
for i in range(0, N):
# GCD of M and arr[i]
gcdOfMandelement = gcd(M, arr[i])
# If GCD is 1, update xor
if (gcdOfMandelement == 1):
XOR = XOR ^ arr[i]
# Print xor
print(XOR)
# Drive Code
if __name__ == '__main__':
# Given array arr[]
arr = [ 1, 2, 3 ]
# Given number M
M = 4
# Size of the array
N = len(arr)
# Function Call
countInverse(arr, N, M)
# This code is contributed by akhilsaini
C#
// C# program for the above approach
using System;
class GFG{
// Function to return the gcd of a & b
static int gcd(int a, int b)
{
// Base Case
if (a == 0)
return b;
// Recursively calculate GCD
return gcd(b % a, a);
}
// Function to print the Bitwise XOR of
// elements of arr[] if gcd(arr[i], M) is 1
static void countInverse(int[] arr, int N, int M)
{
// Initialize xor
int XOR = 0;
// Traversing the array
for(int i = 0; i < N; i++)
{
// GCD of M and arr[i]
int gcdOfMandelement = gcd(M, arr[i]);
// If GCD is 1, update xor
if (gcdOfMandelement == 1)
{
XOR ^= arr[i];
}
}
// Print xor
Console.WriteLine(XOR);
}
// Drive Code
public static void Main()
{
// Given array arr[]
int[] arr = { 1, 2, 3 };
// Given number M
int M = 4;
// Size of the array
int N = arr.Length;
// Function Call
countInverse(arr, N, M);
}
}
// This code is contributed by akhilsaini
Javascript
2
时间复杂度: O(N * log M)
辅助空间: O(N)