📌  相关文章
📜  计算给定两个数组的所有成对和的 GCD

📅  最后修改于: 2022-05-13 01:56:09.972000             🧑  作者: Mango

计算给定两个数组的所有成对和的 GCD

给定两个大小为NM的数组A[]B[]计算所有成对和的 GCD (A[i]+B[j]) 1<=i<=N 和 1<=j<=M。

例子:

朴素方法:这个问题的简单方法是计算所有成对的和,然后计算它们的 GCD。

下面是这种方法的实现。

C++
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function to calculate gcd
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to calculate the GCD
// of all pairwise sums
int calculateGCD(vector& a,
                 vector& b,
                 int N, int M)
{
    int ans = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
 
            // Pairwise sum of all elements
            int sum = a[i] + b[j];
 
            // Finding gcd of the elements
            ans = gcd(ans, sum);
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    int N = 4, M = 3;
    // Initialization of the vector
    vector A = { 1, 7, 25, 55 };
    vector B = { 1, 3, 5 };
 
    // output
    cout << calculateGCD(A, B, N, M);
    return 0;
}


Java
// Java code to implement the approach
import java.util.*;
 
class GFG {
 
  // Function to calculate gcd
  static int gcd(int a, int b)
  {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }
 
  // Function to calculate the GCD
  // of all pairwise sums
  static int calculateGCD(int a[],
                          int b[],
                          int N, int M)
  {
    int ans = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
 
        // Pairwise sum of all elements
        int sum = a[i] + b[j];
 
        // Finding gcd of the elements
        ans = gcd(ans, sum);
      }
    }
    return ans;
  }
 
  // Driver code
  public static void main (String[] args) {
    int N = 4, M = 3;
 
    // Initialization of the vector
    int A[] = { 1, 7, 25, 55 };
    int B[] = { 1, 3, 5 };
 
    // output
    System.out.print(calculateGCD(A, B, N, M));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code to implement the approach
# Function to calculate gcd
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)
   
# Function to calculate the GCD
# of all pairwise sums
def calculateGCD(a, b, N, M):
    ans = 0
    for i in range(N):
        for j in range(M):
           
            # Pairwise sum of all elements
            sum = a[i]+b[j]
             
            # Finding gcd of the elements
            ans = gcd(ans, sum)
    return ans
 
# Driver code
N = 4
M = 3
A = [1, 7, 25, 55]
B = [1, 3, 5]
print(calculateGCD(A, B, N, M))
 
'''This Code is contributed by Rajat Kumar'''


C#
// C# code to implement the approach
using System;
using System.Numerics;
using System.Collections.Generic;
 
public class GFG {
 
  // Function to calculate gcd
  static int gcd(int a, int b)
  {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }
 
  // Function to calculate the GCD
  // of all pairwise sums
  static int calculateGCD(int[] a, int[] b, int N, int M)
  {
    int ans = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
 
        // Pairwise sum of all elements
        int sum = a[i] + b[j];
 
        // Finding gcd of the elements
        ans = gcd(ans, sum);
      }
    }
    return ans;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int N = 4, M = 3;
     
    // Initialization of the vector
    int[] A = { 1, 7, 25, 55 };
    int[] B = { 1, 3, 5 };
 
    // output
    Console.WriteLine(calculateGCD(A, B, N, M));
  }
}
 
// This code is contributed by phasing17


Javascript


C++
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function for calculating gcd
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to calculate the required GCD
int calculateGCD(vector& a,
                 vector& b, int N, int M)
{
    int ans = 0;
 
    // Sorting the arrays
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
 
    // Calculating the gcd of all the elements
    // of type (i, j) i>0 and j>=0
    // Using the property
    // gcd(a[0]+b[j], a[i]+b[j])
    // =gcd(a[0]+b[j], a[i]-a[0])
    for (int i = 1; i < N; i++) {
        ans = gcd(ans, a[i] - a[0]);
    }
 
    // Calculating the gcd of the remaining
    // elements of the type (a[0]+b[j])
    for (int i = 0; i < M; i++) {
        ans = gcd(ans, a[0] + b[i]);
    }
    return ans;
}
 
