📌  相关文章
📜  存在与给定数模逆的数组元素的异或

📅  最后修改于: 2021-10-26 06:35:17             🧑  作者: Mango

给定一个长度为N的数组arr[]和一个正整数M ,任务是找到与M模逆存在的所有数组元素的按位异或。

例子:

朴素的方法最简单的方法是打印数组中存在任何j的所有元素的异或,其中(1 <= j < M)使得(arr[i] * j) % M = 1 where 0 ≤我 < N

时间复杂度: O(N * M)
辅助空间: O(N)

高效方法:为了优化上述方法,这个想法是使用属性,任何数量的X的下模M中的模逆存在当且仅当M和X的GCD是1即满足gcd(M,X)1 .请按照以下步骤解决问题:

  1. 0初始化一个变量xor ,以存储M下存在模逆的所有元素的异或。
  2. [0, N – 1]范围内遍历数组。
  3. 如果 GCD(M,ARR [I])1,则更新XOR异或=(XOR ^ ARR [I])。
  4. 遍历后,将值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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程