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
nis maximized. Note that a divisor ofnis nice if it is divisible by every prime factor ofn. For example, ifn = 12, then its prime factors are[2,2,3], then6and12are nice divisors, while3and4are 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 1: Problem Transformation + Fast Power
We can factorize \(n\) into prime factors, i.e., \(n = a_1^{k_1} \times a_2^{k_2} \times\cdots \times a_m^{k_m}\), where \(a_i\) is a prime factor and \(k_i\) is the exponent of the prime factor \(a_i\). Since the number of prime factors of \(n\) does not exceed primeFactors, we have \(k_1 + k_2 + \cdots + k_m \leq primeFactors\).
According to the problem description, we know that a good factor of \(n\) must be divisible by all prime factors, which means that a good factor of \(n\) needs to include \(a_1 \times a_2 \times \cdots \times a_m\) as a factor. Then the number of good factors \(k= k_1 \times k_2 \times \cdots \times k_m\), i.e., \(k\) is the product of \(k_1, k_2, \cdots, k_m\). To maximize the number of good factors, we need to split primeFactors into \(k_1, k_2, \cdots, k_m\) to make \(k_1 \times k_2 \times \cdots \times k_m\) the largest. Therefore, the problem is transformed into: split the integer primeFactors into the product of several integers to maximize the product.
Next, we just need to discuss different cases.
- If \(primeFactors \lt 4\), then directly return
primeFactors. - If \(primeFactors\) is a multiple of \(3\), then we split
primeFactorsinto multiples of \(3\), i.e., \(3^{\frac{primeFactors}{3}}\). - If \(primeFactors\) modulo \(3\) equals \(1\), then we split
primeFactorsinto \(\frac{primeFactors}{3} - 1\) multiples of \(3\), and then multiply by \(4\), i.e., \(3^{\frac{primeFactors}{3} - 1} \times 4\). - If \(primeFactors\) modulo \(3\) equals \(2\), then we split
primeFactorsinto \(\frac{primeFactors}{3}\) multiples of \(3\), and then multiply by \(2\), i.e., \(3^{\frac{primeFactors}{3}} \times 2\).
In the above process, we use fast power to calculate the modulus.
The time complexity is \(O(\log n)\), and the space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |