Skip to content

feat: add solutions to lc problem: No.2438 #4640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 83 additions & 24 deletions solution/2400-2499/2438.Range Product Queries of Powers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,19 @@ tags:

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

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

时间复杂度 $O(n \times \log n)$,忽略答案的空间消耗,空间复杂度 $O(\log n)$。其中 $n$ 为 $queries$ 的长度。
首先,对于给定的正整数 $n$,我们通过 $n \& -n$ 可以快速得到二进制表示中最低位的 $1$ 对应的数值,也就是当前数的最小 $2$ 的幂因子。对 $n$ 反复执行这个操作并减去该值,就能依次得到所有置位的 $2$ 的幂,形成 $\textit{powers}$ 数组。这个数组是递增的,且长度等于 $n$ 的二进制表示中 $1$ 的个数。

接下来,我们需要处理每个查询。对于当前查询 $(l, r)$,我们需要计算

$$
\textit{answers}[i] = \prod_{j=l}^{r} \textit{powers}[j]
$$

其中 $\textit{answers}[i]$ 是第 $i$ 个查询的答案。由于查询的结果可能非常大,我们需要对每个答案取模 $10^9 + 7$。

时间复杂度 $O(m \times \log n)$,其中 $m$ 为数组 $\textit{queries}$ 的长度。忽略答案的空间消耗,空间复杂度 $O(\log n)$。

<!-- tabs:start -->

