[LeetCode] Maximize Number of Nice Divisors

1808. Maximize Number of Nice Divisors

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 most primeFactors.
  • The number of nice divisors of n is maximized. Note that 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], then 6 and 12 are nice divisors, while 3 and 4 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
const int MOD = 1e9 + 7;
long fastPow(long n, long p) {
long res = 1L;
while(p) {
if(p & 1)
res = res * n % MOD;
n = n * n % MOD;
p>>=1;
}
return res;
}
public:
int maxNiceDivisors(int primeFactors) {
if(primeFactors < 4)
return primeFactors;
int prefix = primeFactors % 3 == 0 ? 3 : primeFactors % 3 == 1 ? 4 : 6;
return prefix * fastPow(3, primeFactors / 3 - 1) % MOD;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/03/28/PS/LeetCode/maximize-number-of-nice-divisors/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.