📌  相关文章
📜  使用不同素数因子的偶数乘积计算数组中的对

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

给定两个分别由NM 个整数组成的数组A[]B[] ,任务是计算对(A[i], B[j])的数量,使得它们的不同质因子的计数的乘积是偶数。

例子:

朴素的方法:最简单的方法是从两个数组中生成所有可能的对(A[i], B[j]) ,对于每一对,计算数组元素的不同质因子的数量并检查它们的乘积是否为偶数或不是。如果发现为真,则增加此类对的计数。

时间复杂度: O(N 5/2 )
辅助空间: O(1)

有效方法:可以通过预先计算两个数组中直到最大元素的所有数字的不同质因数的计数来优化上述方法,并使用以下两个数字的乘积属性:

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

  • 首先,计算直到 MAX 的所有数字的不同质因数并将其存储在 vector 中,比如countDistinct。
  • 初始化两个变量,例如evenCountoddCount,以存储元素的计数,其中元素的计数具有B[] 中数组元素的不同质因子的偶数和奇数。
  • 遍历数组B[]。如果countDistinct[B[i]] = 0 ,则跳过此步骤。如果是奇数,则将oddCount加1。否则,将evenCount加1。
  • 将变量evenPairs初始化为0
  • 通过evenCount遍历数组A []和增量evenPairs如果countDistinct [A [I]]为奇数。
  • 否则,增量evenPairs通过evenCount + oddCount。
  • 打印evenPairs的值

下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
 
#include 
using namespace std;
#define MAX 1000000
 
// Function to calculate count of
// distinct prime factors of a number
void countOfPrimefactors(vector& CountDistinct)
{
    bool prime[MAX + 1];
 
    for (int i = 0; i <= MAX; i++) {
        CountDistinct[i] = 0;
        prime[i] = true;
    }
 
    // Sieve of Eratosthenes
    for (long long int i = 2; i <= MAX; i++) {
 
        if (prime[i] == true) {
            CountDistinct[i] = 1;
 
            for (long long int j = i * 2; j <= MAX;
                 j += i) {
 
                CountDistinct[j]++;
                prime[j] = false;
            }
        }
    }
}
 
// Function to count pairs with even
// product of distinct prime factors
int CountEvenPair(int A[], int B[], int N, int M)
{
    // Stores count of
    // distinct prime factors
    vector countDistinct(MAX + 1);
 
    countOfPrimefactors(countDistinct);
 
    // Stores the count of numbers
    // with even prime factors in B[]
    int evenCount = 0;
 
    // Stores the count of numbers
    // with odd prime factors in B[]
    int oddCount = 0;
 
    // Even Product Pairs
    int evenPairs = 0;
 
    // Traverse the array B[]
    for (int i = 0; i < M; i++) {
 
        // Since, product has to be
        // positive i.e > 0
 
        if (countDistinct[B[i]] == 0)
            continue;
 
        // If count of prime factors is odd
        if (countDistinct[B[i]] & 1) {
 
            // Increment oddCount by 1
            oddCount++;
        }
        else {
 
            // Increment evenCount by 1
            evenCount++;
        }
    }
 
    for (int i = 0; i < N; i++) {
 
        // Since, product has to be
        // positive i.e > 0
 
        if (countDistinct[A[i]] == 0)
            continue;
 
        // If count of prime factors is odd
        if (countDistinct[A[i]] & 1) {
 
            // odd * even = even
            evenPairs += (evenCount);
        }
 
        // If count of prime factors is even
        else {
 
            // even * odd = even
            // even * even = even
            evenPairs += evenCount + oddCount;
        }
    }
 
    return evenPairs;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 2, 3 };
    int B[] = { 4, 5, 6 };
 
    int N = sizeof(A) / sizeof(A[0]);
    int M = sizeof(B) / sizeof(B[0]);
 
    cout << CountEvenPair(A, B, N, M);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
  static int MAX = 1000000;
 
  // Function to calculate count of
  // distinct prime factors of a number
  static void countOfPrimefactors(int[] CountDistinct)
  {
    boolean[]  prime = new boolean[MAX + 1];
 
    for (int i = 0; i <= MAX; i++) {
      CountDistinct[i] = 0;
      prime[i] = true;
    }
 
    // Sieve of Eratosthenes
    for (int i = 2; i <= MAX; i++) {
 
      if (prime[i] == true) {
        CountDistinct[i] = 1;
 
        for (int j = i * 2; j <= MAX;
             j += i) {
 
          CountDistinct[j]++;
          prime[j] = false;
        }
      }
    }
  }
 
  // Function to count pairs with even
  // product of distinct prime factors
  static int CountEvenPair(int A[], int B[], int N, int M)
  {
    // Stores count of
    // distinct prime factors
    int[] countDistinct = new int[(MAX + 1)];
 
    countOfPrimefactors(countDistinct);
 
    // Stores the count of numbers
    // with even prime factors in B[]
    int evenCount = 0;
 
    // Stores the count of numbers
    // with odd prime factors in B[]
    int oddCount = 0;
 
    // Even Product Pairs
    int evenPairs = 0;
 
    // Traverse the array B[]
    for (int i = 0; i < M; i++) {
 
      // Since, product has to be
      // positive i.e > 0
 
      if (countDistinct[B[i]] == 0)
        continue;
 
      // If count of prime factors is odd
      if ((countDistinct[B[i]] & 1) != 0) {
 
        // Increment oddCount by 1
        oddCount++;
      }
      else {
 
        // Increment evenCount by 1
        evenCount++;
      }
    }
 
    for (int i = 0; i < N; i++) {
 
      // Since, product has to be
      // positive i.e > 0
 
      if (countDistinct[A[i]] == 0)
        continue;
 
      // If count of prime factors is odd
      if ((countDistinct[A[i]] & 1) != 0) {
 
        // odd * even = even
        evenPairs += (evenCount);
      }
 
      // If count of prime factors is even
      else {
 
        // even * odd = even
        // even * even = even
        evenPairs += evenCount + oddCount;
      }
    }
    return evenPairs;
  }
 
 
  // Driver Code
  public static void main(String[] args)
  {
    int A[] = { 1, 2, 3 };
    int B[] = { 4, 5, 6 };
 
    int N = A.length;
    int M = B.length;
 
    System.out.println(CountEvenPair(A, B, N, M));
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# Python 3 implementation of
# the above approach
MAX = 1000000
 
# Function to calculate count of
# distinct prime factors of a number
def countOfPrimefactors(CountDistinct):
    global MAX
    prime = [0 for i in range(MAX + 1)]
 
    for i in range(MAX+1):
        CountDistinct[i] = 0
        prime[i] = True
 
    # Sieve of Eratosthenes
    for i in range(2,MAX+1,1):
        if (prime[i] == True):
            CountDistinct[i] = 1
 
            for j in range(i * 2,MAX+1,i):
                CountDistinct[j] += 1
                prime[j] = False
             
# Function to count pairs with even
# product of distinct prime factors
def CountEvenPair(A, B, N, M):
    global MAX
     
    # Stores count of
    # distinct prime factors
    countDistinct = [0 for i in range(MAX + 1)]
    countOfPrimefactors(countDistinct)
 
    # Stores the count of numbers
    # with even prime factors in B[]
    evenCount = 0
 
    # Stores the count of numbers
    # with odd prime factors in B[]
    oddCount = 0
 
    # Even Product Pairs
    evenPairs = 0
 
    # Traverse the array B[]
    for i in range(M):
       
        # Since, product has to be
        # positive i.e > 0
        if (countDistinct[B[i]] == 0):
            continue
 
        # If count of prime factors is odd
        if (countDistinct[B[i]] & 1):
           
            # Increment oddCount by 1
            oddCount += 1
         
        else:
            # Increment evenCount by 1
            evenCount += 1
 
    for i in range(N):
       
        # Since, product has to be
        # positive i.e > 0
 
        if (countDistinct[A[i]] == 0):
            continue
 
        # If count of prime factors is odd
        if (countDistinct[A[i]] & 1):
           
            # odd * even = even
            evenPairs += (evenCount)
 
        # If count of prime factors is even
        else:
            # even * odd = even
            # even * even = even
            evenPairs += evenCount + oddCount
 
    return evenPairs
 
# Driver Code
if __name__ == '__main__':
    A =  [1, 2, 3]
    B =  [4, 5, 6]
    N = len(A)
    M = len(B)
    print(CountEvenPair(A, B, N, M))
      
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
public class GFG
{
 
  static int MAX = 1000000;
 
  // Function to calculate count of
  // distinct prime factors of a number
  static void countOfPrimefactors(int[] CountDistinct)
  {
    bool[]  prime = new bool[MAX + 1];
 
    for (int i = 0; i <= MAX; i++) {
      CountDistinct[i] = 0;
      prime[i] = true;
    }
 
    // Sieve of Eratosthenes
    for (int i = 2; i <= MAX; i++) {
 
      if (prime[i] == true) {
        CountDistinct[i] = 1;
 
        for (int j = i * 2; j <= MAX;
             j += i) {
 
          CountDistinct[j]++;
          prime[j] = false;
        }
      }
    }
  }
 
  // Function to count pairs with even
  // product of distinct prime factors
  static int CountEvenPair(int []A, int []B, int N, int M)
  {
     
    // Stores count of
    // distinct prime factors
    int[] countDistinct = new int[(MAX + 1)];
    countOfPrimefactors(countDistinct);
 
    // Stores the count of numbers
    // with even prime factors in B[]
    int evenCount = 0;
 
    // Stores the count of numbers
    // with odd prime factors in B[]
    int oddCount = 0;
 
    // Even Product Pairs
    int evenPairs = 0;
 
    // Traverse the array B[]
    for (int i = 0; i < M; i++) {
 
      // Since, product has to be
      // positive i.e > 0
      if (countDistinct[B[i]] == 0)
        continue;
 
      // If count of prime factors is odd
      if ((countDistinct[B[i]] & 1) != 0) {
 
        // Increment oddCount by 1
        oddCount++;
      }
      else {
 
        // Increment evenCount by 1
        evenCount++;
      }
    }
 
    for (int i = 0; i < N; i++) {
 
      // Since, product has to be
      // positive i.e > 0
 
      if (countDistinct[A[i]] == 0)
        continue;
 
      // If count of prime factors is odd
      if ((countDistinct[A[i]] & 1) != 0) {
 
        // odd * even = even
        evenPairs += (evenCount);
      }
 
      // If count of prime factors is even
      else {
 
        // even * odd = even
        // even * even = even
        evenPairs += evenCount + oddCount;
      }
    }
    return evenPairs;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int []A = { 1, 2, 3 };
    int []B = { 4, 5, 6 };
 
    int N = A.Length;
    int M = B.Length;
 
    Console.WriteLine(CountEvenPair(A, B, N, M));
  }
}
 
// This code is contributed by AnkThon


Javascript


输出:
2

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

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