Skip to content

Commit 0bfd273

Browse files
committed
feat: add solutions to lc problem: No.2438
No.2438.Range Product Queries of Powers
1 parent 09c0480 commit 0bfd273

File tree

8 files changed

+230
-73
lines changed

8 files changed

+230
-73
lines changed

solution/2400-2499/2438.Range Product Queries of Powers/README.md

Lines changed: 83 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,19 @@ tags:
6767

6868
### 方法一:位运算 + 模拟
6969

70-
我们先通过位运算(lowbit)得到 powers 数组,然后通过模拟的方式求出每个查询的答案。
70+
我们可以使用位运算(Lowbit)来得到 $\textit{powers}$ 数组,然后通过模拟的方式求出每个查询的答案。
7171

72-
时间复杂度 $O(n \times \log n)$,忽略答案的空间消耗,空间复杂度 $O(\log n)$。其中 $n$ 为 $queries$ 的长度。
72+
首先,对于给定的正整数 $n$,我们通过 $n \& -n$ 可以快速得到二进制表示中最低位的 $1$ 对应的数值,也就是当前数的最小 $2$ 的幂因子。对 $n$ 反复执行这个操作并减去该值,就能依次得到所有置位的 $2$ 的幂,形成 $\textit{powers}$ 数组。这个数组是递增的,且长度等于 $n$ 的二进制表示中 $1$ 的个数。
73+
74+
接下来,我们需要处理每个查询。对于当前查询 $(l, r)$,我们需要计算
75+
76+
$$
77+
\textit{answers}[i] = \prod_{j=l}^{r} \textit{powers}[j]
78+
$$
79+
80+
其中 $\textit{answers}[i]$ 是第 $i$ 个查询的答案。由于查询的结果可能非常大,我们需要对每个答案取模 $10^9 + 7$。
81+
82+
时间复杂度 $O(m \times \log n)$,其中 $m$ 为数组 $\textit{queries}$ 的长度。忽略答案的空间消耗,空间复杂度 $O(\log n)$。
7383

7484
<!-- tabs:start -->
7585

