B - 水泳の練習 / Swimming Practice Editorial by admin
swe-2-highSolution
Takahashi can initially swim \(A\) meters. After \(n\) practice sessions he can swim \(A + Bn\) meters. We need the smallest non-negative integer \(n\) satisfying \(A + Bn \geq C\).
- If \(A \geq C\), the goal is already achieved, so \(n = 0\).
- If \(A < C\), rearranging gives \(Bn \geq C - A\), i.e., \(n \geq \dfrac{C - A}{B}\). Hence the answer is \(\left\lceil \dfrac{C - A}{B} \right\rceil\).
Ceiling division of integers can be computed as \(\left\lceil \dfrac{x}{y} \right\rceil = \left\lfloor \dfrac{x + y - 1}{y} \right\rfloor\) (for \(x \geq 0\), \(y > 0\)), so the answer is the floor division of \((C - A) + B - 1\) by \(B\).
Proof of Correctness
After \(n\) sessions the distance is \(A + Bn\) (each session adds \(B\), by induction).
Case \(A \geq C\): \(n = 0\) satisfies \(A + B \cdot 0 = A \geq C\), and since \(n\) must be non-negative, \(0\) is the minimum. The output is correct.
Case \(A < C\): Let \(n^* = \lceil (C - A)/B \rceil\) be the output. By definition of the ceiling, \(n^* \geq (C - A)/B\), so \(A + Bn^* \geq C\); thus \(n^*\) satisfies the condition. Conversely, for any integer \(n < n^*\), we have \(n \leq n^* - 1 < (C-A)/B\), so \(A + Bn < C\) and the condition fails. Therefore \(n^*\) is the smallest non-negative integer satisfying the condition, and the output is correct.
Complexity
- Time: \(O(1)\)
- Space: \(O(1)\) (only three integers are read; no additional arrays are used)