Skip to content

Commit 0aed8a5

Browse files
authored
feat: add solutions for lc No.3792 (#4941)
1 parent 180652e commit 0aed8a5

File tree

11 files changed

+466
-2
lines changed

11 files changed

+466
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
- [乘积小于 K 的子数组](/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README.md) - `双指针`
5353
- [位 1 的个数](/solution/0100-0199/0191.Number%20of%201%20Bits/README.md) - `位运算``lowbit`
5454
- [合并区间](/solution/0000-0099/0056.Merge%20Intervals/README.md) - `区间合并`
55-
<!-- 排序算法、待补充 -->
55+
<!-- 排序算法、待补充 -->
5656

5757
### 2. 数据结构
5858

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3792.Sum%20of%20Increasing%20Product%20Blocks/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3792. 递增乘积块之和 🔒](https://leetcode.cn/problems/sum-of-increasing-product-blocks)
10+
11+
[English Version](/solution/3700-3799/3792.Sum%20of%20Increasing%20Product%20Blocks/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给定一个整数&nbsp;<code>n</code>。</p>
18+
19+
<p>一个序列的形成如下:</p>
20+
21+
<ul>
22+
<li>第&nbsp;<code>1</code>&nbsp;块包含&nbsp;<code>1</code>。</li>
23+
<li>第&nbsp;<code>2</code>&nbsp;块包含&nbsp;<code>2 * 3</code>。</li>
24+
<li>第&nbsp;<code>i</code>&nbsp;块是之后&nbsp;<code>i</code>&nbsp;个连续整数的乘积。</li>
25+
</ul>
26+
27+
<p>令&nbsp;<code>F(n)</code>&nbsp;为前 <code>n</code>&nbsp;块之和。</p>
28+
29+
<p>返回一个整数表示&nbsp;<code>F(n)</code> <strong>模上</strong>&nbsp;<code>10<sup>9</sup> + 7</code>。</p>
30+
31+
<p>&nbsp;</p>
32+
33+
<p><strong class="example">示例 1:</strong></p>
34+
35+
<div class="example-block">
36+
<p><span class="example-io"><b>输入:</b>n = 3</span></p>
37+
38+
<p><span class="example-io"><b>输出:</b>127</span></p>
39+
40+
<p><strong>解释:</strong></p>
41+
42+
<ul>
43+
<li>块 1:<code>1</code></li>
44+
<li>块 2:<code>2 * 3 = 6</code></li>
45+
<li>块 3:<code>4 * 5 * 6 = 120</code></li>
46+
</ul>
47+
48+
<p><code>F(3) = 1 + 6 + 120 = 127</code></p>
49+
</div>
50+
51+
<p><strong class="example">示例 2:</strong></p>
52+
53+
<div class="example-block">
54+
<p><span class="example-io"><b>输入:</b>n = 7</span></p>
55+
56+
<p><span class="example-io"><b>输出:</b>6997165</span></p>
57+
58+
<p><strong>解释:</strong></p>
59+
60+
<ul>
61+
<li>块 1:<code>1</code></li>
62+
<li>块 2:<code>2 * 3 = 6</code></li>
63+
<li>块 3:<code>4 * 5 * 6 = 120</code></li>
64+
<li>块 4:<code>7 * 8 * 9 * 10 = 5040</code></li>
65+
<li>块 5:<code>11 * 12 * 13 * 14 * 15 = 360360</code></li>
66+
<li>块 6:<code>16 * 17 * 18 * 19 * 20 * 21 = 39070080</code></li>
67+
<li>块 7:<code>22 * 23 * 24 * 25 * 26 * 27 * 28 = 5967561600</code></li>
68+
</ul>
69+
70+
<p><code>F(7) = 6006997207 % (10<sup>9</sup> + 7) = 6997165</code></p>
71+
</div>
72+
73+
<p>&nbsp;</p>
74+
75+
<p><strong>提示:</strong></p>
76+
77+
<ul>
78+
<li><code>1 &lt;= n &lt;= 1000</code></li>
79+
</ul>
80+
81+
<!-- description:end -->
82+
83+
## 解法
84+
85+
<!-- solution:start -->
86+
87+
### 方法一:模拟
88+
89+
我们可以直接模拟每一块的乘积并累加到答案中。需要注意的是,由于乘积可能会非常大,我们需要在每一步计算时对结果取模。
90+
91+
时间复杂度 $O(n^2)$,空间复杂度 $O(1)$。
92+
93+
<!-- tabs:start -->
94+
95+
#### Python3
96+
97+
```python
98+
class Solution:
99+
def sumOfBlocks(self, n: int) -> int:
100+
ans = 0
101+
mod = 10**9 + 7
102+
k = 1
103+
for i in range(1, n + 1):
104+
x = 1
105+
for j in range(k, k + i):
106+
x = (x * j) % mod
107+
ans = (ans + x) % mod
108+
k += i
109+
return ans
110+
```
111+
112+
#### Java
113+
114+
```java
115+
class Solution {
116+
public int sumOfBlocks(int n) {
117+
final int mod = (int) 1e9 + 7;
118+
long ans = 0;
119+
int k = 1;
120+
for (int i = 1; i <= n; ++i) {
121+
long x = 1;
122+
for (int j = k; j < k + i; ++j) {
123+
x = x * j % mod;
124+
}
125+
ans = (ans + x) % mod;
126+
k += i;
127+
}
128+
return (int) ans;
129+
}
130+
}
131+
```
132+
133+
#### C++
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
int sumOfBlocks(int n) {
139+
const int mod = 1e9 + 7;
140+
long long ans = 0;
141+
int k = 1;
142+
for (int i = 1; i <= n; ++i) {
143+
long long x = 1;
144+
for (int j = k; j < k + i; ++j) {
145+
x = x * j % mod;
146+
}
147+
ans = (ans + x) % mod;
148+
k += i;
149+
}
150+
return ans;
151+
}
152+
};
153+
```
154+
155+
#### Go
156+
157+
```go
158+
func sumOfBlocks(n int) (ans int) {
159+
const mod int = 1e9 + 7
160+
k := 1
161+
for i := 1; i <= n; i++ {
162+
x := 1
163+
for j := k; j < k+i; j++ {
164+
x = x * j % mod
165+
}
166+
ans = (ans + x) % mod
167+
k += i
168+
}
169+
return
170+
}
171+
```
172+
173+
#### TypeScript
174+
175+
```ts
176+
function sumOfBlocks(n: number): number {
177+
const mod = 1000000007;
178+
let k = 1;
179+
let ans = 0;
180+
for (let i = 1; i <= n; i++) {
181+
let x = 1;
182+
for (let j = k; j < k + i; j++) {
183+
x = (x * j) % mod;
184+
}
185+
ans = (ans + x) % mod;
186+
k += i;
187+
}
188+
return ans;
189+
}
190+
```
191+
192+
<!-- tabs:end -->
193+
194+
<!-- solution:end -->
195+
196+
<!-- problem:end -->

0 commit comments

Comments
 (0)