A robotics workshop starts a day of drone test flights with a supply of already-charged batteries. Every flight drains exactly one charged battery, turning it into a depleted battery. The workshop shares a swap depot with other teams: whenever the pile of depleted batteries collected so far reaches a fixed exchange rate, the depot swaps exactly that many depleted batteries for one freshly charged battery (never more, never fewer at a time -- leftover depleted batteries stay in the pile and carry over toward the next swap). Flights continue, batteries keep getting depleted and swapped, until the workshop is completely out of charged batteries and cannot reach the exchange rate again. Determine the total number of batteries used (equivalently, the total number of flights flown) over the whole day.
A single line containing two integers chargedBatteries and swapRate, separated by a space.
A single integer: the total number of batteries used before the workshop runs out.
Example 1
Input
9 3
Expected
13
Explanation
Starting with 9 charged batteries, the workshop flies 9 flights, leaving 9 depleted batteries. Those exchange for 9/3 = 3 more charged batteries (0 left over), flown as 3 more flights, leaving 3 depleted, which exchange for 1 more charged battery, flown as 1 more flight, leaving 1 depleted battery -- not enough to reach the exchange rate of 3, so the workshop stops. Total flights: 9 + 3 + 1 = 13.
Example 2
Input
15 4
Expected
19
Explanation
15 charged batteries fly 15 flights, leaving 15 depleted, which exchange for 3 charged batteries (3 left over depleted) -> 3 more flights (18 total), leaving 3+3=6 depleted, exchanging for 1 charged battery (2 left over) -> 1 more flight (19 total), leaving 2+1=3 depleted -- below the exchange rate of 4, so it stops. Total flights: 19.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →