📜  一个数的因数的给定列表的除数的乘积

📅  最后修改于: 2021-09-06 06:25:18             🧑  作者: 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. 对于素数pa 次幂, f(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素数,计算:
  • 使用第三个观察,更新所需的产品:
  • 除数d的数量使用费马小定理更新:

下面是上述方法的实现:

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


Javascript


输出:
1331

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