公式

A - お買い物の合計金額 / Total Shopping Amount 解説 by admin

Qwen3-Coder-480B

Overview

This is a problem where you need to find the remainder when the total price of items is divided by a certain number \(M\).

Analysis

This problem simply requires finding the sum of all given item prices \(L_1, L_2, \ldots, L_K\) and outputting the remainder when that sum is divided by \(M\).

A straightforward approach is to add up all the prices and then divide by \(M\). Looking at the constraints, the number of items \(K\) is at most \(10^5\), and each item price \(L_i\) is at most \(10^4\), so the total amount is at most around \(10^5 \times 10^4 = 10^9\) yen, which is well within the range that Python’s standard integer type can handle. Therefore, computing the sum directly poses no issues.

The key point is that it suffices to compute the remainder just once at the end, without taking remainders along the way. This is due to the property of modular arithmetic with respect to addition: $\( (a + b) \bmod M = ((a \bmod M) + (b \bmod M)) \bmod M \)$ However, in this case, we can simply compute % M at the end, so there is no need to take remainders during intermediate steps.

Algorithm

  1. Read the number of items \(K\) and the divisor \(M\) from input.
  2. Read each item’s price \(L_1, L_2, \ldots, L_K\) as a list.
  3. Compute the sum total of all prices:
    
    total = sum(L)
    
  4. Compute the remainder of the total amount divided by \(M\) and output it:
    
    points = total % M
    print(points)
    

Example

Input:

3 100
30 70 10

The total is \(30 + 70 + 10 = 110\) yen. \(110 \bmod 100 = 10\), so the output is 10.

Complexity

  • Time complexity: \(O(K)\) — since we need to add each element once
  • Space complexity: \(O(K)\) — since we store the input prices as a list

Implementation Notes

  • Make sure to read the input correctly (multiple integers separated by spaces)

  • When computing the sum, overflow is rarely a concern in Python, but care is needed in other languages

  • Don’t forget to apply % M at the end

    Source Code

K, M = map(int, input().split())
L = list(map(int, input().split()))
total = sum(L)
points = total % M
print(points)

This editorial was generated by qwen3-coder-480b.

投稿日時:
最終更新: