Parcel-delivery app~20 min2s · 256 MBtarget O(log n) time, O(1) spacecomplexity
A parcel-delivery app numbers the parcels on a belt 1, 2, 3 and so on, up to a limit. One scanner checks every parcel whose number is a multiple of a first step. A second scanner checks every parcel whose number is a multiple of a second step.
A parcel that both scanners would check is checked once. A control total is the sum of the numbers of all checked parcels.
Write inspected_total(limit, first_step, second_step).
limit.first_step or a multiple of second_step.Input: limit = 20, first_step = 3, second_step = 5
Output: 98With a limit of 20 and the steps 3 and 5, the checked parcels are 3, 5, 6, 9, 10, 12, 15, 18, 20. The parcel 15 is a multiple of both and is counted once.
| Rule | Multiples up to 20 | Sum |
|---|---|---|
| Multiples of 3 | 3, 6, 9, 12, 15, 18 | 63 |
| Multiples of 5 | 5, 10, 15, 20 | 50 |
| Multiples of both (of 15) | 15 | 15 |
The total is 63 + 50 - 15 = 98.
Input: limit = 10, first_step = 2, second_step = 2
Output: 30Both steps are 2. The checked parcels are 2, 4, 6, 8 and 10, and their sum is 30.
Input: limit = 1, first_step = 2, second_step = 3
Output: 0With a limit of 1 no number is a multiple of 2 or 3, so the total is 0.
1 ≤ limit ≤ 10^82 ≤ first_step, second_step ≤ 1000A loop over every number from 1 to the limit takes 10^8 steps. That is about the most a judge allows in 2 seconds, and in a slower language it is too much. Do less work per number, or stop looping.
The total can reach about 2.5 x 10^15, so it needs a 64-bit number.