1808. Maximize Number of Nice Divisors
Hard35.4% acceptance10,019 / 28,320 submissions
Asked by 1 company
Topics
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
Hints
Hint 1
The number of nice divisors is equal to the product of the count of each prime factor. Then the problem is reduced to: given n, find a sequence of numbers whose sum equals n and whose product is maximized.
Hint 2
This sequence can have no numbers that are larger than 4. Proof: if it contains a number x that is larger than 4, then you can replace x with floor(x/2) and ceil(x/2), and floor(x/2) * ceil(x/2) > x. You can also replace 4s with two 2s. Hence, there will always be optimal solutions with only 2s and 3s.
Hint 3
If there are three 2s, you can replace them with two 3s to get a better product. Hence, you'll never have more than two 2s.
Hint 4
Keep adding 3s as long as n ≥ 5.