Find Products of Elements of Big Array Problem
Description
LeetCode Problem 3145.
The powerful array of a non-negative integer x is defined as the shortest sorted array of powers of two that sum up to x. The table below illustrates examples of how the powerful array is determined. It can be proven that the powerful array of x is unique.
| num | Binary Representation | powerful array |
|---|---|---|
| 1 | 00001 | [1] |
| 8 | 01000 | [8] |
| 10 | 01010 | [2, 8] |
| 13 | 01101 | [1, 4, 8] |
| 23 | 10111 | [1, 2, 4, 16] |
The array big_nums is created by concatenating the powerful arrays for every positive integer i in ascending order: 1, 2, 3, and so on. Thus, big_nums begins as [1, 2, 1, 2, 4, 1, 4, 2, 4, 1, 2, 4, 8, ...].
You are given a 2D integer matrix queries, where for queries[i] = [from_i, to_i, mod_i] you should calculate (big_nums[from_i] * big_nums[from_i + 1] * ... * big_nums[to_i]) % mod_i.
Return an integer array answer such that answer[i] is the answer to the i^th query.
Example 1:
Input: queries = [[1,3,7]]
Output: [4]
Explanation:
There is one query.
big_nums[1..3] = [2,1,2]. The product of them is 4. The result is 4 % 7 = 4.
Example 2:
Input: queries = [[2,5,3],[7,7,4]]
Output: [2,2]
Explanation:
There are two queries.
First query: big_nums[2..5] = [1,2,4,1]. The product of them is 8. The result is 8 % 3 = 2.
Second query: big_nums[7] = 2. The result is 2 % 4 = 2.
Constraints:
1 <= queries.length <= 500queries[i].length == 30 <= queries[i][0] <= queries[i][1] <= 10^151 <= queries[i][2] <= 10^5
Solution
Approach: Binary Search + Bit Counting
Every element of big_nums is a power of two, so the product of a range is 2^power, where power is the sum of the exponents in that range. Let f(i) be the sum of the exponents of the first i elements of big_nums. Then the query [from, to, mod] equals 2^(f(to + 1) - f(from)) % mod, computed with fast exponentiation.
To compute f(i), look at how the powerful arrays are laid out. The numbers in [2^k, 2^(k+1) - 1] are the numbers in [0, 2^k - 1] with the extra bit k, so each of their arrays is the old array plus one more element, the exponent k. This gives two tables: cnt[k], the total length of the arrays of all numbers below 2^k, and s[k], the sum of their exponents.
For any x, the total length and exponent sum for the numbers [0, x] come from peeling off the highest set bit 2^k of x. The block below 2^k is read from cnt[k] and s[k]. The rest is the same problem for the smaller number x - 2^k, except that every number carries an extra exponent k (which adds (x - 2^k + 1) * k to the sum).
Now binary search the largest x whose arrays hold at most i elements in total. The remaining elements are the first few elements of the powerful array of x + 1, which are its lowest set bits, so we add their exponents one by one.
Complexity: O(q log M) time and O(log M) extra space, where q is the number of queries and M is the largest index (about 1015).
m = 50
cnt = [0] * (m + 1)
s = [0] * (m + 1)
p = 1
for i in range(1, m + 1):
cnt[i] = cnt[i - 1] * 2 + p
s[i] = s[i - 1] * 2 + p * (i - 1)
p *= 2
def num_idx_and_sum(x: int) -> tuple:
idx = 0
total_sum = 0
while x:
i = x.bit_length() - 1
idx += cnt[i]
total_sum += s[i]
x -= 1 << i
total_sum += (x + 1) * i
idx += x + 1
return (idx, total_sum)
def f(i: int) -> int:
l, r = 0, 1 << m
while l < r:
mid = (l + r + 1) >> 1
idx, _ = num_idx_and_sum(mid)
if idx < i:
l = mid
else:
r = mid - 1
total_sum = 0
idx, total_sum = num_idx_and_sum(l)
i -= idx
x = l + 1
for _ in range(i):
y = x & -x
total_sum += y.bit_length() - 1
x -= y
return total_sum
class Solution:
def findProductsOfElements(self, queries: List[List[int]]) -> List[int]:
return [pow(2, f(right + 1) - f(left), mod) for left, right, mod in queries]