@@ -87,8 +97,8 @@ class Solution:
8797
ans = []
8898
for l, r in queries:
8999
x = 1
90-
for y in powers[l : r + 1]:
91-
x = (x * y) % mod
100+
for i in range(l, r + 1):
101+
x = x * powers[i] % mod
92102
ans.append(x)
93103
return ans
94104
```
@@ -97,21 +107,21 @@ class Solution:
97107

98108
```java
99109
class Solution {
100-
private static final int MOD = (int) 1e9 + 7;
101-
102110
public int[] productQueries(int n, int[][] queries) {
103111
int[] powers = new int[Integer.bitCount(n)];
104112
for (int i = 0; n > 0; ++i) {
105113
int x = n & -n;
106114
powers[i] = x;
107115
n -= x;
108116
}
109-
int[] ans = new int[queries.length];
110-
for (int i = 0; i < ans.length; ++i) {
111-
long x = 1;
117+
int m = queries.length;
118+
int[] ans = new int[m];
119+
final int mod = (int) 1e9 + 7;
120+
for (int i = 0; i < m; ++i) {
112121
int l = queries[i][0], r = queries[i][1];
122+
long x = 1;
113123
for (int j = l; j <= r; ++j) {
114-
x = (x * powers[j]) % MOD;
124+
x = x * powers[j] % mod;
115125
}
116126
ans[i] = (int) x;
117127
}
@@ -125,23 +135,22 @@ class Solution {
125135
```cpp
126136
class Solution {
127137
public:
128-
const int mod = 1e9 + 7;
129-
130138
vector<int> productQueries(int n, vector<vector<int>>& queries) {
131139
vector<int> powers;
132140
while (n) {
133141
int x = n & -n;
134-
powers.emplace_back(x);
142+
powers.push_back(x);
135143
n -= x;
136144
}
137145
vector<int> ans;
138-
for (auto& q : queries) {
146+
const int mod = 1e9 + 7;
147+
for (const auto& q : queries) {
139148
int l = q[0], r = q[1];
140-
long long x = 1l;
149+
long long x = 1;
141150
for (int j = l; j <= r; ++j) {
142-
x = (x * powers[j]) % mod;
151+
x = x * powers[j] % mod;
143152
}
144-
ans.emplace_back(x);
153+
ans.push_back(x);
145154
}
146155
return ans;
147156
}
@@ -152,26 +161,76 @@ public:
152161
153162
```go
154163
func productQueries(n int, queries [][]int) []int {
155-
var mod int = 1e9 + 7
156-
powers := []int{}
164+
var powers []int
157165
for n > 0 {
158166
x := n & -n
159167
powers = append(powers, x)
160168
n -= x
161169
}
162-
ans := make([]int, len(queries))
163-
for i, q := range queries {
170+
const mod = 1_000_000_007
171+
ans := make([]int, 0, len(queries))
172+
for _, q := range queries {
164173
l, r := q[0], q[1]
165174
x := 1
166-
for _, y := range powers[l : r+1] {
167-
x = (x * y) % mod
175+
for j := l; j <= r; j++ {
176+
x = x * powers[j] % mod
168177
}
169-
ans[i] = x
178+
ans = append(ans, x)
170179
}
171180
return ans
172181
}
173182
```
174183

184+
#### TypeScript
185+
186+
```ts
187+
function productQueries(n: number, queries: number[][]): number[] {
188+
const powers: number[] = [];
189+
while (n > 0) {
190+
const x = n & -n;
191+
powers.push(x);
192+
n -= x;
193+
}
194+
const mod = 1_000_000_007;
195+
const ans: number[] = [];
196+
for (const [l, r] of queries) {
197+
let x = 1;
198+
for (let j = l; j <= r; j++) {
199+
x = (x * powers[j]) % mod;
200+
}
201+
ans.push(x);
202+
}
203+
return ans;
204+
}
205+
```
206+
207+
#### Rust
208+
209+
```rust
210+
impl Solution {
211+
pub fn product_queries(mut n: i32, queries: Vec<Vec<i32>>) -> Vec<i32> {
212+
let mut powers = Vec::new();
213+
while n > 0 {
214+
let x = n & -n;
215+
powers.push(x);
216+
n -= x;
217+
}
218+
let modulo = 1_000_000_007;
219+
let mut ans = Vec::with_capacity(queries.len());
220+
for q in queries {
221+
let l = q[0] as usize;
222+
let r = q[1] as usize;
223+
let mut x: i64 = 1;
224+
for j in l..=r {
225+
x = x * powers[j] as i64 % modulo;
226+
}
227+
ans.push(x as i32);
228+
}
229+
ans
230+
}
231+
}
232+
```
233+
175234
<!-- tabs:end -->
176235

177236
<!-- solution:end -->

solution/2400-2499/2438.Range Product Queries of Powers/README_EN.md

Lines changed: 83 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,19 @@ The answer to the only query is powers[0] = 2. The answer modulo 10<sup>9</sup>
6767

6868
### Solution 1: Bit Manipulation + Simulation
6969

70-
First, we use bit manipulation (lowbit) to get the powers array, and then simulate to get the answer for each query.
70+
We can use bit manipulation (lowbit) to obtain the $\textit{powers}$ array, and then use simulation to find the answer for each query.
7171

72-
The time complexity is $O(n \times \log n)$, ignoring the space consumption of the answer, the space complexity is $O(\log n)$. Here, $n$ is the length of $queries$.
72+
First, for a given positive integer $n$, we can quickly obtain the value corresponding to the lowest bit $1$ in the binary representation through $n \& -n$, which is the minimum power of $2$ factor of the current number. By repeatedly performing this operation on $n$ and subtracting this value, we can sequentially obtain all the powers of $2$ corresponding to the set bits, forming the $\textit{powers}$ array. This array is in increasing order, and its length equals the number of $1$s in the binary representation of $n$.
73+
74+
Next, we need to process each query. For the current query $(l, r)$, we need to calculate
75+
76+
$$
77+
\textit{answers}[i] = \prod_{j=l}^{r} \textit{powers}[j]
78+
$$
79+
80+
where $\textit{answers}[i]$ is the answer to the $i$-th query. Since the query results can be very large, we need to take modulo $10^9 + 7$ for each answer.
81+
82+
The time complexity is $O(m \times \log n)$, where $m$ is the length of the array $\textit{queries}$. Ignoring the space consumption of the answer, the space complexity is $O(\log n)$.
7383

7484
<!-- tabs:start -->
7585

@@ -87,8 +97,8 @@ class Solution:
8797
ans = []
8898
for l, r in queries:
8999
x = 1
90-
for y in powers[l : r + 1]:
91-
x = (x * y) % mod
100+
for i in range(l, r + 1):
101+
x = x * powers[i] % mod
92102
ans.append(x)
93103
return ans
94104
```
@@ -97,21 +107,21 @@ class Solution:
97107

98108
```java
99109
class Solution {
100-
private static final int MOD = (int) 1e9 + 7;
101-
102110
public int[] productQueries(int n, int[][] queries) {
103111
int[] powers = new int[Integer.bitCount(n)];
104112
for (int i = 0; n > 0; ++i) {
105113
int x = n & -n;
106114
powers[i] = x;
107115
n -= x;
108116
}
109-
int[] ans = new int[queries.length];
110-
for (int i = 0; i < ans.length; ++i) {
111-
long x = 1;
117+
int m = queries.length;
118+
int[] ans = new int[m];
119+
final int mod = (int) 1e9 + 7;
120+
for (int i = 0; i < m; ++i) {
112121
int l = queries[i][0], r = queries[i][1];
122+
long x = 1;
113123
for (int j = l; j <= r; ++j) {
114-
x = (x * powers[j]) % MOD;
124+
x = x * powers[j] % mod;
115125
}
116126
ans[i] = (int) x;
117127
}
@@ -125,23 +135,22 @@ class Solution {
125135
```cpp
126136
class Solution {
127137
public:
128-
const int mod = 1e9 + 7;
129-
130138
vector<int> productQueries(int n, vector<vector<int>>& queries) {
131139
vector<int> powers;
132140
while (n) {
133141
int x = n & -n;
134-
powers.emplace_back(x);
142+
powers.push_back(x);
135143
n -= x;
136144
}
137145
vector<int> ans;
138-
for (auto& q : queries) {
146+
const int mod = 1e9 + 7;
147+
for (const auto& q : queries) {
139148
int l = q[0], r = q[1];
140-
long long x = 1l;
149+
long long x = 1;
141150
for (int j = l; j <= r; ++j) {
142-
x = (x * powers[j]) % mod;
151+
x = x * powers[j] % mod;
143152
}
144-
ans.emplace_back(x);
153+
ans.push_back(x);
145154
}
146155
return ans;
147156
}
@@ -152,26 +161,76 @@ public:
152161
153162
```go
154163
func productQueries(n int, queries [][]int) []int {
155-
var mod int = 1e9 + 7
156-
powers := []int{}
164+
var powers []int
157165
for n > 0 {
158166
x := n & -n
159167
powers = append(powers, x)
160168
n -= x
161169
}
162-
ans := make([]int, len(queries))
163-
for i, q := range queries {
170+
const mod = 1_000_000_007
171+
ans := make([]int, 0, len(queries))
172+
for _, q := range queries {
164173
l, r := q[0], q[1]
165174
x := 1
166-
for _, y := range powers[l : r+1] {
167-
x = (x * y) % mod
175+
for j := l; j <= r; j++ {
176+
x = x * powers[j] % mod
168177
}
169-
ans[i] = x
178+
ans = append(ans, x)
170179
}
171180
return ans
172181
}
173182
```
174183

184+
#### TypeScript
185+
186+
```ts
187+
function productQueries(n: number, queries: number[][]): number[] {
188+
const powers: number[] = [];
189+
while (n > 0) {
190+
const x = n & -n;
191+
powers.push(x);
192+
n -= x;
193+
}
194+
const mod = 1_000_000_007;
195+
const ans: number[] = [];
196+
for (const [l, r] of queries) {
197+
let x = 1;
198+
for (let j = l; j <= r; j++) {
199+
x = (x * powers[j]) % mod;
200+
}
201+
ans.push(x);
202+
}
203+
return ans;
204+
}
205+
```
206+
207+
#### Rust
208+
209+
```rust
210+
impl Solution {
211+
pub fn product_queries(mut n: i32, queries: Vec<Vec<i32>>) -> Vec<i32> {
212+
let mut powers = Vec::new();
213+
while n > 0 {
214+
let x = n & -n;
215+
powers.push(x);
216+
n -= x;
217+
}
218+
let modulo = 1_000_000_007;
219+
let mut ans = Vec::with_capacity(queries.len());
220+
for q in queries {
221+
let l = q[0] as usize;
222+
let r = q[1] as usize;
223+
let mut x: i64 = 1;
224+
for j in l..=r {
225+
x = x * powers[j] as i64 % modulo;
226+
}
227+
ans.push(x as i32);
228+
}
229+
ans
230+
}
231+
}
232+
```
233+
175234
<!-- tabs:end -->
176235

177236
<!-- solution:end -->
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
class Solution {
22
public:
3-
const int mod = 1e9 + 7;
4-
53
vector<int> productQueries(int n, vector<vector<int>>& queries) {
64
vector<int> powers;
75
while (n) {
86
int x = n & -n;
9-
powers.emplace_back(x);
7+
powers.push_back(x);
108
n -= x;
119
}
1210
vector<int> ans;
13-
for (auto& q : queries) {
11+
const int mod = 1e9 + 7;
12+
for (const auto& q : queries) {
1413
int l = q[0], r = q[1];
15-
long long x = 1l;
14+
long long x = 1;
1615
for (int j = l; j <= r; ++j) {
17-
x = (x * powers[j]) % mod;
16+
x = x * powers[j] % mod;
1817
}
19-
ans.emplace_back(x);
18+
ans.push_back(x);
2019
}
2120
return ans;
2221
}
23-
};
22+
};

0 commit comments

Comments
 (0)