// Driver code
int main()
{
    int N = 4, M = 3;
    // Initialization of the array
    vector A = { 1, 7, 25, 55 };
    vector B = { 1, 3, 5 };
 
    // Function call
    cout << calculateGCD(A, B, N, M);
    return 0;
}


Python3
# Python3 code to implement the approach
 
# Function for calculating gcd
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)
 
# Function to calculate the required GCD
def calculateGCD(a, b, N, M):
    ans = 0
     
    # sorting the arrays
    a.sort()
    b.sort()
     
    # calculating the gcd of all the elements
    # of type (i, j) i>0 and j>=0
    # Using the property
    # gcd(a[0]+b[j], a[i]+b[j])
    # =gcd(a[0]+b[j], a[i]-a[0])
    for i in range(1, N):
        ans = gcd(ans, a[i] - a[0])
         
    # Calculating the gcd of the remaining
    # elements of the type (a[0]+b[j])
    for i in range(M):
        ans = gcd(ans, a[0] + b[i])
    return ans
 
# Driver code
N, M = 4, 3
A = [1, 7, 25, 55]
B = [1, 3, 5]
 
# function all
print(calculateGCD(A, B, N, M))
 
# This code is contributed by phasing17.


输出
2

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

有效方法:可以根据以下数学观察有效地解决问题:

观察

请按照以下步骤解决此问题:

  • 首先对两个数组进行排序。
  • 然后遍历i = 1 到 N-1并计算所有 A[i] -A[0] 对的 GCD(比如X )。
  • 然后遍历i = 1 到 M-1并计算所有 A[0] +B[i] 对的 GCD(比如Y )。
  • 返回最终答案,即 (X 和 Y) 的 GCD。

下面是这种方法的实现。

C++

// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function for calculating gcd
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to calculate the required GCD
int calculateGCD(vector& a,
                 vector& b, int N, int M)
{
    int ans = 0;
 
    // Sorting the arrays
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
 
    // Calculating the gcd of all the elements
    // of type (i, j) i>0 and j>=0
    // Using the property
    // gcd(a[0]+b[j], a[i]+b[j])
    // =gcd(a[0]+b[j], a[i]-a[0])
    for (int i = 1; i < N; i++) {
        ans = gcd(ans, a[i] - a[0]);
    }
 
    // Calculating the gcd of the remaining
    // elements of the type (a[0]+b[j])
    for (int i = 0; i < M; i++) {
        ans = gcd(ans, a[0] + b[i]);
    }
    return ans;
}
 
// Driver code
int main()
{
    int N = 4, M = 3;
    // Initialization of the array
    vector A = { 1, 7, 25, 55 };
    vector B = { 1, 3, 5 };
 
    // Function call
    cout << calculateGCD(A, B, N, M);
    return 0;
}

Python3

# Python3 code to implement the approach
 
# Function for calculating gcd
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)
 
# Function to calculate the required GCD
def calculateGCD(a, b, N, M):
    ans = 0
     
    # sorting the arrays
    a.sort()
    b.sort()
     
    # calculating the gcd of all the elements
    # of type (i, j) i>0 and j>=0
    # Using the property
    # gcd(a[0]+b[j], a[i]+b[j])
    # =gcd(a[0]+b[j], a[i]-a[0])
    for i in range(1, N):
        ans = gcd(ans, a[i] - a[0])
         
    # Calculating the gcd of the remaining
    # elements of the type (a[0]+b[j])
    for i in range(M):
        ans = gcd(ans, a[0] + b[i])
    return ans
 
# Driver code
N, M = 4, 3
A = [1, 7, 25, 55]
B = [1, 3, 5]
 
# function all
print(calculateGCD(A, B, N, M))
 
# This code is contributed by phasing17.
输出
2

时间复杂度:O(N*logN + M*logM)
辅助空间:O(1)