#3145

Find Products of Elements of Big Array

hard · 24.6% accepted · 62 likes · top 4%

array · binary search · bit manipulation

Description

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] = [fromi, toi, modi] you should calculate (big_nums[fromi] * big_nums[fromi + 1] * ... * big_nums[toi]) % modi.

Return an integer array answer such that answer[i] is the answer to the ith query.

Solution