diff --git a/solution/2400-2499/2438.Range Product Queries of Powers/README.md b/solution/2400-2499/2438.Range Product Queries of Powers/README.md index 5f7f48e560173..2f64a5048db94 100644 --- a/solution/2400-2499/2438.Range Product Queries of Powers/README.md +++ b/solution/2400-2499/2438.Range Product Queries of Powers/README.md @@ -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)$。 @@ -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 ``` @@ -97,8 +107,6 @@ 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) { @@ -106,12 +114,14 @@ class Solution { 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; } @@ -125,23 +135,22 @@ class Solution { ```cpp class Solution { public: - const int mod = 1e9 + 7; - vector productQueries(int n, vector>& queries) { vector powers; while (n) { int x = n & -n; - powers.emplace_back(x); + powers.push_back(x); n -= x; } vector 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; } @@ -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 { + 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 + } +} +``` + diff --git a/solution/2400-2499/2438.Range Product Queries of Powers/README_EN.md b/solution/2400-2499/2438.Range Product Queries of Powers/README_EN.md index 9c4df7b6e5e5f..b3e14a94f5514 100644 --- a/solution/2400-2499/2438.Range Product Queries of Powers/README_EN.md +++ b/solution/2400-2499/2438.Range Product Queries of Powers/README_EN.md @@ -67,9 +67,19 @@ The answer to the only query is powers[0] = 2. The answer modulo 109 ### 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)$. @@ -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 ``` @@ -97,8 +107,6 @@ 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) { @@ -106,12 +114,14 @@ class Solution { 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; } @@ -125,23 +135,22 @@ class Solution { ```cpp class Solution { public: - const int mod = 1e9 + 7; - vector productQueries(int n, vector>& queries) { vector powers; while (n) { int x = n & -n; - powers.emplace_back(x); + powers.push_back(x); n -= x; } vector 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; } @@ -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 { + 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 + } +} +``` + diff --git a/solution/2400-2499/2438.Range Product Queries of Powers/Solution.cpp b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.cpp index cc54a96f0ca01..60c89a5851af6 100644 --- a/solution/2400-2499/2438.Range Product Queries of Powers/Solution.cpp +++ b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.cpp @@ -1,23 +1,22 @@ class Solution { public: - const int mod = 1e9 + 7; - vector productQueries(int n, vector>& queries) { vector powers; while (n) { int x = n & -n; - powers.emplace_back(x); + powers.push_back(x); n -= x; } vector 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; } -}; \ No newline at end of file +}; diff --git a/solution/2400-2499/2438.Range Product Queries of Powers/Solution.go b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.go index 160bbc6d0934e..813664397555a 100644 --- a/solution/2400-2499/2438.Range Product Queries of Powers/Solution.go +++ b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.go @@ -1,19 +1,19 @@ 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 -} \ No newline at end of file +} diff --git a/solution/2400-2499/2438.Range Product Queries of Powers/Solution.java b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.java index 6dd51760911ce..b3091fe49b43a 100644 --- a/solution/2400-2499/2438.Range Product Queries of Powers/Solution.java +++ b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.java @@ -1,6 +1,4 @@ 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) { @@ -8,15 +6,17 @@ public int[] productQueries(int n, int[][] queries) { 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; } return ans; } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2438.Range Product Queries of Powers/Solution.py b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.py index b9a2634f02ee4..df594213b79bf 100644 --- a/solution/2400-2499/2438.Range Product Queries of Powers/Solution.py +++ b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.py @@ -9,7 +9,7 @@ def productQueries(self, n: int, queries: List[List[int]]) -> List[int]: 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 diff --git a/solution/2400-2499/2438.Range Product Queries of Powers/Solution.rs b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.rs new file mode 100644 index 0000000000000..bdcd1b2abcc5e --- /dev/null +++ b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.rs @@ -0,0 +1,22 @@ +impl Solution { + pub fn product_queries(mut n: i32, queries: Vec>) -> Vec { + 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 + } +} diff --git a/solution/2400-2499/2438.Range Product Queries of Powers/Solution.ts b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.ts new file mode 100644 index 0000000000000..b983cb99dfb46 --- /dev/null +++ b/solution/2400-2499/2438.Range Product Queries of Powers/Solution.ts @@ -0,0 +1,18 @@ +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; +}