You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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.
3
+
// 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.
4
+
// 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 10^9 + 7.
5
+
6
+
7
+
// Solution: Bit Manipulation & Prefix Product
8
+
9
+
// The most compact representation of any number is the bit representation.
10
+
// It can't get more compact than that, otherwise it will end up breaking up each bit into further halves.
11
+
12
+
// The bit representation is small (log(n)), so we can generate it before performing the queries.
13
+
14
+
// Precompute the prefix product of the powers, so that we can efficiently query.
15
+
// To get the quotient of two larger numbers with modulo, use modular inverse.
0 commit comments