Skip to content

Commit 6844d90

Browse files
committed
feat: add solutions to lc problem: No.1735
No.1735.Count Ways to Make Array With Product
1 parent 589255e commit 6844d90

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

solution/1700-1799/1735.Count Ways to Make Array With Product/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:质因数分解 + 组合数学**
47+
48+
我们可以对 $k$ 进行质因数分解,即 $k = p_1^{x_1} \times p_2^{x_2} \times \cdots \times p_m^{x_m}$,其中 $p_i$ 为质数,而 $x_i$ 为 $p_i$ 的指数。那么题目实际上等价于:把 $x_1$ 个 $p_1$, $x_2$ 个 $p_2$, $\cdots$, $x_m$ 个 $p_m$ 分别放到 $n$ 个位置上,单个位置可以为空,问有多少种方案。
49+
50+
根据组合数学的知识,我们把 $x$ 个球放入 $n$ 个盒子中,有两种情况:
51+
52+
如果盒子不能为空,那么方案数为 $C_{x-1}^{n-1}$,这里是利用隔板法,即一共 $x$ 个球,我们在其中 $x-1$ 个位置插入 $n-1$ 个隔板,从而将 $x$ 个球分成 $n$ 组。
53+
54+
如果盒子可以为空,那么我们可以再增加 $n$ 个虚拟球,然后再利用隔板法,即一共 $x+n$ 个球,我们在其中 $x+n-1$ 个位置插入 $n-1$ 个隔板,从而将实际的 $x$ 个球分成 $n$ 组,并且允许盒子为空,因此方案数为 $C_{x+n-1}^{n-1}$。
55+
56+
因此,对于每个查询 $queries[i]$,我们可以先对 $k$ 进行质因数分解,得到指数 $x_1, x_2, \cdots, x_m$,然后计算 $C_{x_1+n-1}^{n-1}, C_{x_2+n-1}^{n-1}, \cdots, C_{x_m+n-1}^{n-1}$,最后将所有的方案数相乘即可。
57+
58+
所以,问题转化为如何快速计算 $C_m^n$,根据公式 $C_m^n = \frac{m!}{n!(m-n)!}$,我们可以先预处理出 $m!$,然后利用逆元快速计算 $C_m^n$。
59+
60+
时间复杂度 $O(K \times \log \log K + N + m \times \log K)$,空间复杂度 $O(N)$。
61+
4662
<!-- tabs:start -->
4763

4864
### **Python3**

solution/CONTEST_README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
1313
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
14-
| LV3 | 5% | Guardian | &ge;2258.39 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
15-
| LV2 | 20% | Knight | &ge;1888.252 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
14+
| LV3 | 5% | Guardian | &ge;2265.64 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
15+
| LV2 | 20% | Knight | &ge;1894.28 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
1616
| LV1 | 75% | - | - | - |
1717

1818
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。

solution/contest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ def generate_contest_list():
127127
128128
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
129129
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
130-
| LV3 | 5% | Guardian | &ge;2258.39 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
131-
| LV2 | 20% | Knight | &ge;1888.252 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
130+
| LV3 | 5% | Guardian | &ge;2265.64 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
131+
| LV2 | 20% | Knight | &ge;1894.28 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
132132
| LV1 | 75% | - | - | - |
133133
134134
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。

0 commit comments

Comments
 (0)