Expand All @@ -87,8 +97,8 @@ class Solution:
ans = []
for l, r in queries:
x = 1
for y in powers[l : r + 1]:
x = (x * y) % mod
for i in range(l, r + 1):
x = x * powers[i] % mod
ans.append(x)
return ans
```
Expand All @@ -97,21 +107,21 @@ class Solution:

```java
class Solution {
private static final int MOD = (int) 1e9 + 7;

public int[] productQueries(int n, int[][] queries) {
int[] powers = new int[Integer.bitCount(n)];
for (int i = 0; n > 0; ++i) {
int x = n & -n;
powers[i] = x;
n -= x;
}
int[] ans = new int[queries.length];
for (int i = 0; i < ans.length; ++i) {
long x = 1;
int m = queries.length;
int[] ans = new int[m];
final int mod = (int) 1e9 + 7;
for (int i = 0; i < m; ++i) {
int l = queries[i][0], r = queries[i][1];
long x = 1;
for (int j = l; j <= r; ++j) {
x = (x * powers[j]) % MOD;
x = x * powers[j] % mod;
}
ans[i] = (int) x;
}
Expand All @@ -125,23 +135,22 @@ class Solution {
```cpp
class Solution {
public:
const int mod = 1e9 + 7;

vector<int> productQueries(int n, vector<vector<int>>& queries) {
vector<int> powers;
while (n) {
int x = n & -n;
powers.emplace_back(x);
powers.push_back(x);
n -= x;
}
vector<int> ans;
for (auto& q : queries) {
const int mod = 1e9 + 7;
for (const auto& q : queries) {
int l = q[0], r = q[1];
long long x = 1l;
long long x = 1;
for (int j = l; j <= r; ++j) {
x = (x * powers[j]) % mod;
x = x * powers[j] % mod;
}
ans.emplace_back(x);
ans.push_back(x);
}
return ans;
}
Expand All @@ -152,26 +161,76 @@ public:

```go
func productQueries(n int, queries [][]int) []int {
var mod int = 1e9 + 7
powers := []int{}
var powers []int
for n > 0 {
x := n & -n
powers = append(powers, x)
n -= x
}
ans := make([]int, len(queries))
for i, q := range queries {
const mod = 1_000_000_007
ans := make([]int, 0, len(queries))
for _, q := range queries {
l, r := q[0], q[1]
x := 1
for _, y := range powers[l : r+1] {
x = (x * y) % mod
for j := l; j <= r; j++ {
x = x * powers[j] % mod
}
ans[i] = x
ans = append(ans, x)
}
return ans
}
```

#### TypeScript

```ts
function productQueries(n: number, queries: number[][]): number[] {
const powers: number[] = [];
while (n > 0) {
const x = n & -n;
powers.push(x);
n -= x;
}
const mod = 1_000_000_007;
const ans: number[] = [];
for (const [l, r] of queries) {
let x = 1;
for (let j = l; j <= r; j++) {
x = (x * powers[j]) % mod;
}
ans.push(x);
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn product_queries(mut n: i32, queries: Vec<Vec<i32>>) -> Vec<i32> {
let mut powers = Vec::new();
while n > 0 {
let x = n & -n;
powers.push(x);
n -= x;
}
let modulo = 1_000_000_007;
let mut ans = Vec::with_capacity(queries.len());
for q in queries {
let l = q[0] as usize;
let r = q[1] as usize;
let mut x: i64 = 1;
for j in l..=r {
x = x * powers[j] as i64 % modulo;
}
ans.push(x as i32);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
107 changes: 83 additions & 24 deletions solution/2400-2499/2438.Range Product Queries of Powers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,19 @@ The answer to the only query is powers[0] = 2. The answer modulo 10<sup>9</sup>

### Solution 1: Bit Manipulation + Simulation

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

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$.
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$.

Next, we need to process each query. For the current query $(l, r)$, we need to calculate

$$
\textit{answers}[i] = \prod_{j=l}^{r} \textit{powers}[j]
$$

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.

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)$.

<!-- tabs:start -->

Expand All @@ -87,8 +97,8 @@ class Solution:
ans = []
for l, r in queries:
x = 1
for y in powers[l : r + 1]:
x = (x * y) % mod
for i in range(l, r + 1):
x = x * powers[i] % mod
ans.append(x)
return ans
```
Expand All @@ -97,21 +107,21 @@ class Solution:

```java
class Solution {
private static final int MOD = (int) 1e9 + 7;

public int[] productQueries(int n, int[][] queries) {
int[] powers = new int[Integer.bitCount(n)];
for (int i = 0; n > 0; ++i) {
int x = n & -n;
powers[i] = x;
n -= x;
}
int[] ans = new int[queries.length];
for (int i = 0; i < ans.length; ++i) {
long x = 1;
int m = queries.length;
int[] ans = new int[m];
final int mod = (int) 1e9 + 7;
for (int i = 0; i < m; ++i) {
int l = queries[i][0], r = queries[i][1];
long x = 1;
for (int j = l; j <= r; ++j) {
x = (x * powers[j]) % MOD;
x = x * powers[j] % mod;
}
ans[i] = (int) x;
}
Expand All @@ -125,23 +135,22 @@ class Solution {
```cpp
class Solution {
public:
const int mod = 1e9 + 7;

vector<int> productQueries(int n, vector<vector<int>>& queries) {
vector<int> powers;
while (n) {
int x = n & -n;
powers.emplace_back(x);
powers.push_back(x);
n -= x;
}
vector<int> ans;
for (auto& q : queries) {
const int mod = 1e9 + 7;
for (const auto& q : queries) {
int l = q[0], r = q[1];
long long x = 1l;
long long x = 1;
for (int j = l; j <= r; ++j) {
x = (x * powers[j]) % mod;
x = x * powers[j] % mod;
}
ans.emplace_back(x);
ans.push_back(x);
}
return ans;
}
Expand All @@ -152,26 +161,76 @@ public:

```go
func productQueries(n int, queries [][]int) []int {
var mod int = 1e9 + 7
powers := []int{}
var powers []int
for n > 0 {
x := n & -n
powers = append(powers, x)
n -= x
}
ans := make([]int, len(queries))
for i, q := range queries {
const mod = 1_000_000_007
ans := make([]int, 0, len(queries))
for _, q := range queries {
l, r := q[0], q[1]
x := 1
for _, y := range powers[l : r+1] {
x = (x * y) % mod
for j := l; j <= r; j++ {
x = x * powers[j] % mod
}
ans[i] = x
ans = append(ans, x)
}
return ans
}
```

#### TypeScript

```ts
function productQueries(n: number, queries: number[][]): number[] {
const powers: number[] = [];
while (n > 0) {
const x = n & -n;
powers.push(x);
n -= x;
}
const mod = 1_000_000_007;
const ans: number[] = [];
for (const [l, r] of queries) {
let x = 1;
for (let j = l; j <= r; j++) {
x = (x * powers[j]) % mod;
}
ans.push(x);
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn product_queries(mut n: i32, queries: Vec<Vec<i32>>) -> Vec<i32> {
let mut powers = Vec::new();
while n > 0 {
let x = n & -n;
powers.push(x);
n -= x;
}
let modulo = 1_000_000_007;
let mut ans = Vec::with_capacity(queries.len());
for q in queries {
let l = q[0] as usize;
let r = q[1] as usize;
let mut x: i64 = 1;
for j in l..=r {
x = x * powers[j] as i64 % modulo;
}
ans.push(x as i32);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
class Solution {
public:
const int mod = 1e9 + 7;

vector<int> productQueries(int n, vector<vector<int>>& queries) {
vector<int> powers;
while (n) {
int x = n & -n;
powers.emplace_back(x);
powers.push_back(x);
n -= x;
}
vector<int> ans;
for (auto& q : queries) {
const int mod = 1e9 + 7;
for (const auto& q : queries) {
int l = q[0], r = q[1];
long long x = 1l;
long long x = 1;
for (int j = l; j <= r; ++j) {
x = (x * powers[j]) % mod;
x = x * powers[j] % mod;
}
ans.emplace_back(x);
ans.push_back(x);
}
return ans;
}
};
};
Loading