给定一个数组arr[]表示给定数字的质因数列表,任务是找到该数字的除数的乘积。
注意:由于产品可以打印得非常大,因此答案是 mod 10 9 + 7。
例子:
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
天真的方法:
从它的质因数列表中生成数字N ,然后在 O(√N) 计算复杂度中找到它的所有除数并继续计算它们的乘积。打印获得的最终产品。
时间复杂度: O(N 3/2 )
辅助空间: O(1)
有效的方法:
为了解决这个问题,需要考虑以下观察:
- 根据费马小定理, a (m – 1) = 1 (mod m)可以进一步扩展为a x = a x % (m – 1) (mod m)
- 对于素数p的a 次幂, f(p a ) = p (a * (a + 1) / 2)) 。
- 因此, f(a * b) = f(a) (d(b)) * f(b) (d(a)) ,其中 d(a), d(b) 表示 a 和 b 中的除数数分别。
请按照以下步骤解决问题:
- 查找给定列表中每个素数的频率(使用 HashMap/Dictionary)。
- 使用第二个观察,对于每个第i个素数,计算:
fp = power(p[i], (cnt[i] + 1) * cnt[i] / 2), where cnt[i] denotes the frequency of that prime
- 使用第三个观察,更新所需的产品:
ans = power(ans, (cnt[i] + 1)) * power(fp, d) % MOD, where d is the number of divisors up to (i – 1)th prime
- 除数d的数量使用费马小定理更新:
d = d * (cnt[i] + 1) % (MOD – 1)
下面是上述方法的实现:
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)