📜  给定主要因子给定列表中的若干个除数的乘积

📅  最后修改于: 2021-05-17 23:06:35             🧑  作者: Mango

给定一个数组arr [],它表示给定数字的素数的列表,任务是找到该数字的除数的乘积。
注意:由于产品可能很大,请打印答案mod 10 9 + 7。
例子:

天真的方法:
从其主要因子列表中生成数字N ,然后以O(√N)计算复杂度找到其所有除数,并继续计算其乘积。打印获得的最终产品。
时间复杂度: O(N 3/2 )
辅助空间: O(1)

高效方法:
为了解决该问题,需要考虑以下几点:

  1. 根据费马小定理, a (m – 1) = 1(mod m)可以进一步扩展为a x = a x%(m – 1) (mod m)
  2. 对于素数p提高到幂af(p a )= p (a *(a +1)/ 2))
  3. 因此, f(a * b)= f(a) (d(b)) * f(b) (d(a)) ,其中d(a),d(b)表示a和b中的除数分别。

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

  • 在给定列表中找到每个质数的频率(使用HashMap / Dictionary)。
  • 使用第二个观察值,对于每i素数,计算:
  • 使用第三个观察值,更新所需的产品:

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
  
#include  
using namespace std;
  
int MOD = 1000000007;
  
// Function to calculate (a^b)% m
int power(int a, int b, int m)
{
    a %= m;
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = ((res % m) * (a % m))
                  % m;
  
        a = ((a % m) * (a % m)) % m;
  
        b >>= 1;
    }
  
    return res % m;
}
  
// Function to calculate and return
// the product of divisors
int productOfDivisors(int p[], int n)
{
  
    // Stores the frequencies of
    // prime divisors
    map prime; 
  
    for (int i = 0; i < n; i++) {
        prime[p[i]]++;
    }
    int product = 1, d = 1;
  
    // Iterate over the prime
    // divisors
    for (auto itr : prime) {
  
        int val
            = power(itr.first,
                    (itr.second) * (itr.second + 1) / 2,
                    MOD);
  
        // Update the product
        product = (power(product, itr.second + 1, MOD)
                   * power(val, d, MOD))
                  % MOD;
  
        // Update the count of divisors
        d = (d * (itr.second + 1)) % (MOD - 1);
    }
  
    return product;
}
  
// Driver Code
int main()
{
  
    int arr[] = { 11, 11 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout <


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
  
static int MOD = 1000000007;
  
// Function to calculate (a^b)% m
static int power(int a, int b, int m)
{
    a %= m;
    int res = 1;
    while (b > 0) 
    {
        if (b % 2 == 1)
            res = ((res % m) * (a % m)) % m;
  
        a = ((a % m) * (a % m)) % m;
  
        b >>= 1;
    }
    return res % m;
}
  
// Function to calculate and return
// the product of divisors
static int productOfDivisors(int p[], int n)
{
  
    // Stores the frequencies of
    // prime divisors
    HashMap prime = new HashMap(); 
  
    for (int i = 0; i < n; i++) 
    {
        if(prime.containsKey(p[i]))
            prime.put(p[i], prime.get(p[i]) + 1);
        else
            prime.put(p[i], 1);
              
    }
    int product = 1, d = 1;
  
    // Iterate over the prime
    // divisors
    for (Map.Entry itr : prime.entrySet())
    {
        int val = power(itr.getKey(),
                       (itr.getValue()) *
                       (itr.getValue() + 1) / 2, MOD);
  
        // Update the product
        product = (power(product, itr.getValue() + 1, MOD) *
                   power(val, d, MOD)) % MOD;
  
        // Update the count of divisors
        d = (d * (itr.getValue() + 1)) % (MOD - 1);
    }
    return product;
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 11, 11 };
    int n = arr.length;
  
    System.out.println(productOfDivisors(arr,n));
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 program to implement 
# the above approach 
from collections import defaultdict
  
MOD = 1000000007
  
# Function to calculate (a^b)% m
def power(a, b, m):
  
    a %= m
    res = 1
  
    while (b > 0):
        if (b & 1):
            res = ((res % m) * (a % m)) % m
  
        a = ((a % m) * (a % m)) % m
        b >>= 1
      
    return res % m
  
# Function to calculate and return
# the product of divisors
def productOfDivisors(p, n):
  
    # Stores the frequencies of
    # prime divisors
    prime = defaultdict(int)
  
    for i in range(n):
        prime[p[i]] += 1
      
    product, d = 1, 1
  
    # Iterate over the prime
    # divisors
    for itr in prime.keys():
        val = (power(itr, (prime[itr]) * 
                          (prime[itr] + 1) // 2, MOD))
  
        # Update the product
        product = (power(product,
                         prime[itr] + 1, MOD) *
                   power(val, d, MOD) % MOD)
  
        # Update the count of divisors
        d = (d * (prime[itr] + 1)) % (MOD - 1)
  
    return product
  
# Driver Code
if __name__ == "__main__":
  
    arr = [ 11, 11 ]
    n = len(arr)
      
    print(productOfDivisors(arr, n))
  
# This code is contributed by chitranayal


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
static int MOD = 1000000007;                     
  
// Function to calculate (a^b)% m
static int power(int a, int b, int m)
{
    a %= m;
    int res = 1;
    while (b > 0) 
    {
        if (b % 2 == 1)
            res = ((res % m) * (a % m)) % m;
  
        a = ((a % m) * (a % m)) % m;
        b >>= 1;
    }
    return res % m;
}
  
// Function to calculate and return
// the product of divisors
static int productOfDivisors(int []p, int n)
{
      
    // Stores the frequencies of
    // prime divisors
    Dictionary prime = new Dictionary(); 
  
    for(int i = 0; i < n; i++) 
    {
        if(prime.ContainsKey(p[i]))
            prime[p[i]] = prime[p[i]] + 1;
        else
            prime.Add(p[i], 1);
    }
    int product = 1, d = 1;
  
    // Iterate over the prime
    // divisors
    foreach(KeyValuePair itr in prime)
    {
        int val = power(itr.Key,
                       (itr.Value) *
                       (itr.Value + 1) / 2, MOD);
  
        // Update the product
        product = (power(product, itr.Value + 1, MOD) *
                   power(val, d, MOD)) % MOD;
  
        // Update the count of divisors
        d = (d * (itr.Value + 1)) % (MOD - 1);
    }
    return product;
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 11, 11 };
    int n = arr.Length;
  
    Console.WriteLine(productOfDivisors(arr,n));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
1331

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