A - アルバイトの給料計算 / Calculating Part-Time Job Pay 解説 by admin
Gemini 3.0 Flash (Thinking)Overview
This is a problem where, for \(N\) types of part-time jobs, you calculate “hourly wage \(\times\) working hours” for each and find the total sum.
Analysis
The salary earned from each part-time job can be calculated as follows: - 1st part-time job: \(A_1 \times T_1\) yen - 2nd part-time job: \(A_2 \times T_2\) yen - … - \(N\)-th part-time job: \(A_N \times T_N\) yen
The sum of all these values is the total salary Takahashi receives this month. Expressed as a formula, the answer is \(\sum_{i=1}^{N} (A_i \times T_i)\).
Constraints and Notes
- Computational complexity: Since \(N\) is at most \(10^5\), computing and summing each product one by one \(N\) times is well within the time limit.
- Magnitude of values: The maximum possible answer is approximately \(N \times A_i \times T_i = 10^5 \times 10^6 \times 10^6 = 10^{17}\). Although this is a very large number, Python natively supports arbitrary-precision integers (a type that can handle arbitrarily large numbers), so you can compute this directly without any special handling and still get the correct answer.
Algorithm
- Initialize a variable
total_salaryto \(0\) to hold the total amount. - Read \(N\) from the input.
- Repeat the following process \(N\) times (\(i = 1, 2, \dots, N\)):
- Read the hourly wage \(A_i\) and working hours \(T_i\) for the \(i\)-th part-time job.
- Calculate \(A_i \times T_i\) and add the result to
total_salary.
- Output the final value of
total_salary.
Complexity
- Time complexity: \(O(N)\) Since we perform one multiplication and one addition for each of the \(N\) part-time jobs, the processing time is proportional to \(N\).
- Space complexity: \(O(N)\)
In this implementation, we use
sys.stdin.read().split()to store all input data into a list at once, so the memory usage is proportional to the number of inputs \(N\).
Implementation Notes
Fast input: Since \(N\) can be as large as \(10^5\), reading all input at once using
sys.stdin.read().split()is faster than callinginput()\(N\) times.Index management: When reading input all at once, the 0th element of the list is \(N\), and from the 1st element onward, the values are arranged as \(A_1, T_1, A_2, T_2, \dots\). It is important to correctly specify the position of the \(i\)-th part-time job’s information in the list using an expression like
2 * i + 1.Source Code
import sys
def main():
# 標準入力からすべてのデータを読み込み、空白文字で分割します
input_data = sys.stdin.read().split()
# 入力が空の場合は終了します
if not input_data:
return
# 最初の値はアルバイトの種類数 N です
n = int(input_data[0])
total_salary = 0
# 各アルバイトの時給 A_i と 働いた時間 T_i を順に処理します
for i in range(n):
# input_data[1] から順に A1, T1, A2, T2, ... と並んでいます
a = int(input_data[2 * i + 1])
t = int(input_data[2 * i + 2])
# 給料を加算します
total_salary += a * t
# 合計金額を出力します
print(total_salary)
if __name__ == '__main__':
main()
This editorial was generated by gemini-3-flash-thinking.
投稿日時:
最終更新: