You are given a positive integer primeFactors. You are asked to construct a positive integer n that satisfies the following conditions:
The number of prime factors of n (not necessarily distinct) is at mostprimeFactors.
The number of nice divisors of n is maximized. Note that a divisor of n is nice if it is divisible by every prime factor of n. For example, if n = 12, then its prime factors are [2,2,3], then 6 and 12 are nice divisors, while 3 and 4 are not.
Return the number of nice divisors ofn. Since that number can be too large, return it modulo10^9 + 7.
Note that a prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. The prime factors of a number n is a list of prime numbers such that their product equals n.
Input: primeFactors =5Output: 6Explanation: 200is a valid value of n.It has 5 prime factors:[2,2,2,5,5], and it has 6 nice divisors:[10,20,40,50,100,200].There is not other value of n that has at most 5 prime factors and more nice divisors.
To maximize the number of nice divisors, we want to split the primeFactors into as many 3’s as possible (integer break problem). This is because the product of numbers that sum to primeFactors is maximized when using as many 3’s as possible, with a few 2’s if needed. The answer is the product of these numbers, modulo 1e9+7.
classSolution {
public:int mod =1e9+7;
longlongpower(longlong a, longlong b) {
longlong ans =1;
while (b) {
if (b &1) ans = ans * a % mod;
a = a * a % mod;
b >>=1;
}
return ans;
}
intmaxNiceDivisors(int pf) {
if (pf ==1) return1;
int q = pf /3, r = pf %3;
if (r ==0) return power(3, q);
if (r ==1) return power(3, q-1) *4% mod;
return power(3, q) *2% mod;
}
};
funcmaxNiceDivisors(pfint) int {
mod:= int(1e9+7)
power:=func(a, bint) int {
ans:=1forb > 0 {
ifb&1==1 {
ans = ans*a%mod }
a = a*a%modb>>=1 }
returnans }
ifpf==1 {
return1 }
q, r:=pf/3, pf%3ifr==0 {
returnpower(3, q)
}
ifr==1 {
returnpower(3, q-1) *4%mod }
returnpower(3, q) *2%mod}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
staticfinalint MOD = 1000000007;
privatelongpower(long a, long b) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) ans = ans * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return ans;
}
publicintmaxNiceDivisors(int pf) {
if (pf == 1) return 1;
int q = pf / 3, r = pf % 3;
if (r == 0) return (int)power(3, q);
if (r == 1) return (int)(power(3, q-1) * 4 % MOD);
return (int)(power(3, q) * 2 % MOD);
}
}
classSolution {
privateval mod = 1_000_000_007L
privatefunpower(a: Long, b: Int): Long {
var ans = 1Lvar base = a
var exp = b
while (exp > 0) {
if (exp and 1==1) ans = ans * base % mod
base = base * base % mod
exp = exp shr 1 }
return ans
}
funmaxNiceDivisors(pf: Int): Int {
if (pf ==1) return1val q = pf / 3val r = pf % 3returnwhen (r) {
0-> power(3, q).toInt()
1-> (power(3, q-1) * 4 % mod).toInt()
else-> (power(3, q) * 2 % mod).toInt()
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defmaxNiceDivisors(self, pf: int) -> int:
mod =10**9+7defpower(a: int, b: int) -> int:
ans =1while b:
if b &1:
ans = ans * a % mod
a = a * a % mod
b >>=1return ans
if pf ==1:
return1 q, r = divmod(pf, 3)
if r ==0:
return power(3, q)
if r ==1:
return power(3, q-1) *4% mod
return power(3, q) *2% mod
impl Solution {
pubfnmax_nice_divisors(pf: i32) -> i32 {
let mod_ =1_000_000_007i64;
fnpower(mut a: i64, mut b: i32, mod_: i64) -> i64 {
letmut ans =1i64;
while b >0 {
if b &1==1 {
ans = ans * a % mod_;
}
a = a * a % mod_;
b >>=1;
}
ans
}
if pf ==1 {
return1;
}
let q = pf /3;
let r = pf %3;
if r ==0 {
return power(3, q, mod_) asi32;
}
if r ==1 {
return (power(3, q-1, mod_) *4% mod_) asi32;
}
(power(3, q, mod_) *2% mod_) asi32 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
maxNiceDivisors(pf: number):number {
constmod=1e9+7;
functionpower(a: number, b: number):number {
letans=1;
while (b>0) {
if (b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
returnans;
}
if (pf===1) return1;
constq= Math.floor(pf/3), r=pf%3;
if (r===0) returnpower(3, q);
if (r===1) returnpower(3, q-1) *4%mod;
returnpower(3, q) *2%mod;
}
}