Product of divisors of a number from a given list of its prime factors

• Last Updated : 25 May, 2021

Given an array arr[] representing a list of prime factors of a given number, the task is to find the product of divisors of that number.
Note: Since the product can be, very large printed, the answer is mod 109 + 7.

Examples:

Input: arr[] = {2, 2, 3}
Output: 1728
Explanation:
Product of the given prime factors = 2 * 2 * 3 = 12.
Divisors of 12 are {1, 2, 3, 4, 6, 12}.
Hence, the product of divisors is 1728.

Input: arr[] = {11, 11}
Output: 1331

Naive Approach:
Generate the number N from its list of prime factors, then find all its divisors in O(√N) computational complexity and keep computing their product. Print the final product obtained.
Time Complexity: O(N3/2
Auxiliary Space: O(1)
Efficient Approach:
To solve the problem, the following observations need to be taken into account:

1. According to Fermat’s little theorem, a(m – 1) = 1 (mod m) which can be further extended to ax = a x % (m – 1) (mod m)
2. For a prime p raised to the power a, f(pa) = p(a * (a + 1) / 2)).
3. Hence, f(a * b) = f(a)(d(b)) * f(b)(d(a)), where d(a), d(b) denotes the number of divisors in a and b respectively.

Follow the steps below to solve the problem:

• Find the frequency of every prime in the given list (using a HashMap/Dictionary).
• Using the second observation, for every ith prime, calculate:

fp = power(p[i], (cnt[i] + 1) * cnt[i] / 2), where cnt[i] denotes the frequency of that prime

• Using the third observation, update the required product:

ans = power(ans, (cnt[i] + 1)) * power(fp, d) % MOD, where d is the number of divisors up to (i – 1)th prime

• The number of divisors d is updated using Fermat’s Little Theorem:

d = d * (cnt[i] + 1) % (MOD – 1)

Below is the implementation of the above approach:

C++

 // C++ Program to implement// the above approach #include using namespace std; int MOD = 1000000007; // Function to calculate (a^b)% mint 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 divisorsint 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 Codeint main(){     int arr[] = { 11, 11 };    int n = sizeof(arr) / sizeof(arr);     cout <

Java

 // Java Program to implement// the above approachimport java.util.*;class GFG{ static int MOD = 1000000007; // Function to calculate (a^b)% mstatic 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 divisorsstatic 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 Codepublic 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 approachfrom collections import defaultdict MOD = 1000000007 # Function to calculate (a^b)% mdef 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 divisorsdef 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 Codeif __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 approachusing System;using System.Collections.Generic; class GFG{ static int MOD = 1000000007;                     // Function to calculate (a^b)% mstatic 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 divisorsstatic 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 Codepublic 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


Output:
1331

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up