给定表示给定数字质数列表的数组arr[]
,任务是找到该数字除数的乘积。
注意:由于乘积可以打印得很大,因此答案模10 ^ 9 7
。
示例:
输入:
arr[] = {2, 2, 3}
输出:1728
说明:
因数
2 * 2 * 3 = 12
。12 的除数为
{1,2,3,4,6,12}
。因此,除数的乘积为 1728。
输入:
arr[] = {11, 11}
输出: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)
,其中cnt[i]
表示该质数的频率。 -
使用第三个观察值,更新所需的乘积:
ans = power(ans, (cnt[i] 1)) * power(fp, d) % mod
,其中d
是直到第i - 1
个质数的除数数量。 -
使用费马小定理更新除数的数量
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
输出:
1331
时间复杂度:o(n)
。
辅助空间:o(n)
。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处