📌  相关文章
📜  检查与所有其他元素互素的数组元素

📅  最后修改于: 2021-04-22 00:46:05             🧑  作者: Mango

给定阵列的ARR正整数[]其中2≤ARR [I]≤10 6对于i的所有可能值。任务是检查给定数组中是否至少有一个元素与该数组的所有其他元素形成互质对。如果不存在这样的元素,则打印“否”,然后打印是”

例子:

天真的方法:一个简单的解决方案是检查每个元素与所有其他元素的gcd是否等于1。该解决方案的时间复杂度为O(n 2 )

高效的方法:一种有效的解决方案是在给定的数组中生成整数的所有素数。使用哈希,存储每个元素的计数,它是数组中任何数字的素数。如果该元素与其他元素不包含任何公共质数因子,则它总是与其他元素形成互质对。
为了生成素因数,请阅读文章O(log n)中使用Sieve的素因数分解。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
#define MAXN 1000001
  
// Stores smallest prime factor for every number
int spf[MAXN];
  
// Hash to store prime factors count
int hash1[MAXN] = { 0 };
  
// Function to calculate SPF (Smallest Prime Factor)
// for every number till MAXN
void sieve()
{
    spf[1] = 1;
    for (int i = 2; i < MAXN; i++)
  
        // Marking smallest prime factor for every
        // number to be itself
        spf[i] = i;
  
    // Separately marking spf for every even
    // number as 2
    for (int i = 4; i < MAXN; i += 2)
        spf[i] = 2;
  
    // Checking if i is prime
    for (int i = 3; i * i < MAXN; i++) {
  
        // Marking SPF for all numbers divisible by i
        if (spf[i] == i) {
            for (int j = i * i; j < MAXN; j += i)
  
                // Marking spf[j] if it is not
                // previously marked
                if (spf[j] == j)
                    spf[j] = i;
        }
    }
}
  
// Function to store the prime factors after dividing
// by the smallest prime factor at every step
void getFactorization(int x)
{
    int temp;
    while (x != 1) {
        temp = spf[x];
        if (x % temp == 0) {
  
            // Storing the count of
            // prime factors in hash
            hash1[spf[x]]++;
            x = x / spf[x];
        }
        while (x % temp == 0)
            x = x / temp;
    }
}
  
// Function that returns true if there are
// no common prime factors between x
// and other numbers of the array
bool check(int x)
{
    int temp;
    while (x != 1) {
        temp = spf[x];
  
        // Checking whether it common
        // prime factor with other numbers
        if (x % temp == 0 && hash1[temp] > 1)
            return false;
        while (x % temp == 0)
            x = x / temp;
    }
    return true;
}
  
// Function that returns true if there is
// an element in the array which is coprime
// with all the other elements of the array
bool hasValidNum(int arr[], int n)
{
  
    // Using sieve for generating prime factors
    sieve();
  
    for (int i = 0; i < n; i++)
        getFactorization(arr[i]);
  
    // Checking the common prime factors
    // with other numbers
    for (int i = 0; i < n; i++)
        if (check(arr[i]))
            return true;
  
    return false;
}
  
