A - 小雨の予報 / Light Rain Forecast 解説 by admin 印刷ページ
swe-2-highSolution
The precipitation on day \(i\) (\(1\)-indexed) is the \(i\)-th term of an arithmetic sequence with first term \(A\) and common difference \(B\):
\[A + (i-1)B\]
So it suffices to output this value for \(i = 1, 2, \ldots, N\) in order.
In the implementation, we initialize a variable x to \(A\) and add \(B\) to it after printing each day. This generates the same sequence without evaluating the formula each time.
The maximum value is \(A + (N-1)B \le 10^9 + 99 \cdot 10^9 = 10^{11}\), which does not fit in a 32-bit integer, so in C++ we use a 64-bit integer type (long long). Python’s int has arbitrary precision, so it works as is.
Proof of correctness
Loop invariant: at the start of the \(i\)-th iteration, x \(= A + (i-1)B\) holds (for \(i = 1, \ldots, N\)).
- Initialization:
x\(= A\), and for \(i = 1\) we have \(A + 0 \cdot B = A\), so it holds. - Maintenance: the \(i\)-th iteration prints
x\(= A + (i-1)B\) and then adds \(B\), so at the start of the next iterationx\(= A + iB\). - Termination: the loop runs exactly \(N\) times.
Hence the value printed on line \(i\) is \(A + (i-1)B\), which matches the problem’s definition of the day-\(i\) precipitation (\(A\) on day 1, increasing by \(B\) each day). Therefore the program is correct.
Complexity
- Time: \(O(N)\) (one output per day)
- Space: \(O(1)\) auxiliary (no input array is stored; only the running variable
xis updated)
There is no tie-breaking involved (the output is uniquely determined).
投稿日時:
最終更新: