A - 倍数の報酬 / Reward of Multiples Editorial by admin
Claude 4.5 OpusOverview
Given the compensation amounts of \(N\) employees, the problem asks to find the sum of compensation amounts for employees whose compensation is a multiple of \(K\).
Analysis
Problem Breakdown
What this problem requires can be broken down into 2 steps: 1. Determine whether each employee’s compensation \(P_i\) is a multiple of \(K\) 2. Sum up all compensation amounts that satisfy the condition
How to Determine Multiples
Whether a number \(P_i\) is a multiple of \(K\) can be determined using the remainder (modulo).
If the remainder when dividing \(P_i\) by \(K\) is \(0\), then \(P_i\) is a multiple of \(K\).
For example, when \(K = 3\): - \(P_i = 6\) → \(6 \div 3 = 2\) remainder \(0\) → multiple of \(K\) - \(P_i = 7\) → \(7 \div 3 = 2\) remainder \(1\) → not a multiple of \(K\)
Why This Approach Is Sufficient
A straightforward approach of “checking each employee one by one” is sufficient.
- The number of employees \(N\) is at most \(2 \times 10^5\)
- For each employee, we only perform a multiple check (one division operation)
- This totals about \(2 \times 10^5\) calculations, which fits within the time limit
No special techniques or algorithms are needed; this can be solved with a simple brute-force search.
Algorithm
- Read \(N\) and \(K\) from input
- Read the list of compensation amounts \(P\) from input
- Initialize the sum to \(0\)
- For each compensation amount \(P_i\):
- If \(P_i \mod K = 0\) (\(P_i\) is divisible by \(K\)), add \(P_i\) to the sum
- Output the final sum
Concrete Example
Sample input: \(N = 5\), \(K = 3\), \(P = [6, 7, 9, 4, 12]\)
| Employee | Compensation \(P_i\) | \(P_i \mod 3\) | Multiple of \(K\)? |
|---|---|---|---|
| 1 | 6 | 0 | ✓ |
| 2 | 7 | 1 | ✗ |
| 3 | 9 | 0 | ✓ |
| 4 | 4 | 1 | ✗ |
| 5 | 12 | 0 | ✓ |
Sum: \(6 + 9 + 12 = 27\)
Complexity
- Time complexity: \(O(N)\)
- For each of the \(N\) employees, we perform a multiple check (constant time) and addition
- Space complexity: \(O(N)\)
- Required to store the list of compensation amounts
Implementation Notes
Modulo operator
%: In Python,p % K == 0can determine whether \(p\) is a multiple of \(K\)Generator expression: Writing
sum(p for p in P if p % K == 0)allows you to concisely express the sum of elements satisfying the condition in one lineWhen no employees qualify: If no employees satisfy the condition,
sum()automatically returns \(0\), so no special handling is neededSource Code
N, K = map(int, input().split())
P = list(map(int, input().split()))
total = sum(p for p in P if p % K == 0)
print(total)
This editorial was generated by claude4.5opus.
posted:
last update: