Official

B - 水泳の練習 / Swimming Practice Editorial by admin

gpt-6-astra-medium

Overview

We want to find the minimum number of practice sessions needed to swim at least \(C\) meters, starting from the current distance of \(A\) meters and increasing the distance by \(B\) meters per session. This can be calculated by dividing the remaining distance needed by \(B\) and rounding up.

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\), let the remaining distance be \(\mathrm{need} = C - A\). The required number of practice sessions is \(\mathrm{need}\) divided by \(B\), rounded up.

For example, when \(A = 10,\ B = 3,\ C = 18\), the remaining distance is \(8\) meters. - Practicing \(2\) times allows you to swim \(16\) meters, which does not reach the goal. - Practicing \(3\) times allows you to swim \(19\) meters, so the answer is \(3\).

If we simply compute the integer division \(8 / 3\), the fractional part is truncated to \(2\), which would be incorrect. Therefore, we use the formula for ceiling division of positive integers:

\(\left\lceil \dfrac{\mathrm{need}}{B} \right\rceil = \left\lfloor \dfrac{\mathrm{need} + B - 1}{B} \right\rfloor\)

Note that repeatedly adding \(B\) to the swimming distance until reaching the goal would also easily run within the time limit under these constraints. However, using the formula above allows us to find the answer without any loops.

Algorithm

  1. Read \(A, B, C\) as input.
  2. If \(A \geq C\), output \(0\) and terminate.
  3. Compute the remaining distance \(\mathrm{need} = C - A\).
  4. Compute the integer division \((\mathrm{need} + B - 1) / B\) and output it.

Since this value is the number of sessions required to cover the remaining distance rounded up, it represents the minimum number of practice sessions to achieve the goal.

Complexity

  • Time Complexity: \(O(1)\)
  • Space Complexity: \(O(1)\)

Implementation Notes

  • Check \(A \geq C\) first to handle the case where no practice is needed.

  • In languages like C++, integer division / truncates the decimal part when the operands are positive. Therefore, adding \(B - 1\) before dividing achieves ceiling division.

  • You can compute the exact answer using only integer arithmetic without using floating-point numbers.

    Source Code

// 問題文から独立に実装: 足りない距離を B で切り上げ割り算
#include <iostream>
int main() {
    long long a, b, c; std::cin >> a >> b >> c;
    if (a >= c) { std::cout << 0 << std::endl; return 0; }
    long long need = c - a;
    std::cout << (need + b - 1) / b << std::endl;
}

This editorial was generated by gpt-6-astra-medium.