📌  相关文章
📜  给定 Array 中 GCD 不是素数的对的计数

📅  最后修改于: 2021-10-27 09:06:50             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是找到对的数量,使得对的最大公约数 (GCD) 不是素数。对(i, j)(j, i)被认为是相同的。

例子:

方法:给定的问题可以通过找到直到10 5 的所有素数并将它们存储在一个集合中来解决给定的问题,然后对于每对(i, j)如果它们的 GCD 不在集合中,则计算这对。请按照以下步骤解决问题:

  • 定义一个函数primeSieve()并使用 Eratosthenes 筛法查找最多10 5 的素数并将其存储在一个无序集合中,例如S
  • 初始化变量,比如count0 ,它存储对的总计数。
  • 从给定数组生成所有可能的对,如果它们的 GCD 不在集合中,则将count的值增加1
  • 执行上述步骤后,打印count的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the prime numbers
void primeSieve(bool* p)
{
    for (int i = 2; i * i <= 1000000; i++) {
 
        // If p[i] is not changed,
        // then it is a prime
        if (p[i] == true) {
 
            // Update all multiples of i
            // as non prime
            for (int j = i * 2;
                 j <= 1000000; j += i) {
                p[j] = false;
            }
        }
    }
}
 
// Function to find GCD of two integers
// a and b
int gcd(int a, int b)
{
    // Base Case
    if (b == 0)
        return a;
 
    // Find GCD Recursively
    return gcd(b, a % b);
}
 
// Function to count the number of
// pairs whose GCD is non prime
int countPairs(int arr[], int n,
               unordered_set s)
{
    // Stores the count of valid pairs
    int count = 0;
 
    // Traverse over the array arr[]
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
 
            // Calculate the GCD
            int x = gcd(arr[i], arr[j]);
 
            // Update the count
            if (s.find(x) == s.end())
                count++;
        }
    }
 
    // Return count
    return count;
}
 
// Utility Function to find all the prime
// numbers and find all the pairs
void countPairsUtil(int arr[], int n)
{
    // Stores all the prime numbers
    unordered_set s;
    bool p[1000005];
    memset(p, true, sizeof(p));
 
    // Find all the prime numbers
    primeSieve(p);
 
    s.insert(2);
 
    // Insert prime numbers in the
    // unordered set
    for (int i = 3; i <= 1000000; i += 2)
        if (p[i])
            s.insert(i);
 
    // Find the count of valid pairs
    cout << countPairs(arr, n, s);
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 9 };
    int N = sizeof(arr) / sizeof(arr[0]);
    countPairsUtil(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the prime numbers
static void primeSieve(boolean[] p)
{
    for (int i = 2; i * i <= 1000000; i++) {
 
        // If p[i] is not changed,
        // then it is a prime
        if (p[i] == true) {
 
            // Update all multiples of i
            // as non prime
            for (int j = i * 2;
                 j <= 1000000; j += i) {
                p[j] = false;
            }
        }
    }
}
 
// Function to find GCD of two integers
// a and b
static int gcd(int a, int b)
{
    // Base Case
    if (b == 0)
        return a;
 
    // Find GCD Recursively
    return gcd(b, a % b);
}
 
// Function to count the number of
// pairs whose GCD is non prime
static int countPairs(int arr[], int n,
               HashSet s)
{
    // Stores the count of valid pairs
    int count = 0;
 
    // Traverse over the array arr[]
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
 
            // Calculate the GCD
            int x = gcd(arr[i], arr[j]);
 
            // Update the count
            if (!s.contains(x))
                count++;
        }
    }
 
    // Return count
    return count;
}
 
// Utility Function to find all the prime
// numbers and find all the pairs
static void countPairsUtil(int arr[], int n)
{
    // Stores all the prime numbers
    HashSet s = new HashSet();
    boolean []p = new boolean[1000005];
    for(int i=0;i


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
// Function to find the prime numbers
static void primeSieve(bool[] p)
{
    for (int i = 2; i * i <= 1000000; i++) {
 
        // If p[i] is not changed,
        // then it is a prime
        if (p[i] == true) {
 
            // Update all multiples of i
            // as non prime
            for (int j = i * 2;
                 j <= 1000000; j += i) {
                p[j] = false;
            }
        }
    }
}
 
// Function to find GCD of two integers
// a and b
static int gcd(int a, int b)
{
    // Base Case
    if (b == 0)
        return a;
 
    // Find GCD Recursively
    return gcd(b, a % b);
}
 
// Function to count the number of
// pairs whose GCD is non prime
static int countPairs(int []arr, int n,
               HashSet s)
{
    // Stores the count of valid pairs
    int count = 0;
 
    // Traverse over the array []arr
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
 
            // Calculate the GCD
            int x = gcd(arr[i], arr[j]);
 
            // Update the count
            if (!s.Contains(x))
                count++;
        }
    }
 
    // Return count
    return count;
}
 
// Utility Function to find all the prime
// numbers and find all the pairs
static void countPairsUtil(int []arr, int n)
{
   
    // Stores all the prime numbers
    HashSet s = new HashSet();
    bool []p = new bool[1000005];
    for(int i = 0; i < p.Length; i++)
        p[i] = true;
 
    // Find all the prime numbers
    primeSieve(p);
 
    s.Add(2);
 
    // Insert prime numbers in the
    // unordered set
    for (int i = 3; i <= 1000000; i += 2)
        if (p[i])
            s.Add(i);
 
    // Find the count of valid pairs
    Console.Write(countPairs(arr, n, s));
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 2, 3, 9 };
    int N = arr.Length;
    countPairsUtil(arr, N);
 
}
}
 
// This code is contributed by 29AjayKumar


输出:
2

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

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