-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2438-range-product-queries-of-powers.py
More file actions
32 lines (26 loc) · 899 Bytes
/
2438-range-product-queries-of-powers.py
File metadata and controls
32 lines (26 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# time complexity: O(logn + q)
# space complexity: O(logn + q)
from typing import List
class Solution:
def productQueries(self, n: int, queries: List[List[int]]) -> List[int]:
MOD = 10 ** 9 + 7
binaryInt = bin(n)[2:][::-1]
powers = []
for i, num in enumerate(binaryInt):
if num == "1":
powers.append(2 ** i)
prefix = [1 for _ in range(len(powers) + 1)]
for i in range(len(powers)):
prefix[i + 1] = (prefix[i] * powers[i]) % MOD
result = []
for start, end in queries:
product = (prefix[end + 1] *
pow(prefix[start], MOD - 2, MOD)) % MOD
result.append(product)
return result
n = 15
queries = [[0, 1], [2, 2], [0, 3]]
print(Solution().productQueries(n, queries))
n = 2
queries = [[0, 0]]
print(Solution().productQueries(n, queries))