LeetCode 题解工作台

生成乘积数组的方案数

给你一个二维整数数组 queries ,其中 queries[i] = [n i , k i ] 。第 i 个查询 queries[i] 要求构造长度为 n i 、每个元素都是正整数的数组,且满足所有元素的乘积为 k i ,请你找出有多少种可行的方案。由于答案可能会很大,方案数需要对 10 9 + …

category

5

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

我们可以对 进行质因数分解,即 $k = p_1^{x_1} \times p_2^{x_2} \times \cdots \times p_m^{x_m}$,其中 为质数,而 为 的指数。那么题目实际上等价于:把 个 , 个 , , 个 分别放到 个位置上,单个位置可以为空,问有多少种方案。 根据组合数学的知识,我们把 个球放入 个盒子中,有两种情况:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个二维整数数组 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 <= 104
  • 1 <= ni, ki <= 104
lightbulb

解题思路

方法一:质因数分解 + 组合数学

我们可以对 kk 进行质因数分解,即 k=p1x1×p2x2××pmxmk = p_1^{x_1} \times p_2^{x_2} \times \cdots \times p_m^{x_m},其中 pip_i 为质数,而 xix_ipip_i 的指数。那么题目实际上等价于:把 x1x_1p1p_1, x2x_2p2p_2, \cdots, xmx_mpmp_m 分别放到 nn 个位置上,单个位置可以为空,问有多少种方案。

根据组合数学的知识,我们把 xx 个球放入 nn 个盒子中,有两种情况:

如果盒子不能为空,那么方案数为 Cx1n1C_{x-1}^{n-1},这里是利用隔板法,即一共 xx 个球,我们在其中 x1x-1 个位置插入 n1n-1 个隔板,从而将 xx 个球分成 nn 组。

如果盒子可以为空,那么我们可以再增加 nn 个虚拟球,然后再利用隔板法,即一共 x+nx+n 个球,我们在其中 x+n1x+n-1 个位置插入 n1n-1 个隔板,从而将实际的 xx 个球分成 nn 组,并且允许盒子为空,因此方案数为 Cx+n1n1C_{x+n-1}^{n-1}

因此,对于每个查询 queries[i]queries[i],我们可以先对 kk 进行质因数分解,得到指数 x1,x2,,xmx_1, x_2, \cdots, x_m,然后计算 Cx1+n1n1,Cx2+n1n1,,Cxm+n1n1C_{x_1+n-1}^{n-1}, C_{x_2+n-1}^{n-1}, \cdots, C_{x_m+n-1}^{n-1},最后将所有的方案数相乘即可。

所以,问题转化为如何快速计算 CmnC_m^n,根据公式 Cmn=m!n!(mn)!C_m^n = \frac{m!}{n!(m-n)!},我们可以先预处理出 m!m!,然后利用逆元快速计算 CmnC_m^n

时间复杂度 O(K×loglogK+N+m×logK)O(K \times \log \log K + N + m \times \log K),空间复杂度 O(N)O(N)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
speed

复杂度分析

指标
时间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
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

生成乘积数组的方案数题解:状态·转移·动态规划 | LeetCode #1735 困难