A - 往復パトロール / Round-Trip Patrol 解説 by admin
Gemini 3.0 FlashOverview
This is a problem of finding the total travel distance when performing \(M\) round-trip patrols from the waiting area (checkpoint \(N\)) to checkpoint \(1\).
Analysis
The key to solving this problem is to think about “how much distance is covered in one round trip.”
Takahashi travels the following route each time: 1. Go from the waiting area (checkpoint \(N\)) to checkpoint \(1\). 2. Return from checkpoint \(1\) to the waiting area (checkpoint \(N\)).
Since the travel distance from checkpoint \(i\) to \(j\) is expressed as \(|i - j|\), each travel distance is as follows: - Going: \(|N - 1| = N - 1\) seconds - Returning: \(|1 - N| = N - 1\) seconds
Therefore, the total round-trip time for one alarm is \((N - 1) + (N - 1) = 2(N - 1)\) seconds.
Since the alarm rings a total of \(M\) times, the overall total travel time can be calculated as \(M \times 2(N - 1)\) seconds.
Note on Constraints
\(N\) and \(M\) can be up to \(10^9\), which are very large values. If you try to simulate each of the \(M\) trips one by one (using a loop), it would require \(10^9\) computations, which would not fit within the time limit (TLE). However, by calculating using a formula as in this case, the answer can be obtained instantly.
Algorithm
- Read \(N\) and \(M\) from the input.
- Multiply the distance of one round trip \(2 \times (N - 1)\) by the number of trips \(M\).
- Output the result.
Complexity
- Time complexity: \(O(1)\)
- Regardless of the values of \(N\) and \(M\), the answer is obtained solely through formula computation.
- Space complexity: \(O(1)\)
- Almost no additional memory is used.
Implementation Notes
In Python, very large integers are handled automatically, but when using other programming languages (such as C++ or Java), note that the computation result can be on the order of \(2 \times 10^{18}\), so you need to use a 64-bit integer type (long long or long).
Source Code
N, M = map(int, input().split())
print(2 * M * (N - 1))
This editorial was generated by gemini-3-flash-preview.
投稿日時:
最終更新: