A - 倍数の報酬 / Reward of Multiples Editorial by admin
Gemini 3.0 FlashOverview
Given the compensation amounts \(P_i\) of \(N\) employees, the problem asks to find the total sum of only those amounts that are multiples of a specified value \(K\).
Analysis
The key points for solving this problem are the following two:
How to determine multiples Whether a number \(P_i\) is a multiple of \(K\) can be determined in programming by checking “whether the remainder of dividing \(P_i\) by \(K\) is \(0\).” In most languages, this can be written as the conditional expression
P_i % K == 0using the%operator.Computational complexity and magnitude of values
- The number of employees \(N\) is at most \(2 \times 10^5\). An \(O(N)\) algorithm that checks each employee exactly once is well within the time limit.
- Since the compensation \(P_i\) can be up to \(10^9\) and the number of employees \(N\) can be up to \(2 \times 10^5\), the total sum can reach approximately \(2 \times 10^{14}\). In Python, large integers (arbitrary-precision integers) are handled automatically, so this is not an issue. However, in languages like C++, a 64-bit integer type (such as
long long) must be used.
Algorithm
- Initialize a variable
total_sumto \(0\) to hold the total sum. - Check each employee’s compensation \(P_i\) one by one in order.
- If \(P_i\) is divisible by \(K\) (
P_i % K == 0), add that value tototal_sum. - After checking all compensation amounts, output
total_sum.
Complexity
- Time complexity: \(O(N)\) Since we scan through \(N\) elements once each, the computation completes in time proportional to the input size.
- Space complexity: \(O(N)\) In this implementation, all input values are stored in a list (array), resulting in \(O(N)\). Note that if values are processed one by one as they are read, this can be reduced to \(O(1)\).
Implementation Notes
Fast I/O: Since \(N\) can be large, repeatedly calling
input()in Python may result in long execution times. By usingsys.stdin.read().split()to read all input at once, we speed up the processing.Conditional branching: We use
if p % k == 0:to ensure that values are added only when they are multiples.Source Code
import sys
def solve():
input_data = sys.stdin.read().split()
if not input_data:
return
n = int(input_data[0])
k = int(input_data[1])
p_list = map(int, input_data[2:])
total_sum = 0
for p in p_list:
if p % k == 0:
total_sum += p
print(total_sum)
if __name__ == "__main__":
solve()
This editorial was generated by gemini-3-flash-preview.
posted:
last update: