Maximize Number of Nice Divisors - Problem

You are given a positive integer primeFactors. You need to construct a positive integer n that satisfies the following conditions:

  • The number of prime factors of n (not necessarily distinct) is at most primeFactors.
  • The number of nice divisors of n is maximized.

Note: 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], so 6 and 12 are nice divisors, while 3 and 4 are not.

Return the number of nice divisors of n. Since the answer can be very large, return it modulo 109 + 7.

Input & Output

Example 1 — Basic Case
$ Input: primeFactors = 5
Output: 6
💡 Note: Optimal partition is [3,2] giving us nice divisors = 3 × 2 = 6. The number could be 2³×3² with nice divisors being multiples of 2×3=6.
Example 2 — All 3s
$ Input: primeFactors = 8
Output: 18
💡 Note: 8 % 3 = 2, so we use [3,3,2] partition. Nice divisors = 3 × 3 × 2 = 18.
Example 3 — Small Input
$ Input: primeFactors = 4
Output: 4
💡 Note: For small inputs ≤ 4, the answer is the number itself. Partition [4] or [2,2] both give 4.

Constraints

  • 1 ≤ primeFactors ≤ 109

Visualization

Tap to expand
Maximize Number of Nice Divisors INPUT primeFactors = 5 We can use up to 5 primes P1 P2 P3 P4 P5 Example: n = 12 12 = 2 × 2 × 3 Prime factors: [2, 2, 3] Nice Divisors 6, 12 - divisible by 2 and 3 3, 4 - NOT nice (must divide by ALL primes) ALGORITHM STEPS 1 Split into groups Maximize product of exponents 2 Use groups of 3 3×3 > 2×2×2 (9 > 8) 3 Handle remainder r=1: use 2×2, r=2: use 2 4 Calculate result 3^q × 2^r mod (10^9+7) For primeFactors = 5: 5 = 3 + 2 Groups: [3, 2] n = p1^3 × p2^2 Nice divisors = 3 × 2 = 6 (exponent product) FINAL RESULT Optimal split: 5 = 3 + 2 Group 1 3 primes Group 2 2 primes × 3 × 2 = 6 Output: 6 OK - Maximum achieved! n = p^3 × q^2 has exactly 6 nice divisors Key Insight: Nice divisors count = product of all prime exponents. To maximize product with sum = primeFactors, use as many 3s as possible (math proof: e/3 is maximized at e=3). For remainder 1, replace one 3 with two 2s (since 2×2 > 3×1). Use modular exponentiation for large numbers. TutorialsPoint - Maximize Number of Nice Divisors | Optimal Solution
Asked in
Google 15 Facebook 12 Microsoft 8
23.4K Views
Medium Frequency
~35 min Avg. Time
567 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen