LeetCode 题解工作台
好因子的最大数目
给你一个正整数 primeFactors 。你需要构造一个正整数 n ,它满足以下条件: n 质因数(质因数需要考虑重复的情况)的数目 不超过 primeFactors 个。 n 好因子的数目最大化。如果 n 的一个因子可以被 n 的每一个质因数整除,我们称这个因子是 好因子 。比方说,如果 n =…
3
题型
5
代码语言
3
相关题
当前训练重点
困难 · 数学·递归
答案摘要
我们可以将 进行质因数分解,即 $n = a_1^{k_1} \times a_2^{k_2} \times\cdots \times a_m^{k_m}$,其中 为质因子,而 为质因子 的指数。由于 的质因子个数不超过 个,因此 $k_1 + k_2 + \cdots + k_m \leq primeFactors$。 而根据题意描述,我们知道 的好因子要满足能被所有的质因子整除,…
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数学·递归 题型思路
题目描述
给你一个正整数 primeFactors 。你需要构造一个正整数 n ,它满足以下条件:
n质因数(质因数需要考虑重复的情况)的数目 不超过primeFactors个。n好因子的数目最大化。如果n的一个因子可以被n的每一个质因数整除,我们称这个因子是 好因子 。比方说,如果n = 12,那么它的质因数为[2,2,3],那么6和12是好因子,但3和4不是。
请你返回 n 的好因子的数目。由于答案可能会很大,请返回答案对 109 + 7 取余 的结果。
请注意,一个质数的定义是大于 1 ,且不能被分解为两个小于该数的自然数相乘。一个数 n 的质因子是将 n 分解为若干个质因子,且它们的乘积为 n 。
示例 1:
输入:primeFactors = 5 输出:6 解释:200 是一个可行的 n 。 它有 5 个质因子:[2,2,2,5,5] ,且有 6 个好因子:[10,20,40,50,100,200] 。 不存在别的 n 有至多 5 个质因子,且同时有更多的好因子。
示例 2:
输入:primeFactors = 8 输出:18
提示:
1 <= primeFactors <= 109
解题思路
方法一:问题转换 + 快速幂
我们可以将 进行质因数分解,即 ,其中 为质因子,而 为质因子 的指数。由于 的质因子个数不超过 个,因此 。
而根据题意描述,我们知道 的好因子要满足能被所有的质因子整除,也即是说 的好因子需要包含 作为因数。那么好因子的个数 ,即 为 的乘积。要最大化好因子的个数,也即是说我们要将 primeFactors 拆分成 ,使得 最大。因此问题转换为:将整数 primeFactors 拆分成若干个整数的乘积,使得乘积最大。
接下来,我们只需要分情况讨论。
- 如果 ,那么直接返回
primeFactors即可。 - 如果 为 的倍数,那么我们将
primeFactors拆分成 的倍数个 ,即 。 - 如果 除以 余 ,那么我们将
primeFactors拆分成 个 ,再乘以 ,即 。 - 如果 除以 余 ,那么我们将
primeFactors拆分成 个 ,再乘以 ,即 。
以上过程中,我们利用快速幂取模求解。
时间复杂度 ,空间复杂度 。
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
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
They want you to notice that nice divisors count as a product of exponent sizes, not to enumerate divisors of n.
- question_mark
They expect the integer-break insight that mostly 3s maximize the product under a fixed sum.
- question_mark
They are checking whether you can combine number theory reasoning with safe modulo power for very large primeFactors.
常见陷阱
外企场景- error
Returning 1 for primeFactors = 2 or 3 because of generic integer break logic, even though this problem allows using all factors directly and the answer should be 2 or 3.
- error
Leaving a remainder 1 as 3 + 1, which loses value compared with converting it to 2 + 2.
- error
Building n or trying candidate factorizations explicitly, which misses the reduction and cannot scale to 10^9.
进阶变体
外企场景- arrow_right_alt
Ask for the actual exponent partition instead of only the maximum count, which still ends in mostly 3s with a small tail adjustment.
- arrow_right_alt
Replace nice divisors with ordinary divisors, changing the count formula from a1 * a2 * ... * ak to (a1 + 1)(a2 + 1)... and altering the optimization target.
- arrow_right_alt
Force exactly k distinct primes in n, which adds another constraint to the exponent partition and changes the best split.