返回介绍

solution / 1800-1899 / 1808.Maximize Number of Nice Divisors / README_EN

发布于 2024-06-17 01:03:14 字数 7243 浏览 0 评论 0 收藏 0

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 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.

 

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 primeFactors into multiples of $3$, i.e., $3^{\frac{primeFactors}{3}}$.
  • If $primeFactors$ modulo $3$ equals $1$, then we split primeFactors into $\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 primeFactors into $\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)$.

class Solution:
  def maxNiceDivisors(self, primeFactors: int) -> int:
    mod = 10**9 + 7
    if primeFactors < 4:
      return primeFactors
    if primeFactors % 3 == 0:
      return pow(3, primeFactors // 3, mod) % mod
    if primeFactors % 3 == 1:
      return 4 * pow(3, primeFactors // 3 - 1, mod) % mod
    return 2 * pow(3, primeFactors // 3, mod) % mod
class Solution {
  private final int mod = (int) 1e9 + 7;

  public int maxNiceDivisors(int primeFactors) {
    if (primeFactors < 4) {
      return primeFactors;
    }
    if (primeFactors % 3 == 0) {
      return qpow(3, primeFactors / 3);
    }
    if (primeFactors % 3 == 1) {
      return (int) (4L * qpow(3, primeFactors / 3 - 1) % mod);
    }
    return 2 * qpow(3, primeFactors / 3) % mod;
  }

  private int qpow(long a, long n) {
    long ans = 1;
    for (; n > 0; n >>= 1) {
      if ((n & 1) == 1) {
        ans = ans * a % mod;
      }
      a = a * a % mod;
    }
    return (int) ans;
  }
}
class Solution {
public:
  int maxNiceDivisors(int primeFactors) {
    if (primeFactors < 4) {
      return primeFactors;
    }
    const int mod = 1e9 + 7;
    auto qpow = [&](long long a, long long n) {
      long long ans = 1;
      for (; n; n >>= 1) {
        if (n & 1) {
          ans = ans * a % mod;
        }
        a = a * a % mod;
      }
      return (int) ans;
    };
    if (primeFactors % 3 == 0) {
      return qpow(3, primeFactors / 3);
    }
    if (primeFactors % 3 == 1) {
      return qpow(3, primeFactors / 3 - 1) * 4L % mod;
    }
    return qpow(3, primeFactors / 3) * 2 % mod;
  }
};
func maxNiceDivisors(primeFactors int) int {
  if primeFactors < 4 {
    return primeFactors
  }
  const mod = 1e9 + 7
  qpow := func(a, n int) int {
    ans := 1
    for ; n > 0; n >>= 1 {
      if n&1 == 1 {
        ans = ans * a % mod
      }
      a = a * a % mod
    }
    return ans
  }
  if primeFactors%3 == 0 {
    return qpow(3, primeFactors/3)
  }
  if primeFactors%3 == 1 {
    return qpow(3, primeFactors/3-1) * 4 % mod
  }
  return qpow(3, primeFactors/3) * 2 % mod
}
/**
 * @param {number} primeFactors
 * @return {number}
 */
var maxNiceDivisors = function (primeFactors) {
  if (primeFactors < 4) {
    return primeFactors;
  }
  const mod = 1e9 + 7;
  const qpow = (a, n) => {
    let ans = 1;
    for (; n; n >>= 1) {
      if (n & 1) {
        ans = Number((BigInt(ans) * BigInt(a)) % BigInt(mod));
      }
      a = Number((BigInt(a) * BigInt(a)) % BigInt(mod));
    }
    return ans;
  };
  const k = Math.floor(primeFactors / 3);
  if (primeFactors % 3 === 0) {
    return qpow(3, k);
  }
  if (primeFactors % 3 === 1) {
    return (4 * qpow(3, k - 1)) % mod;
  }
  return (2 * qpow(3, k)) % mod;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文