Range Product Queries of Powers
Real-world version: Products Over Stretches of the Binary PiecesThe statement here is our own: the same inputs, outputs and technique.
The number n can be written as a sum of powers of two, each used at most once, and in exactly one way. List those powers in increasing order and call the list pieces.
Each entry of queries is a pair [from, to] naming a stretch of pieces, both ends included. Answer each with the product of the pieces in that stretch, taken modulo 10^9 + 7.
Return the answers in the order the queries are asked.
Examples
Example 1
- Input
- n = 7, queries = [[0, 2]]
- Output
- [8]
Seven splits into 1, 2 and 4, and the whole stretch multiplies to 8.
Example 2
- Input
- n = 12, queries = [[0, 0], [1, 1]]
- Output
- [4, 8]
Twelve splits into 4 and 8, and each query asks for one of them on its own.
Example 3
- Input
- n = 255, queries = [[0, 7]]
- Output
- [268435456]
The eight pieces are the powers of two from 1 up to 128, so their exponents add to 28 and the product is two to that.
Constraints
- 1 <= n <= 10^9
- 1 <= queries.length <= 10^5
- queries[i].length == 2
- 0 <= queries[i][0] <= queries[i][1]
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def product_queries(n: int, queries: list[list[int]]) -> list[int]:public int[] productQueries(int n, int[][] queries)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Maximum ORReal-world version: Boosting the Channel PanelMedium
- Minimum Operations to Make Binary Array Elements Equal to One IReal-world version: Raising Every Flag With Triple FlipsMedium
- Minimum Number of K Consecutive Bit FlipsReal-world version: Flipping Windows to Raise Every SwitchHard
- Single NumberReal-world version: The Unpaired Survey CorrectionEasy