📜  N个数的乘积的除数

📅  最后修改于: 2021-04-22 02:33:11             🧑  作者: Mango

给定整数数组arr [] ,任务是计算给定数组中所有元素的乘积除数。
例子:

一个简单的解决方案是将所有N个整数相乘并计算乘积的除数。但是,如果乘积超过10 7,那么我们将无法使用此方法,因为使用筛分方法无法有效地对大于10 ^ 7的数进行质因子分解。
一个有效的解决方案不涉及所有数字乘积的计算。我们已经知道,当我们将两个数字相乘时,乘幂就会增加。例如,

因此,要计算除数的数量,主要重点是遇到的素数。因此,我们将只强调产品中遇到的素数,而不必理会产品本身。在遍历数组时,我们会保留遇到的每个素数的计数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#define MAX 10000002
 
using namespace std;
int prime[MAX];
 
// Array to store count of primes
int prime_count[MAX];
 
// Function to store smallest prime factor
// of every number till MAX
void sieve()
{
    memset(prime, 0, sizeof(prime));
    prime[0] = prime[1] = 1;
    for (int i = 2; i * i < MAX; i++) {
        if (prime[i] == 0) {
            for (int j = i * 2; j < MAX; j += i) {
                if (prime[j] == 0)
                    prime[j] = i;
            }
        }
    }
    for (int i = 2; i < MAX; i++) {
 
        // If the number is prime then it's
        // smallest prime factor is the number
        // itself
        if (prime[i] == 0)
            prime[i] = i;
    }
}
 
// Function to return the count of the divisors for
// the product of all the numbers from the array
long long numberOfDivisorsOfProduct(const int* arr,
                                           int n)
{
    memset(prime_count, 0, sizeof(prime_count));
 
    for (int i = 0; i < n; i++) {
        int temp = arr[i];
        while (temp != 1) {
 
            // Increase the count of prime
            // encountered
            prime_count[prime[temp]]++;
            temp = temp / prime[temp];
        }
    }
 
    long long ans = 1;
 
    // Multiplying the count of primes
    // encountered
    for (int i = 2; i < MAX; i++) {
        ans = ans * (prime_count[i] + 1);
    }
 
    return ans;
}
 
