A - 小雨の予報 / Light Rain Forecast 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
Starting with an initial precipitation of \(A\) on the first day, \(B\) is added each day. Following this rule, output the precipitation for each of the \(N\) days in order.
Analysis
Writing out the precipitation for each day gives the following:
- Day \(1\): \(A\)
- Day \(2\): \(A+B\)
- Day \(3\): \(A+2B\)
By day \(i\), the amount has increased \(i-1\) times since the first day, so the precipitation on day \(i\) can be calculated as:
\(A+(i-1)B\)
For example, when \(N=4,\ A=3,\ B=2\), the outputs are \(3,\ 5,\ 7,\ 9\) in order.
A naive approach of repeatedly adding \(B\) to the previous day’s precipitation is also fast enough. In our code, we will directly compute the precipitation for each day using the formula above.
Note that on the first day, there is no increase yet. If you mistakenly compute day \(i\) as \(A+iB\), every value will be larger by \(B\).
Algorithm
- Read \(N,\ A,\ B\).
- Iterate \(i\) from \(0\) to \(N-1\).
- Compute and print \(A+Bi\), which is the precipitation for day \(i+1\).
By doing this, the precipitation from the first day to the \(N\)-th day can be printed in the correct order.
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(1)\)
The computation and printing are performed \(1\) time for each day. Since the precipitation values are not stored in an array, the additional space required is constant.
Implementation Notes
Python’s
range(n)generates \(0,1,\ldots,N-1\). Therefore, in the code,a + b * irepresents the precipitation on day \(i+1\).Calling
printinside the loop allows outputting each day’s precipitation on a new line.The maximum precipitation can be up to \(10^9+99\times10^9=10^{11}\), which exceeds the range of a \(32\)-bit integer. While Python’s
inthandles arbitrarily large integers, other languages will require a \(64\)-bit integer type.No special handling is required for edge cases such as \(N=1\) or \(B=0\).
Source Code
# 問題文から独立に実装
n, a, b = map(int, input().split())
for i in range(n):
print(a + b * i)
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: