A - 小雨の予報 / Light Rain Forecast 解説 by admin
gpt-6-astra-mediumOverview
This problem asks you to output the precipitation for \(N\) days in order, starting from the first day’s precipitation \(A\) and increasing by \(B\) each day.
Analysis
The precipitation changes as follows:
- Day \(1\): \(A\)
- Day \(2\): \(A+B\)
- Day \(3\): \(A+2B\)
In other words, the precipitation on day \(i\) is \(A+(i-1)B\).
While you can compute this formula directly on each day, you can also process it exactly as described in the problem statement by keeping the current precipitation in a variable and adding \(B\) each time after outputting it.
For example, when \(N=4,\ A=3,\ B=2\), the precipitations are \(3,5,7,9\), and each of these should be printed on a separate line.
In this problem, straightforwardly processing day by day is sufficiently fast. However, if you add \(B\) before outputting, the first day’s precipitation will become \(A+B\). Make sure to follow the order: “output first, then add”.
Algorithm
- Read \(N,A,B\).
- Initialize a variable
xrepresenting the current precipitation with \(A\). - Repeat the following operation \(N\) times:
- Output
x. - Add \(B\) to
xto update it to the next day’s precipitation.
- Output
In the first output, x is \(A\). After that, it increases by \(B\) with each output, so on the \(i\)-th iteration, \(A+(i-1)B\) will be correctly output.
Complexity
- Time Complexity: \(O(N)\)
We perform output and addition across \(N\) iterations. - Space Complexity: \(O(1)\)
We only keep track of the current value without storing each day’s precipitation in an array.
Implementation Notes
Using
range(N)allows you to loop exactly \(N\) times.print(x)appends a newline at the end, so each day’s value is printed on a separate line as required.No special handling is needed for \(B=0\); it will simply output \(A\) every day.
The maximum possible precipitation is \(A+(N-1)B \leq 10^{11}\). Although this exceeds the range of a signed \(32\)-bit integer, Python handles arbitrarily large integers as-is.
Source Code
import sys
def main():
N, A, B = map(int, sys.stdin.readline().split())
x = A
for _ in range(N):
print(x)
x += B
main()
This editorial was generated by gpt-6-astra-medium.