- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 41 lines of Python from the credited upstream file 3145.py.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution:2 def findProductsOfElements(self, queries: list[list[int]]) -> list[int]:3 def sumBitsTill(x: int) -> int:4 """Returns sum(i.bit_count()), where 1 <= i <= x."""5 sumBits = 06 powerOfTwo = 17 while powerOfTwo <= x:8 sumBits += (x (2 * powerOfTwo)) * powerOfTwo9 sumBits += max(0, x % (2 * powerOfTwo) + 1 - powerOfTwo)10 powerOfTwo *= 211 return sumBits12 13 def sumPowersTill(x: int) -> int:14 """Returns sum(all powers of i), where 1 <= i <= x."""15 sumPowers = 016 powerOfTwo = 117 for power in range(x.bit_length()):18 sumPowers += (x (2 * powerOfTwo)) * powerOfTwo * power19 sumPowers += max(0, x % (2 * powerOfTwo) + 1 - powerOfTwo) * power20 powerOfTwo *= 221 return sumPowers22 23 def sumPowersFirstKBigNums(k: int) -> int:24 """Returns the sum of powers of the first k numbers in `big_nums`."""25 26 num = bisect.bisect_left(range(k), k, key=sumBitsTill)27 sumPowers = sumPowersTill(num - 1)28 remainingCount = k - sumBitsTill(num - 1)29 for power in range(num.bit_length()):30 if num >> power & 1:31 sumPowers += power32 remainingCount -= 133 if remainingCount == 0:34 break35 return sumPowers36 37 return [pow(2,38 sumPowersFirstKBigNums(b + 1) -39 sumPowersFirstKBigNums(a), mod)40 for a, b, mod in queries]41