Official

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

gpt-6-astra-medium

Overview

We increase the swimming distance by \(B\) meters with each practice session until it is at least \(C\) meters, and count the number of sessions.

Analysis

Each time you practice, the distance you can swim always increases by \(B\) meters. Therefore, you can find the answer by simply simulating each practice session one by one as described in the problem statement.

Even with this naive approach, it is sufficiently fast under the given constraints. The case requiring the most practice sessions is \(A=1,\ B=1,\ C=10000\), which takes \(9999\) sessions. Therefore, there is no concern of getting a TLE (Time Limit Exceeded) from the loop.

For example, when \(A=3,\ B=2,\ C=8\), the swimming distance changes as follows:

  • Before practice: \(3\) meters
  • After the 1st practice: \(5\) meters
  • After the 2nd practice: \(7\) meters
  • After the 3rd practice: \(9\) meters

The first time it reaches at least \(8\) meters is after the 3rd practice, so the answer is \(3\).

The distance does not need to equal the target exactly. It is important to repeat the process “until it is at least \(C\) meters”, rather than “until it becomes exactly \(C\) meters”.

Algorithm

  1. Read the current swimming distance as a, the distance gained per practice as b, and the target as c.
  2. Initialize count, which represents the number of practice sessions, to \(0\).
  3. While a < c, repeat the following:
    • Add b to a.
    • Increment count by \(1\).
  4. Output count.

After each iteration, a represents the distance you can swim after count practice sessions. Since we only practice while the distance is strictly less than the target and stop as soon as it reaches or exceeds the target, we obtain the minimum required number of practice sessions.

Complexity

Let \(K\) be the required number of practice sessions. If \(A \geq C\), then \(K=0\); otherwise, \(K=\left\lceil \frac{C-A}{B} \right\rceil\).

  • Time complexity: \(O(K+1)\)
    Each practice session takes \(O(1)\) constant time.
  • Space complexity: \(O(1)\)
    A constant number of variables are used.

Implementation Notes

  • Use while a < c: as the loop condition. If you use a <= c, an unnecessary extra practice session will be performed when the distance reaches the target exactly.

  • By initializing count to \(0\), if \(A \geq C\) from the beginning, the loop will not execute at all and will correctly output \(0\).

  • Since \(B \geq 1\), the swimming distance strictly increases with each practice, guaranteeing that the loop will always terminate.

    Source Code

# 問題文から独立に実装: 目標に届くまで 1 回ずつ練習を繰り返す
a, b, c = map(int, input().split())
count = 0
while a < c:
    a += b
    count += 1
print(count)

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