Skip to content

Commit 76a903a

Browse files
authored
update: 添加问题“2438.二的幂数组中查询范围内的乘积”的代码和题解 (#1073)
* 2438: WA.cpp (#1072) * 2438: WA.cpp (#1072) - 溢出 * 2438: RE.cpp (#1072) 刚刚debug一直不显示pows到结果,最后一看vscode的cpp debug插件直接占了100多G(虚拟)内存,杀掉也很卡,强制重启了最后 * 2438: AC.cpp (#1072) cpp - AC,60.94%,95.31% 上次commit报错信息: Line 1122: Char 9: runtime error: reference binding to misaligned address 0xbebebebebebebec2 for type 'int', which requires 4 byte alignment (stl_vector.h) 0xbebebebebebebec2: note: pointer points here <memory cannot be printed> SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/stl_vector.h:1131:9 * 2438: AC.py (#1072) python - AC,80.79%,7.28% * update: 添加问题“2438.二的幂数组中查询范围内的乘积”的代码和题解 (#1073) Signed-off-by: LetMeFly666 <[email protected]> * clean: non implementated files (#1072) * clean: non implementated files (#1072) --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 6785649 commit 76a903a

7 files changed

+233
-3
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-11 18:41:58
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-11 21:43:41
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
const ll MOD = 1e9 + 7;
13+
14+
class Solution {
15+
public:
16+
vector<int> productQueries(int n, vector<vector<int>>& queries) {
17+
vector<int> pows;
18+
int th = 0;
19+
while (n) {
20+
if (n & 1) {
21+
pows.push_back(1 << th);
22+
}
23+
n >>= 1;
24+
th++;
25+
}
26+
vector<int> ans(queries.size());
27+
for (int i = 0; i < queries.size(); i++) {
28+
ll thisVal = 1;
29+
for (int j = queries[i][0]; j <= queries[i][1]; j++) {
30+
thisVal = thisVal * pows[j] % MOD;
31+
}
32+
ans[i] = thisVal;
33+
}
34+
return ans;
35+
}
36+
};
37+
38+
#if defined(_WIN32) || defined(__APPLE__)
39+
/*
40+
15
41+
[[0,1],[2,2],[0,3]]
42+
43+
[2,4,64]
44+
*/
45+
int main() {
46+
int n;
47+
string s;
48+
while (cin >> n >> s) {
49+
vector<vector<int>> q = stringToVectorVector(s);
50+
Solution sol;
51+
debug(sol.productQueries(n, q));
52+
}
53+
return 0;
54+
}
55+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-08-11 21:37:10
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-08-11 21:46:52
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def productQueries(self, n: int, queries: List[List[int]]) -> List[int]:
11+
pows = []
12+
th = 0
13+
while n:
14+
if n & 1:
15+
pows.append(1 << th)
16+
th += 1
17+
n >>= 1
18+
perfix = [1] * (len(pows) + 1)
19+
for i in range(1, len(pows) + 1):
20+
perfix[i] = perfix[i - 1] * pows[i - 1]
21+
return [perfix[q[1] + 1] // perfix[q[0]] % 1000000007 for q in queries]

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2025-08-07 14:11:36
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-08-08 10:19:31
5+
* @LastEditTime: 2025-08-11 22:07:08
66
*/
77
pub struct Solution;
88
include!("0869-reordered-power-of-2_20250810.rs"); // 这个fileName是会被脚本替换掉的

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@
775775
|2432.处理用时最长的那个任务的员工|简单|<a href="https://leetcode.cn/problems/the-employee-that-worked-on-the-longest-task/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/05/LeetCode%202432.%E5%A4%84%E7%90%86%E7%94%A8%E6%97%B6%E6%9C%80%E9%95%BF%E7%9A%84%E9%82%A3%E4%B8%AA%E4%BB%BB%E5%8A%A1%E7%9A%84%E5%91%98%E5%B7%A5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130514393" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/the-employee-that-worked-on-the-longest-task/solutions/2257620/letmefly-2432chu-li-yong-shi-zui-chang-d-iasj/" target="_blank">LeetCode题解</a>|
776776
|2434.使用机器人打印字典序最小的字符串|中等|<a href="https://leetcode.cn/problems/using-a-robot-to-print-the-lexicographically-smallest-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/06/LeetCode%202434.%E4%BD%BF%E7%94%A8%E6%9C%BA%E5%99%A8%E4%BA%BA%E6%89%93%E5%8D%B0%E5%AD%97%E5%85%B8%E5%BA%8F%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148484075" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/using-a-robot-to-print-the-lexicographically-smallest-string/solutions/3694913/letmefly-2434shi-yong-ji-qi-ren-da-yin-z-js4s/" target="_blank">LeetCode题解</a>|
777777
|2437.有效时间的数目|简单|<a href="https://leetcode.cn/problems/number-of-valid-clock-times/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/09/LeetCode%202437.%E6%9C%89%E6%95%88%E6%97%B6%E9%97%B4%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130573965" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-valid-clock-times/solutions/2262532/2437you-xiao-shi-jian-de-shu-mu-by-tisfy-76x6/" target="_blank">LeetCode题解</a>|
778+
|2438.二的幂数组中查询范围内的乘积|中等|<a href="https://leetcode.cn/problems/range-product-queries-of-powers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/11/LeetCode%202438.%E4%BA%8C%E7%9A%84%E5%B9%82%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E8%AF%A2%E8%8C%83%E5%9B%B4%E5%86%85%E7%9A%84%E4%B9%98%E7%A7%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/150227536" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/range-product-queries-of-powers/solutions/3750070/letmefly-2438er-de-mi-shu-zu-zhong-cha-x-w8ic/" target="_blank">LeetCode题解</a>|
778779
|2441.与对应负数同时存在的最大正整数|简单|<a href="https://leetcode.cn/problems/largest-positive-integer-that-exists-with-its-negative/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/13/LeetCode%202441.%E4%B8%8E%E5%AF%B9%E5%BA%94%E8%B4%9F%E6%95%B0%E5%90%8C%E6%97%B6%E5%AD%98%E5%9C%A8%E7%9A%84%E6%9C%80%E5%A4%A7%E6%AD%A3%E6%95%B4%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130656433" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/largest-positive-integer-that-exists-with-its-negative/solutions/2268118/letmefly-2441yu-dui-ying-fu-shu-tong-shi-9c8j/" target="_blank">LeetCode题解</a>|
779780
|2446.判断两个事件是否存在冲突|简单|<a href="https://leetcode.cn/problems/determine-if-two-events-have-conflict/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/17/LeetCode%202446.%E5%88%A4%E6%96%AD%E4%B8%A4%E4%B8%AA%E4%BA%8B%E4%BB%B6%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%E5%86%B2%E7%AA%81/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130718458" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/determine-if-two-events-have-conflict/solutions/2272942/letmefly-2446pan-duan-liang-ge-shi-jian-glh1m/" target="_blank">LeetCode题解</a>|
780781
|2451.差值数组不同的字符串|简单|<a href="https://leetcode.cn/problems/odd-string-difference/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/25/LeetCode%202451.%E5%B7%AE%E5%80%BC%E6%95%B0%E7%BB%84%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130859334" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/odd-string-difference/solutions/2282785/letmefly-2451chai-zhi-shu-zu-bu-tong-de-ekf6t/" target="_blank">LeetCode题解</a>|
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
title: 2438.二的幂数组中查询范围内的乘积:模拟(前缀和可选)
3+
date: 2025-08-11 21:48:30
4+
tags: [题解, LeetCode, 中等, 位运算, 数组, 前缀和]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2438.二的幂数组中查询范围内的乘积:模拟(前缀和可选)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/range-product-queries-of-powers/](https://leetcode.cn/problems/range-product-queries-of-powers/)
11+
12+
<p>给你一个正整数&nbsp;<code>n</code>&nbsp;,你需要找到一个下标从&nbsp;<strong>0</strong>&nbsp;开始的数组&nbsp;<code>powers</code>&nbsp;,它包含 <strong>最少</strong>&nbsp;数目的 <code>2</code>&nbsp;的幂,且它们的和为&nbsp;<code>n</code>&nbsp;。<code>powers</code>&nbsp;数组是&nbsp;<strong>非递减</strong>&nbsp;顺序的。根据前面描述,构造&nbsp;<code>powers</code>&nbsp;数组的方法是唯一的。</p>
13+
14+
<p>同时给你一个下标从 <strong>0</strong>&nbsp;开始的二维整数数组&nbsp;<code>queries</code>&nbsp;,其中&nbsp;<code>queries[i] = [left<sub>i</sub>, right<sub>i</sub>]</code>&nbsp;,其中&nbsp;<code>queries[i]</code>&nbsp;表示请你求出满足&nbsp;<code>left<sub>i</sub> &lt;= j &lt;= right<sub>i</sub></code>&nbsp;的所有&nbsp;<code>powers[j]</code>&nbsp;的乘积。</p>
15+
16+
<p>请你返回一个数组<em>&nbsp;</em><code>answers</code>&nbsp;,长度与<em>&nbsp;</em><code>queries</code>&nbsp;的长度相同,其中<em>&nbsp;</em><code>answers[i]</code>是第<em>&nbsp;</em><code>i</code>&nbsp;个查询的答案。由于查询的结果可能非常大,请你将每个&nbsp;<code>answers[i]</code>&nbsp;都对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;<strong>取余</strong>&nbsp;。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>示例 1:</strong></p>
21+
22+
<pre><b>输入:</b>n = 15, queries = [[0,1],[2,2],[0,3]]
23+
<b>输出:</b>[2,4,64]
24+
<strong>解释:</strong>
25+
对于 n = 15 ,得到 powers = [1,2,4,8] 。没法得到元素数目更少的数组。
26+
第 1 个查询的答案:powers[0] * powers[1] = 1 * 2 = 2 。
27+
第 2 个查询的答案:powers[2] = 4 。
28+
第 3 个查询的答案:powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64 。
29+
每个答案对 10<sup>9</sup> + 7 得到的结果都相同,所以返回 [2,4,64] 。
30+
</pre>
31+
32+
<p><strong>示例 2:</strong></p>
33+
34+
<pre><b>输入:</b>n = 2, queries = [[0,0]]
35+
<b>输出:</b>[2]
36+
<strong>解释:</strong>
37+
对于 n = 2, powers = [2] 。
38+
唯一一个查询的答案是 powers[0] = 2 。答案对 10<sup>9</sup> + 7 取余后结果相同,所以返回 [2] 。
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
43+
<p><strong>提示:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
47+
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
48+
<li><code>0 &lt;= start<sub>i</sub> &lt;= end<sub>i</sub> &lt; powers.length</code></li>
49+
</ul>
50+
51+
52+
53+
## 解题方法:模拟
54+
55+
**如何将$n$拆成递增的2的幂组成的数组powers**
56+
57+
很简单,$n$二进制下哪一位(记为$i$)是$1$就将$1\lt \lt i$按顺序加入powers数组。
58+
59+
**对于一个query如何求出对应answer**
60+
61+
两种方法:
62+
63+
1. 直接暴力从query[0]累乘到query[1]并随时取模
64+
2. 前缀和的思想,使用数组perfix[j]代表$\prod_{0}^{j-1}powers[i]$
65+
66+
直接暴力的方法并不会很耗时,因为powers数组中最多30个元素;
67+
68+
前缀和的思想适用于Python这种自带大整数的编程语言,否则取模后的相除还需要取逆元。
69+
70+
### 时空复杂度分析
71+
72+
暴力:
73+
74+
+ 时间复杂度$O(\log n\times len(queries))$
75+
+ 空间复杂度$O(\log n)$
76+
77+
前缀和
78+
79+
+ 时间复杂度$O(\log n + len(queries))$
80+
+ 空间复杂度$O(\log n)$
81+
82+
### AC代码
83+
84+
#### C++
85+
86+
```cpp
87+
/*
88+
* @Author: LetMeFly
89+
* @Date: 2025-08-11 18:41:58
90+
* @LastEditors: LetMeFly.xyz
91+
* @LastEditTime: 2025-08-11 21:43:41
92+
*/
93+
typedef long long ll;
94+
const ll MOD = 1e9 + 7;
95+
96+
class Solution {
97+
public:
98+
vector<int> productQueries(int n, vector<vector<int>>& queries) {
99+
vector<int> pows;
100+
int th = 0;
101+
while (n) {
102+
if (n & 1) {
103+
pows.push_back(1 << th);
104+
}
105+
n >>= 1;
106+
th++;
107+
}
108+
vector<int> ans(queries.size());
109+
for (int i = 0; i < queries.size(); i++) {
110+
ll thisVal = 1;
111+
for (int j = queries[i][0]; j <= queries[i][1]; j++) {
112+
thisVal = thisVal * pows[j] % MOD;
113+
}
114+
ans[i] = thisVal;
115+
}
116+
return ans;
117+
}
118+
};
119+
```
120+
121+
#### Python
122+
123+
```python
124+
'''
125+
Author: LetMeFly
126+
Date: 2025-08-11 21:37:10
127+
LastEditors: LetMeFly.xyz
128+
LastEditTime: 2025-08-11 21:46:52
129+
'''
130+
from typing import List
131+
132+
class Solution:
133+
def productQueries(self, n: int, queries: List[List[int]]) -> List[int]:
134+
pows = []
135+
th = 0
136+
while n:
137+
if n & 1:
138+
pows.append(1 << th)
139+
th += 1
140+
n >>= 1
141+
perfix = [1] * (len(pows) + 1)
142+
for i in range(1, len(pows) + 1):
143+
perfix[i] = perfix[i - 1] * pows[i - 1]
144+
return [perfix[q[1] + 1] // perfix[q[0]] % 1000000007 for q in queries]
145+
```
146+
147+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/150227536)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/08/11/LeetCode%202438.%E4%BA%8C%E7%9A%84%E5%B9%82%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E8%AF%A2%E8%8C%83%E5%9B%B4%E5%86%85%E7%9A%84%E4%B9%98%E7%A7%AF/)哦~
148+
>
149+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,8 +1352,11 @@ categories: [自用]
13521352
|canary|n. 金丝雀;试验品|
13531353
|stylist|n. 发型师,形象设计师,语言风格优美的人|
13541354
|shrewd|adj. 机灵的,精明的|
1355+
|||
1356+
|prudent|adj. 谨慎的,精打细算的|
13551357

1356-
这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
1358+
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
1359+
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)
13571360

13581361
<p class="wordCounts">单词收录总数</p>
13591362

Solutions/Other-Japanese-LearningNotes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ xx型
720720
|お腹(おなか)|腹部|
721721
|あし||
722722
|体(からだ)|身体|
723+
|調子(ちょうし)|状态/身体|
723724
|手(て)||
724725
|足(あし)||
725726
|||
@@ -1427,7 +1428,7 @@ xx型
14271428
|Lサイズは小さいです。<br/>大号很小。|
14281429
|この赤いドレスがお勧めづす。<br/>推荐这件红色的礼服。|
14291430
|どのズボンにしますか。<br/>选哪条裤子呢?|
1430-
|<br/>|
1431+
|調子が悪いです。<br/>身体不舒服。|
14311432
|<br/>|
14321433
|<br/>|
14331434
|<br/>|

0 commit comments

Comments
 (0)