// Driver code
int main()
{
    int arr[] = { 2, 8, 4, 10, 6, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    if (hasValidNum(arr, n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
static int MAXN = 1000001;
  
// Stores smallest prime factor for every number
static int[] spf = new int[MAXN];
  
// Hash to store prime factors count
static int[] hash1 = new int[MAXN];
  
// Function to calculate SPF (Smallest Prime Factor)
// for every number till MAXN
static void sieve()
{
    spf[1] = 1;
    for (int i = 2; i < MAXN; i++)
  
        // Marking smallest prime factor for every
        // number to be itself
        spf[i] = i;
  
    // Separately marking spf for every even
    // number as 2
    for (int i = 4; i < MAXN; i += 2)
        spf[i] = 2;
  
    // Checking if i is prime
    for (int i = 3; i * i < MAXN; i++) 
    {
  
        // Marking SPF for all numbers divisible by i
        if (spf[i] == i) 
        {
            for (int j = i * i; j < MAXN; j += i)
  
                // Marking spf[j] if it is not
                // previously marked
                if (spf[j] == j)
                    spf[j] = i;
        }
    }
}
  
// Function to store the prime factors after dividing
// by the smallest prime factor at every step
static void getFactorization(int x)
{
    int temp;
    while (x != 1) 
    {
        temp = spf[x];
        if (x % temp == 0) 
        {
  
            // Storing the count of
            // prime factors in hash
            hash1[spf[x]]++;
            x = x / spf[x];
        }
        while (x % temp == 0)
            x = x / temp;
    }
}
  
// Function that returns true if there are
// no common prime factors between x
// and other numbers of the array
static boolean check(int x)
{
    int temp;
    while (x != 1) 
    {
        temp = spf[x];
  
        // Checking whether it common
        // prime factor with other numbers
        if (x % temp == 0 && hash1[temp] > 1)
            return false;
        while (x % temp == 0)
            x = x / temp;
    }
    return true;
}
  
// Function that returns true if there is
// an element in the array which is coprime
// with all the other elements of the array
static boolean hasValidNum(int []arr, int n)
{
  
    // Using sieve for generating prime factors
    sieve();
  
    for (int i = 0; i < n; i++)
        getFactorization(arr[i]);
  
    // Checking the common prime factors
    // with other numbers
    for (int i = 0; i < n; i++)
        if (check(arr[i]))
            return true;
  
    return false;
}
  
// Driver code
public static void main (String[] args) 
{
  
    int []arr = { 2, 8, 4, 10, 6, 7 };
    int n = arr.length;
  
    if (hasValidNum(arr, n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by chandan_jnu


Python3
# Python3 implementation of the approach
MAXN = 1000001
  
# Stores smallest prime factor for 
# every number
spf = [i for i in range(MAXN)]
  
# Hash to store prime factors count
hash1 = [0 for i in range(MAXN)]
  
# Function to calculate SPF (Smallest 
# Prime Factor) for every number till MAXN
def sieve():
  
    # Separately marking spf for 
    # every even number as 2
    for i in range(4, MAXN, 2):
        spf[i] = 2
  
    # Checking if i is prime
    for i in range(3, MAXN):
  
        if i * i < MAXN:
            break
  
        # Marking SPF for all numbers
        # divisible by i
        if (spf[i] == i):
            for j in range(i * i, MAXN, i):
  
                # Marking spf[j] if it is not
                # previously marked
                if (spf[j] == j):
                    spf[j] = i
  
# Function to store the prime factors 
# after dividing by the smallest prime 
# factor at every step
def getFactorization(x):
  
    while (x != 1):
        temp = spf[x]
        if (x % temp == 0):
  
            # Storing the count of
            # prime factors in hash
            hash1[spf[x]] += 1
            x = x // spf[x]
  
        while (x % temp == 0):
            x = x // temp
  
# Function that returns true if there 
# are no common prime factors between x
# and other numbers of the array
def check(x):
  
    while (x != 1):
        temp = spf[x]
  
        # Checking whether it common
        # prime factor with other numbers
        if (x % temp == 0 and hash1[temp] > 1):
            return False
        while (x % temp == 0):
            x = x //temp
      
    return True
  
# Function that returns true if there is
# an element in the array which is coprime
# with all the other elements of the array
def hasValidNum(arr, n):
  
    # Using sieve for generating 
    # prime factors
    sieve()
  
    for i in range(n):
        getFactorization(arr[i])
  
    # Checking the common prime factors
    # with other numbers
    for i in range(n):
        if (check(arr[i])):
            return True
  
    return False
  
# Driver code
arr = [2, 8, 4, 10, 6, 7]
n = len(arr)
  
if (hasValidNum(arr, n)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
static int MAXN=1000001;
  
// Stores smallest prime factor for every number
static int[] spf = new int[MAXN];
  
// Hash to store prime factors count
static int[] hash1 = new int[MAXN];
  
// Function to calculate SPF (Smallest Prime Factor)
// for every number till MAXN
static void sieve()
{
    spf[1] = 1;
    for (int i = 2; i < MAXN; i++)
  
        // Marking smallest prime factor for every
        // number to be itself
        spf[i] = i;
  
    // Separately marking spf for every even
    // number as 2
    for (int i = 4; i < MAXN; i += 2)
        spf[i] = 2;
  
    // Checking if i is prime
    for (int i = 3; i * i < MAXN; i++) 
    {
  
        // Marking SPF for all numbers divisible by i
        if (spf[i] == i) 
        {
            for (int j = i * i; j < MAXN; j += i)
  
                // Marking spf[j] if it is not
                // previously marked
                if (spf[j] == j)
                    spf[j] = i;
        }
    }
}
  
// Function to store the prime factors after dividing
// by the smallest prime factor at every step
static void getFactorization(int x)
{
    int temp;
    while (x != 1) 
    {
        temp = spf[x];
        if (x % temp == 0) 
        {
  
            // Storing the count of
            // prime factors in hash
            hash1[spf[x]]++;
            x = x / spf[x];
        }
        while (x % temp == 0)
            x = x / temp;
    }
}
  
// Function that returns true if there are
// no common prime factors between x
// and other numbers of the array
static bool check(int x)
{
    int temp;
    while (x != 1) 
    {
        temp = spf[x];
  
        // Checking whether it common
        // prime factor with other numbers
        if (x % temp == 0 && hash1[temp] > 1)
            return false;
        while (x % temp == 0)
            x = x / temp;
    }
    return true;
}
  
// Function that returns true if there is
// an element in the array which is coprime
// with all the other elements of the array
static bool hasValidNum(int []arr, int n)
{
  
    // Using sieve for generating prime factors
    sieve();
  
    for (int i = 0; i < n; i++)
        getFactorization(arr[i]);
  
    // Checking the common prime factors
    // with other numbers
    for (int i = 0; i < n; i++)
        if (check(arr[i]))
            return true;
  
    return false;
}
  
// Driver code
static void Main()
{
    int []arr = { 2, 8, 4, 10, 6, 7 };
    int n = arr.Length;
  
    if (hasValidNum(arr, n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by chandan_jnu


PHP
 1)
            return false;
        while ($x % $temp == 0)
            $x = (int)($x / $temp);
    }
    return true;
}
  
// Function that returns true if there is
// an element in the array which is coprime
// with all the other elements of the array
function hasValidNum($arr, $n)
{
    global $spf,$MAXN,$hash1;
  
    // Using sieve for generating prime factors
    sieve();
  
    for ($i = 0; $i < $n; $i++)
        getFactorization($arr[$i]);
  
    // Checking the common prime factors
    // with other numbers
    for ($i = 0; $i < $n; $i++)
        if (check($arr[$i]))
            return true;
  
    return false;
}
  
// Driver code
    $arr = array( 2, 8, 4, 10, 6, 7 );
    $n = count($arr);
  
    if (hasValidNum($arr, $n))
        echo "Yes";
    else
        echo "No";
  
// This code is contributed by chandan_jnu
?>


输出:
Yes