1808. Maximize Number of Nice Divisors
Description
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 ofn
is nice if it is divisible by every prime factor ofn
. For example, ifn = 12
, then its prime factors are[2,2,3]
, then6
and12
are nice divisors, while3
and4
are not.
Return the number of nice divisors of n
. Since that number can be too large, return it modulo 109 + 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
.
Example 1:
Input: primeFactors = 5 Output: 6 Explanation: 200 is 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.
Example 2:
Input: primeFactors = 8 Output: 18
Constraints:
1 <= primeFactors <= 109
Solutions
Solution: Math
- Time complexity: O(logn)
- Space complexity: O(1)
JavaScript
js
/**
* @param {number} primeFactors
* @return {number}
*/
const maxNiceDivisors = function (primeFactors) {
if (primeFactors <= 3) return primeFactors;
const MODULO = BigInt(10 ** 9 + 7);
const powMod = (base, exponent) => {
let result = 1n;
while (exponent) {
if (exponent % 2n) {
result = (result * base) % MODULO;
}
base = (base * base) % MODULO;
exponent /= 2n;
}
return result;
};
const count = BigInt(Math.floor(primeFactors / 3));
const remainder = primeFactors % 3;
if (remainder === 0) return Number(powMod(3n, count));
if (remainder === 1) {
const result = powMod(3n, count - 1n);
return Number((result * 4n) % MODULO);
}
return Number((powMod(3n, count) * 2n) % MODULO);
};