公式

A - 植物の成長記録 / Plant Growth Record 解説 by admin

Gemini 3.0 Flash (Thinking)

Overview

Given \(N\) plants, each with a “current height” and “daily growth rate,” the problem asks you to find the total height of all plants after \(D\) days.

Analysis

Focusing on the \(i\)-th plant, its current height is \(A_i\) and its daily growth rate is \(B_i\). Therefore, the height \(H_i\) of the \(i\)-th plant after \(D\) days can be expressed as follows: $\(H_i = A_i + (B_i \times D)\)$

What we want to find is the total height \(S\) of all plants: $\(S = \sum_{i=1}^{N} H_i = \sum_{i=1}^{N} (A_i + B_i \times D)\)$

Using the distributive law of mathematics (or the properties of the summation symbol \(\sum\)), this expression can be transformed as follows: $\(S = (\sum_{i=1}^{N} A_i) + D \times (\sum_{i=1}^{N} B_i)\)$

In other words, if we compute the “sum of current heights” and the “sum of daily growth rates” separately, we can obtain the answer simply by multiplying the latter by \(D\) and adding them together.

Since \(N\) is at most \(10^5\), a single loop over all plants is more than sufficient. Also, the maximum possible answer is \(2 \times 10^{16}\), which is very large, but this can be handled without precision issues by using languages with arbitrary-precision integers such as Python, or by using the long long type in C++.

Algorithm

  1. Initialize a variable sum_a to hold the sum of current heights and a variable sum_b to hold the sum of growth rates, both set to \(0\).
  2. For each plant from \(1\) to \(N\), perform the following:
    • Add \(A_i\) to sum_a.
    • Add \(B_i\) to sum_b.
  3. Compute the final answer as sum_a + D * sum_b and output it.

Complexity

  • Time complexity: \(O(N)\)
    • Since we perform a number of addition operations proportional to the number of plants \(N\), this runs very efficiently.
  • Space complexity: \(O(N)\)
    • When using sys.stdin.read().split() to read all input at once into a list, memory consumption is proportional to the input size.

Implementation Notes

  • Fast I/O: Since \(N\) can be as large as \(10^5\), reading all input at once using sys.stdin.read().split() is faster than calling input() repeatedly, reducing execution time.

  • Avoiding overflow: Since the answer can reach \(2 \times 10^{16}\), depending on the programming language, you may need to use a 64-bit integer type (such as long long). In Python, the standard int type automatically handles arbitrarily large values, so no special consideration is needed.

    Source Code

import sys

def solve():
    # 全ての入力を読み込み、空白で分割してリストにする
    data = sys.stdin.read().split()
    if not data:
        return
    
    # N: 植物の本数, D: 経過日数
    N = int(data[0])
    D = int(data[1])
    
    sum_a = 0
    sum_b = 0
    
    # 各植物の現在の高さ A_i と1日あたりの成長量 B_i を集計する
    # data[2 + 2*i] が A_i, data[3 + 2*i] が B_i に対応する
    for i in range(N):
        sum_a += int(data[2 + 2 * i])
        sum_b += int(data[3 + 2 * i])
    
    # D日後の合計高さ = (現在の高さの合計) + D * (1日あたりの成長量の合計)
    ans = sum_a + D * sum_b
    print(ans)

if __name__ == '__main__':
    solve()

This editorial was generated by gemini-3-flash-thinking.

投稿日時:
最終更新: