Parcel-delivery app~15 min2s · 256 MBtarget O(n + q) time, O(n) spacenumber-theory
A freight hub has a row of sorting routes, and each route starts a shift with one batch of parcels. A splitter on route i cuts every batch into fans[i] smaller batches, and it runs stages[i] times in a row. So route i ends with fans[i] to the power stages[i] batches, and 0 stages leaves its one batch.
Trays carry two batches each, so an odd total leaves one tray half empty. The supervisor asks many questions, each about a stretch of routes. For each one, tell whether the total number of batches is odd.
Write batch_total_parities(fans, stages, queries).
[left, right]: the routes from left to right, both included, counting from 0.Input: fans = [3, 4, 5, 2], stages = [2, 3, 0, 5], queries = [[0, 2], [1, 3], [2, 2], [0, 3]]
Output: [0, 1, 1, 0]The four routes hold 9, 64, 1 and 32 batches. Take the first question, routes 0 to 2.
| Route | fans |
stages |
Batches | Odd? |
|---|---|---|---|---|
| 0 | 3 | 2 | 9 | yes |
| 1 | 4 | 3 | 64 | no |
| 2 | 5 | 0 | 1 | yes |
The total is 9 + 64 + 1 = 74, which is even, so the answer is 0.
Input: fans = [6], stages = [0], queries = [[0, 0]]
Output: [1]With 0 stages the route holds 6 to the power 0, which is 1 batch. One batch is odd, even though the fan 6 is even.
Input: fans = [7, 7, 7], stages = [100, 200, 300], queries = [[0, 2]]
Output: [1]Each route holds an odd number of batches, and three odd numbers add up to an odd total.
1 ≤ n ≤ 10^5 routes, where n is the length of fans and stages1 ≤ fans[i] ≤ 10^9 and 0 ≤ stages[i] ≤ 10^90 ≤ q ≤ 10^5 questions, with 0 ≤ left ≤ right < nA route with fans[i] = 10^9 and stages[i] = 10^9 holds a number with about 9 x 10^9 digits. No program can write that down, let alone add 10^5 of them.
The question only asks odd or even, which is the last bit of the total. The last bit of a sum and of a power follow simple rules, so the huge numbers never need to exist.