|
| 1 | +# [1808. Maximize Number of Nice Divisors](https://leetcode.com/problems/maximize-number-of-nice-divisors) |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +<div class="elfjS" data-track-load="description_content"><p>You are given a positive integer <code>primeFactors</code>. You are asked to construct a positive integer <code>n</code> that satisfies the following conditions:</p> |
| 6 | + |
| 7 | +<ul> |
| 8 | + <li>The number of prime factors of <code>n</code> (not necessarily distinct) is <strong>at most</strong> <code>primeFactors</code>.</li> |
| 9 | + <li>The number of nice divisors of <code>n</code> is maximized. Note that a divisor of <code>n</code> is <strong>nice</strong> if it is divisible by every prime factor of <code>n</code>. For example, if <code>n = 12</code>, then its prime factors are <code>[2,2,3]</code>, then <code>6</code> and <code>12</code> are nice divisors, while <code>3</code> and <code>4</code> are not.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p>Return <em>the number of nice divisors of</em> <code>n</code>. Since that number can be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> |
| 13 | + |
| 14 | +<p>Note that a prime number is a natural number greater than <code>1</code> that is not a product of two smaller natural numbers. The prime factors of a number <code>n</code> is a list of prime numbers such that their product equals <code>n</code>.</p> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | +<p><strong class="example">Example 1:</strong></p> |
| 18 | + |
| 19 | +<pre><strong>Input:</strong> primeFactors = 5 |
| 20 | +<strong>Output:</strong> 6 |
| 21 | +<strong>Explanation:</strong> 200 is a valid value of n. |
| 22 | +It has 5 prime factors: [2,2,2,5,5], and it has 6 nice divisors: [10,20,40,50,100,200]. |
| 23 | +There is not other value of n that has at most 5 prime factors and more nice divisors. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p><strong class="example">Example 2:</strong></p> |
| 27 | + |
| 28 | +<pre><strong>Input:</strong> primeFactors = 8 |
| 29 | +<strong>Output:</strong> 18 |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>1 <= primeFactors <= 10<sup>9</sup></code></li> |
| 37 | +</ul></div> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | + |
| 41 | +## Solutions |
| 42 | + |
| 43 | +**Solution: `Math`** |
| 44 | + |
| 45 | +- Time complexity: <em>O(logn)</em> |
| 46 | +- Space complexity: <em>O(1)</em> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | + |
| 50 | +### **JavaScript** |
| 51 | + |
| 52 | +```js |
| 53 | +/** |
| 54 | + * @param {number} primeFactors |
| 55 | + * @return {number} |
| 56 | + */ |
| 57 | +const maxNiceDivisors = function (primeFactors) { |
| 58 | + if (primeFactors <= 3) return primeFactors; |
| 59 | + const MODULO = BigInt(10 ** 9 + 7); |
| 60 | + |
| 61 | + const powMod = (base, exponent) => { |
| 62 | + let result = 1n; |
| 63 | + |
| 64 | + while (exponent) { |
| 65 | + if (exponent % 2n) { |
| 66 | + result = (result * base) % MODULO; |
| 67 | + } |
| 68 | + |
| 69 | + base = (base * base) % MODULO; |
| 70 | + exponent /= 2n; |
| 71 | + } |
| 72 | + |
| 73 | + return result; |
| 74 | + }; |
| 75 | + |
| 76 | + const count = BigInt(Math.floor(primeFactors / 3)); |
| 77 | + const remainder = primeFactors % 3; |
| 78 | + |
| 79 | + if (remainder === 0) return Number(powMod(3n, count)); |
| 80 | + if (remainder === 1) { |
| 81 | + const result = powMod(3n, count - 1n); |
| 82 | + |
| 83 | + return Number((result * 4n) % MODULO); |
| 84 | + } |
| 85 | + |
| 86 | + return Number((powMod(3n, count) * 2n) % MODULO); |
| 87 | +}; |
| 88 | +``` |
0 commit comments