// Driver code
int main()
{
    sieve();
    int arr[] = { 2, 4, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << numberOfDivisorsOfProduct(arr, n);
    return 0;
}


Java
// Java implementation of the approach
 
import java.util.Arrays;
 
// Java implementation of the approach
class GFG {
 
    final static int MAX = 10000002;
 
    static int prime[] = new int[MAX];
 
// Array to store count of primes
    static int prime_count[] = new int[MAX];
 
// Function to store smallest prime factor
// of every number till MAX
    static void sieve() {
        Arrays.fill(prime, 0, MAX, 0);
        prime[0] = prime[1] = 1;
        for (int i = 2; i * i < MAX; i++) {
            if (prime[i] == 0) {
                for (int j = i * 2; j < MAX; j += i) {
                    if (prime[j] == 0) {
                        prime[j] = i;
                    }
                }
            }
        }
        for (int i = 2; i < MAX; i++) {
 
            // If the number is prime then it's
            // smallest prime factor is the number
            // itself
            if (prime[i] == 0) {
                prime[i] = i;
            }
        }
    }
 
// Function to return the count of the divisors for
// the product of all the numbers from the array
    static long numberOfDivisorsOfProduct(int[] arr,
            int n) {
        Arrays.fill(prime_count, 0, MAX, 0);
 
        for (int i = 0; i < n; i++) {
            int temp = arr[i];
            while (temp != 1) {
 
                // Increase the count of prime
                // encountered
                prime_count[prime[temp]]++;
                temp = temp / prime[temp];
            }
        }
 
        long ans = 1;
 
        // Multiplying the count of primes
        // encountered
        for (int i = 2; i < MAX; i++) {
            ans = ans * (prime_count[i] + 1);
        }
 
        return ans;
    }
 
// Driver code
    public static void main(String[] args) {
        sieve();
        int arr[] = {2, 4, 6};
        int n = arr.length;
        System.out.println(numberOfDivisorsOfProduct(arr, n));
 
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
MAX = 10000002
prime = [0] * (MAX)
MAX_sqrt = int(MAX ** (0.5))
 
# Array to store count of primes
prime_count = [0] * (MAX)
 
# Function to store smallest prime
# factor in prime[]
def sieve():
 
    prime[0], prime[1] = 1, 1
    for i in range(2, MAX_sqrt):
        if prime[i] == 0:
            for j in range(i * 2, MAX, i):
                if prime[j] == 0:
                    prime[j] = i
     
    for i in range(2, MAX):
 
        # If the number is prime then it's
        # the smallest prime factor is the
        # number itself
        if prime[i] == 0:
            prime[i] = i
 
# Function to return the count of the divisors for
# the product of all the numbers from the array
def numberOfDivisorsOfProduct(arr, n):
 
    for i in range(0, n):
        temp = arr[i]
        while temp != 1:
 
            # Increase the count of prime
            # encountered
            prime_count[prime[temp]] += 1
            temp = temp // prime[temp]
 
    ans = 1
 
    # Multiplying the count of primes
    # encountered
    for i in range(2, len(prime_count)):
        ans = ans * (prime_count[i] + 1)
     
    return ans
 
# Driver code
if __name__ == "__main__":
 
    sieve()
    arr = [2, 4, 6]
    n = len(arr)
    print(numberOfDivisorsOfProduct(arr, n))
 
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
public class GFG {
 
    static int MAX = 1000000;
 
    static int []prime = new int[MAX];
 
// Array to store count of primes
    static int []prime_count = new int[MAX];
 
// Function to store smallest prime factor
// of every number till MAX
    static void sieve() { 
        for(int i =0;i


C++
// C++ implementation of the approach
#include 
#define MAX 10000002
 
using namespace std;
int prime[MAX];
 
// Map to store count of primes
unordered_map prime_count;
 
// Function to store smallest prime factor
// in prime[]
void sieve()
{
    memset(prime, 0, sizeof(prime));
    prime[0] = prime[1] = 1;
    for (int i = 2; i * i < MAX; i++) {
        if (prime[i] == 0) {
            for (int j = i * 2; j < MAX; j += i) {
                if (prime[j] == 0)
                    prime[j] = i;
            }
        }
    }
    for (int i = 2; i < MAX; i++) {
 
        // If the number is prime then
        // it's the smallest prime factor
        // is the number itself
        if (prime[i] == 0)
            prime[i] = i;
    }
}
 
// Function to return the count of the divisors for
// the product of all the numbers from the array
long long numberOfDivisorsOfProduct(const int* arr,
                                            int n)
{
 
    for (int i = 0; i < n; i++) {
        int temp = arr[i];
        while (temp != 1) {
 
            // Increase the count of prime
            // encountered
            prime_count[prime[temp]]++;
            temp = temp / prime[temp];
        }
    }
 
    long long ans = 1;
 
    // Multiplying the count of primes
    // encountered
    unordered_map::iterator it;
    for (it = prime_count.begin();
         it != prime_count.end(); it++) {
        ans = ans * (it->second + 1);
    }
 
    return ans;
}
 
// Driver code
int main()
{
    sieve();
    int arr[] = { 3, 5, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << numberOfDivisorsOfProduct(arr, n);
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static int MAX = 10000002;
static int []prime = new int[MAX];
 
// Map to store count of primes
static Map prime_count = new HashMap<>();
 
// Function to store smallest prime factor
// in prime[]
static void sieve()
{
    prime[0] = 1;
    prime[1] = 1;
    for (int i = 2; i * i < MAX; i++)
    {
        if (prime[i] == 0)
        {
            for (int j = i * 2; j < MAX; j += i)
            {
                if (prime[j] == 0)
                    prime[j] = i;
            }
        }
    }
    for (int i = 2; i < MAX; i++)
    {
 
        // If the number is prime then
        // it's the smallest prime factor
        // is the number itself
        if (prime[i] == 0)
            prime[i] = i;
    }
}
 
// Function to return the count of the divisors for
// the product of all the numbers from the array
static long numberOfDivisorsOfProduct(int arr[],
                                            int n)
{
 
    for (int i = 0; i < n; i++)
    {
        int temp = arr[i];
        while (temp != 1)
        {
 
            // Increase the count of prime
            // encountered
            if(!prime_count.containsKey(prime[temp]))
            {
                prime_count.put(prime[temp], 0);   
            }
            prime_count.put(prime[temp], prime_count.get(prime[temp]) + 1);
            temp = temp / prime[temp];
        }
    }
    long ans = 1;
 
    // Multiplying the count of primes
    // encountered
    for(Map.Entry it : prime_count.entrySet())
    {
        ans = ans * (it.getValue() + 1);
    }
    return ans;
}
 
// Driver code
public static void main(String []args)
{
    sieve();
    int arr[] = new int[] { 3, 5, 7 };
    int n = arr.length;
    System.out.print(numberOfDivisorsOfProduct(arr, n));
}
}
 
// This code is contributed by rutvik_56.


Python3
# Python3 implementation of the approach
from collections import defaultdict
 
MAX = 10000002
prime = [0] * (MAX)
MAX_sqrt = int(MAX ** (0.5))
 
# Map to store count of primes
prime_count = defaultdict(lambda:0)
 
# Function to store smallest prime
# factor in prime[]
def sieve():
 
    prime[0], prime[1] = 1, 1
    for i in range(2, MAX_sqrt):
        if prime[i] == 0:
            for j in range(i * 2, MAX, i):
                if prime[j] == 0:
                    prime[j] = i
     
    for i in range(2, MAX):
 
        # If the number is prime then
        # it's the smallest prime factor
        # is the number itself
        if prime[i] == 0:
            prime[i] = i
 
# Function to return the count of the divisors for
# the product of all the numbers from the array
def numberOfDivisorsOfProduct(arr, n):
 
    for i in range(0, n):
        temp = arr[i]
        while temp != 1:
 
            # Increase the count of prime
            # encountered
            prime_count[prime[temp]] += 1
            temp = temp // prime[temp]
 
    ans = 1
 
    # Multiplying the count of primes
    # encountered
    for key in prime_count:
        ans = ans * (prime_count[key] + 1)
     
    return ans
 
# Driver code
if __name__ == "__main__":
 
    sieve()
    arr = [3, 5, 7]
    n = len(arr)
    print(numberOfDivisorsOfProduct(arr, n))
 
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
 
  static int MAX = 10000002;
  static int []prime = new int[MAX];
 
  // Map to store count of primes
  static Dictionary prime_count = new Dictionary();
 
  // Function to store smallest prime factor
  // in prime[]
  static void sieve()
  {
    prime[0] = 1;
    prime[1] = 1;
    for (int i = 2; i * i < MAX; i++)
    {
      if (prime[i] == 0)
      {
        for (int j = i * 2; j < MAX; j += i)
        {
          if (prime[j] == 0)
            prime[j] = i;
        }
      }
    }
    for (int i = 2; i < MAX; i++)
    {
 
      // If the number is prime then
      // it's the smallest prime factor
      // is the number itself
      if (prime[i] == 0)
        prime[i] = i;
    }
  }
 
  // Function to return the count of the divisors for
  // the product of all the numbers from the array
  static long numberOfDivisorsOfProduct(int []arr,
                                        int n)
  {
 
    for (int i = 0; i < n; i++)
    {
      int temp = arr[i];
      while (temp != 1)
      {
 
        // Increase the count of prime
        // encountered
        if(!prime_count.ContainsKey(prime[temp]))
        {
          prime_count[prime[temp]] = 0;   
        }
        prime_count[prime[temp]] += 1;   
        temp = temp / prime[temp];
      }
    }
    long ans = 1;
 
    // Multiplying the count of primes
    // encountered
    foreach(KeyValuePair it in prime_count)
    {
      ans = ans * (it.Value + 1);
    }
    return ans;
  }
 
  // Driver code
  public static void Main(string []args)
  {
    sieve();
    int []arr = new int[] { 3, 5, 7 };
    int n = arr.Length;
    Console.Write(numberOfDivisorsOfProduct(arr, n));
  }
}
 
// This code is contributed by pratham76.


输出:
10

内存有效的方法中,可以用无序映射替换数组,以仅存储遇到的素数的计数。
下面是内存有效方法的实现:

C++

// C++ implementation of the approach
#include 
#define MAX 10000002
 
using namespace std;
int prime[MAX];
 
// Map to store count of primes
unordered_map prime_count;
 
// Function to store smallest prime factor
// in prime[]
void sieve()
{
    memset(prime, 0, sizeof(prime));
    prime[0] = prime[1] = 1;
    for (int i = 2; i * i < MAX; i++) {
        if (prime[i] == 0) {
            for (int j = i * 2; j < MAX; j += i) {
                if (prime[j] == 0)
                    prime[j] = i;
            }
        }
    }
    for (int i = 2; i < MAX; i++) {
 
        // If the number is prime then
        // it's the smallest prime factor
        // is the number itself
        if (prime[i] == 0)
            prime[i] = i;
    }
}
 
// Function to return the count of the divisors for
// the product of all the numbers from the array
long long numberOfDivisorsOfProduct(const int* arr,
                                            int n)
{
 
    for (int i = 0; i < n; i++) {
        int temp = arr[i];
        while (temp != 1) {
 
            // Increase the count of prime
            // encountered
            prime_count[prime[temp]]++;
            temp = temp / prime[temp];
        }
    }
 
    long long ans = 1;
 
    // Multiplying the count of primes
    // encountered
    unordered_map::iterator it;
    for (it = prime_count.begin();
         it != prime_count.end(); it++) {
        ans = ans * (it->second + 1);
    }
 
    return ans;
}
 
// Driver code
int main()
{
    sieve();
    int arr[] = { 3, 5, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << numberOfDivisorsOfProduct(arr, n);
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
class GFG
{
static int MAX = 10000002;
static int []prime = new int[MAX];
 
// Map to store count of primes
static Map prime_count = new HashMap<>();
 
// Function to store smallest prime factor
// in prime[]
static void sieve()
{
    prime[0] = 1;
    prime[1] = 1;
    for (int i = 2; i * i < MAX; i++)
    {
        if (prime[i] == 0)
        {
            for (int j = i * 2; j < MAX; j += i)
            {
                if (prime[j] == 0)
                    prime[j] = i;
            }
        }
    }
    for (int i = 2; i < MAX; i++)
    {
 
        // If the number is prime then
        // it's the smallest prime factor
        // is the number itself
        if (prime[i] == 0)
            prime[i] = i;
    }
}
 
// Function to return the count of the divisors for
// the product of all the numbers from the array
static long numberOfDivisorsOfProduct(int arr[],
                                            int n)
{
 
    for (int i = 0; i < n; i++)
    {
        int temp = arr[i];
        while (temp != 1)
        {
 
            // Increase the count of prime
            // encountered
            if(!prime_count.containsKey(prime[temp]))
            {
                prime_count.put(prime[temp], 0);   
            }
            prime_count.put(prime[temp], prime_count.get(prime[temp]) + 1);
            temp = temp / prime[temp];
        }
    }
    long ans = 1;
 
    // Multiplying the count of primes
    // encountered
    for(Map.Entry it : prime_count.entrySet())
    {
        ans = ans * (it.getValue() + 1);
    }
    return ans;
}
 
// Driver code
public static void main(String []args)
{
    sieve();
    int arr[] = new int[] { 3, 5, 7 };
    int n = arr.length;
    System.out.print(numberOfDivisorsOfProduct(arr, n));
}
}
 
// This code is contributed by rutvik_56.

Python3

# Python3 implementation of the approach
from collections import defaultdict
 
MAX = 10000002
prime = [0] * (MAX)
MAX_sqrt = int(MAX ** (0.5))
 
# Map to store count of primes
prime_count = defaultdict(lambda:0)
 
# Function to store smallest prime
# factor in prime[]
def sieve():
 
    prime[0], prime[1] = 1, 1
    for i in range(2, MAX_sqrt):
        if prime[i] == 0:
            for j in range(i * 2, MAX, i):
                if prime[j] == 0:
                    prime[j] = i
     
    for i in range(2, MAX):
 
        # If the number is prime then
        # it's the smallest prime factor
        # is the number itself
        if prime[i] == 0:
            prime[i] = i
 
# Function to return the count of the divisors for
# the product of all the numbers from the array
def numberOfDivisorsOfProduct(arr, n):
 
    for i in range(0, n):
        temp = arr[i]
        while temp != 1:
 
            # Increase the count of prime
            # encountered
            prime_count[prime[temp]] += 1
            temp = temp // prime[temp]
 
    ans = 1
 
    # Multiplying the count of primes
    # encountered
    for key in prime_count:
        ans = ans * (prime_count[key] + 1)
     
    return ans
 
# Driver code
if __name__ == "__main__":
 
    sieve()
    arr = [3, 5, 7]
    n = len(arr)
    print(numberOfDivisorsOfProduct(arr, n))
 
# This code is contributed by Rituraj Jain

C#

// C# implementation of the approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
 
  static int MAX = 10000002;
  static int []prime = new int[MAX];
 
  // Map to store count of primes
  static Dictionary prime_count = new Dictionary();
 
  // Function to store smallest prime factor
  // in prime[]
  static void sieve()
  {
    prime[0] = 1;
    prime[1] = 1;
    for (int i = 2; i * i < MAX; i++)
    {
      if (prime[i] == 0)
      {
        for (int j = i * 2; j < MAX; j += i)
        {
          if (prime[j] == 0)
            prime[j] = i;
        }
      }
    }
    for (int i = 2; i < MAX; i++)
    {
 
      // If the number is prime then
      // it's the smallest prime factor
      // is the number itself
      if (prime[i] == 0)
        prime[i] = i;
    }
  }
 
  // Function to return the count of the divisors for
  // the product of all the numbers from the array
  static long numberOfDivisorsOfProduct(int []arr,
                                        int n)
  {
 
    for (int i = 0; i < n; i++)
    {
      int temp = arr[i];
      while (temp != 1)
      {
 
        // Increase the count of prime
        // encountered
        if(!prime_count.ContainsKey(prime[temp]))
        {
          prime_count[prime[temp]] = 0;   
        }
        prime_count[prime[temp]] += 1;   
        temp = temp / prime[temp];
      }
    }
    long ans = 1;
 
    // Multiplying the count of primes
    // encountered
    foreach(KeyValuePair it in prime_count)
    {
      ans = ans * (it.Value + 1);
    }
    return ans;
  }
 
  // Driver code
  public static void Main(string []args)
  {
    sieve();
    int []arr = new int[] { 3, 5, 7 };
    int n = arr.Length;
    Console.Write(numberOfDivisorsOfProduct(arr, n));
  }
}
 
// This code is contributed by pratham76.
输出:
8