A - 小雨の予報 / Light Rain Forecast 解説 by admin
gpt-6-astra-mediumOverview
Starting from the first day’s precipitation \(A\), output the values for \(N\) days in order, increasing by \(B\) each day.
Analysis
The precipitation can be obtained by adding \(B\) to the previous day’s value. Therefore, the precipitation for each day is as follows:
- Day \(1\): \(A\)
- Day \(2\): \(A+B\)
- Day \(3\): \(A+2B\)
In general, the precipitation on day \(i\) is \(A+(i-1)B\).
For example, if \(N=4,\ A=3,\ B=2\), the precipitation values are \(3,5,7,9\) in order.
For this problem, a naive approach of computing and printing the value for each day is well within the time limit. There is no need to store all the values in an array; it is sufficient to maintain only the current precipitation in a variable.
However, the maximum precipitation can be up to
\(A+(N-1)B \leq 10^9+99\times 10^9=10^{11}\).
Since this does not fit into a standard \(32\)-bit integer type, use long long in C++.
Algorithm
- Read \(N,A,B\).
- Initialize a variable
currepresenting the current precipitation to \(A\). - Repeat the following operation \(N\) times:
- Output
cur. - Add \(B\) to
cur.
- Output
The first output will be \(A\). Afterwards, \(B\) is added each time a value is output, so at the time of the next output, it will be the next day’s precipitation. By repeating this \(N\) times, we can output the required precipitation for each day in order.
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(1)\)
Implementation Notes
Use
long longfora,b, andcur, which handle precipitation.Make sure to output the value before adding \(B\). If you add it first, you will incorrectly output \(A+B\) on the first day.
Print a newline after outputting each day’s precipitation, resulting in a total of \(N\) lines.
Even if \(B=0\), the same process will correctly output \(A\) for \(N\) times.
Source Code
// 問題文から独立に実装: 前日の値に B を足していく
#include <iostream>
int main() {
int n; long long a, b; std::cin >> n >> a >> b;
long long cur = a;
for (int i = 0; i < n; ++i) { std::cout << cur << "\n"; cur += b; }
}
This editorial was generated by gpt-6-astra-medium.