2438. Range Product Queries of Powers
Description
Given a positive integer n
, there exists a 0-indexed array called powers
, composed of the minimum number of powers of 2
that sum to n
. The array is sorted in non-decreasing order, and there is only one way to form the array.
You are also given a 0-indexed 2D integer array queries
, where queries[i] = [lefti, righti]
. Each queries[i]
represents a query where you have to find the product of all powers[j]
with lefti <= j <= righti
.
Return an array answers
, equal in length to queries
, where answers[i]
is the answer to the ith
query. Since the answer to the ith
query may be too large, each answers[i]
should be returned modulo 109 + 7
.
Example 1:
Input: n = 15, queries = [[0,1],[2,2],[0,3]] Output: [2,4,64] Explanation: For n = 15, powers = [1,2,4,8]. It can be shown that powers cannot be a smaller size. Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2. Answer to 2nd query: powers[2] = 4. Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64. Each answer modulo 109 + 7 yields the same answer, so [2,4,64] is returned.
Example 2:
Input: n = 2, queries = [[0,0]] Output: [2] Explanation: For n = 2, powers = [2]. The answer to the only query is powers[0] = 2. The answer modulo 109 + 7 is the same, so [2] is returned.
Constraints:
1 <= n <= 109
1 <= queries.length <= 105
0 <= starti <= endi < powers.length
Solutions
Solution 1: Bit Manipulation + Simulation
We can use bit manipulation (lowbit) to obtain the \(\textit{powers}\) array, and then use simulation to find the answer for each query.
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
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)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|