B - 水泳の練習 / Swimming Practice Editorial by admin
gpt-6-astra-mediumOverview
Starting from a distance of \(A\) meters that you can currently swim, your distance increases by \(B\) meters with each practice session. Find the minimum number of practice sessions required to be able to swim at least \(C\) meters.
Analysis
The distance you can swim after \(n\) practice sessions is \(A + nB\) meters. Therefore, we want to find the minimum non-negative integer \(n\) satisfying \(A + nB \geq C\).
First, if \(A \geq C\), the goal is already achieved, so the answer is \(0\).
If \(A < C\), you need to increase your distance by another \(C - A\) meters. Since each practice session adds \(B\) meters, the required number of sessions is \((C - A) / B\) rounded up.
For example, when \(A = 10,\ B = 3,\ C = 18\), you need \(8\) more meters. - After 2 sessions, you can only swim up to \(16\) meters. - After 3 sessions, you can swim \(19\) meters, achieving the goal.
As shown above, simply computing (C - A) // B rounds down the fractional part, which may result in fewer sessions than needed and lead to a wrong answer.
For positive integers \(x,\ B\), rounding up \(x / B\) can be computed as (x + B - 1) // B. Therefore, the answer can be obtained using the following formula:
\((C - A + B - 1) \mathbin{//} B\)
Note that simulating the process by repeatedly adding \(B\) until the distance reaches at least \(C\) is also fast enough under these constraints. However, using the formula above allows you to compute the answer without any loops.
Algorithm
- Read \(A,\ B,\ C\).
- If \(A \geq C\), output \(0\).
- Otherwise, output
(C - A + B - 1) // B.
Rounding up ensures that the shortfall is covered, and one fewer session would fall short of the goal; thus, this yields the minimum number of practice sessions.
Complexity
- Time complexity: \(O(1)\)
- Space complexity: \(O(1)\)
Implementation Notes
In Python,
//denotes integer division. For positive integers, it yields the quotient rounded down.Adding \(B - 1\) before dividing leaves the quotient unchanged if the division is exact, and increases the quotient by 1 if there is a remainder.
If the goal is already met, branch early and be sure to output \(0\).
Ceiling division can be computed using only integer arithmetic without using floating-point numbers.
Source Code
def main():
import sys
A, B, C = map(int, sys.stdin.readline().split())
if A >= C:
print(0)
else:
print((C - A + B - 1) // B)
main()
This editorial was generated by gpt-6-astra-medium.