LeetCode 题解工作台
生成乘积数组的方案数
给你一个二维整数数组 queries ,其中 queries[i] = [n i , k i ] 。第 i 个查询 queries[i] 要求构造长度为 n i 、每个元素都是正整数的数组,且满足所有元素的乘积为 k i ,请你找出有多少种可行的方案。由于答案可能会很大,方案数需要对 10 9 + …
5
题型
4
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
我们可以对 进行质因数分解,即 $k = p_1^{x_1} \times p_2^{x_2} \times \cdots \times p_m^{x_m}$,其中 为质数,而 为 的指数。那么题目实际上等价于:把 个 , 个 , , 个 分别放到 个位置上,单个位置可以为空,问有多少种方案。 根据组合数学的知识,我们把 个球放入 个盒子中,有两种情况:
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你一个二维整数数组 queries ,其中 queries[i] = [ni, ki] 。第 i 个查询 queries[i] 要求构造长度为 ni 、每个元素都是正整数的数组,且满足所有元素的乘积为 ki ,请你找出有多少种可行的方案。由于答案可能会很大,方案数需要对 109 + 7 取余 。
请你返回一个整数数组 answer,满足 answer.length == queries.length ,其中 answer[i]是第 i 个查询的结果。
示例 1:
输入:queries = [[2,6],[5,1],[73,660]] 输出:[4,1,50734910] 解释:每个查询之间彼此独立。 [2,6]:总共有 4 种方案得到长度为 2 且乘积为 6 的数组:[1,6],[2,3],[3,2],[6,1]。 [5,1]:总共有 1 种方案得到长度为 5 且乘积为 1 的数组:[1,1,1,1,1]。 [73,660]:总共有 1050734917 种方案得到长度为 73 且乘积为 660 的数组。1050734917 对 109 + 7 取余得到 50734910 。
示例 2 :
输入:queries = [[1,1],[2,2],[3,3],[4,4],[5,5]] 输出:[1,2,3,10,5]
提示:
1 <= queries.length <= 1041 <= ni, ki <= 104
解题思路
方法一:质因数分解 + 组合数学
我们可以对 进行质因数分解,即 ,其中 为质数,而 为 的指数。那么题目实际上等价于:把 个 , 个 , , 个 分别放到 个位置上,单个位置可以为空,问有多少种方案。
根据组合数学的知识,我们把 个球放入 个盒子中,有两种情况:
如果盒子不能为空,那么方案数为 ,这里是利用隔板法,即一共 个球,我们在其中 个位置插入 个隔板,从而将 个球分成 组。
如果盒子可以为空,那么我们可以再增加 个虚拟球,然后再利用隔板法,即一共 个球,我们在其中 个位置插入 个隔板,从而将实际的 个球分成 组,并且允许盒子为空,因此方案数为 。
因此,对于每个查询 ,我们可以先对 进行质因数分解,得到指数 ,然后计算 ,最后将所有的方案数相乘即可。
所以,问题转化为如何快速计算 ,根据公式 ,我们可以先预处理出 ,然后利用逆元快速计算 。
时间复杂度 ,空间复杂度 。
N = 10020
MOD = 10**9 + 7
f = [1] * N
g = [1] * N
p = defaultdict(list)
for i in range(1, N):
f[i] = f[i - 1] * i % MOD
g[i] = pow(f[i], MOD - 2, MOD)
x = i
j = 2
while j <= x // j:
if x % j == 0:
cnt = 0
while x % j == 0:
cnt += 1
x //= j
p[i].append(cnt)
j += 1
if x > 1:
p[i].append(1)
def comb(n, k):
return f[n] * g[k] * g[n - k] % MOD
class Solution:
def waysToFillArray(self, queries: List[List[int]]) -> List[int]:
ans = []
for n, k in queries:
t = 1
for x in p[k]:
t = t * comb(x + n - 1, n - 1) % MOD
ans.append(t)
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity depends on prime factorization of k and the calculation of combinations for each prime exponent across n positions. Space complexity is mainly for factorial tables and storing intermediate results for each query. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Expect candidates to recognize prime factorization reduces multiplicative constraints to additive distributions.
- question_mark
Look for correct handling of large numbers and modulo arithmetic in combinatorial calculations.
- question_mark
Strong solutions efficiently precompute factorials to avoid recomputation for multiple queries.
常见陷阱
外企场景- error
Failing to account for modulo arithmetic during combination calculations, leading to integer overflow.
- error
Miscounting distributions of prime powers when exponents are large compared to n.
- error
Not separating queries properly, causing mixing of results across independent queries.
进阶变体
外企场景- arrow_right_alt
Counting arrays with a sum constraint instead of a product using similar combinatorial DP techniques.
- arrow_right_alt
Handling arrays with restricted value ranges, requiring adjusted prime distributions.
- arrow_right_alt
Extending to multi-dimensional arrays where the product constraint applies along different axes.