A - 当選番号の発表 / Announcement of Winning Numbers Editorial by admin
Qwen3-Coder-480BOverview
When giving prizes to visitors whose registration numbers are multiples of \(K\), output the lottery ticket numbers of those visitors in order.
Analysis
In this problem, visitors are assigned consecutive registration numbers from \(1\) to \(N\) in the order they drew the lottery, and only those whose registration numbers are multiples of \(K\) receive prizes.
For example, when \(N = 7\) and \(K = 3\), the registration numbers are \(1, 2, 3, 4, 5, 6, 7\), and the multiples of \(K\) are \(3, 6\). In other words, the 3rd and 6th visitors receive prizes.
The lottery ticket numbers of these visitors correspond to indices \(2\) and \(5\) (0-indexed) of array \(A\). Therefore, we can obtain the answer by extracting elements from array \(A\) starting at index \(K-1\) and stepping by \(K\).
A naive approach would be to check whether each visitor’s registration number is a multiple of \(K\), which works fine but is wasteful. In practice, we can directly access only the relevant indices, making it efficient.
Algorithm
- Read \(N\), \(K\), and array \(A\) from input.
- From array \(A\), extract elements at positions starting from index \(K-1\) with a step of \(K\).
- Output the extracted elements.
Specifically, by using range(K-1, N, K) in a for loop, we can enumerate the corresponding 0-indexed indices.
Complexity
- Time complexity: \(O(N / K)\) (since we only extract the necessary elements)
- Space complexity: \(O(1)\) (excluding the output list)
※ Since we use a list to store the results for output, the actual space complexity is \(O(N / K)\).
Implementation Notes
Be careful that array indices start from 0 (while registration numbers start from 1)
The start position of the range function should be
K-1, and the step should beKWhen outputting with space-separated format, be careful not to add trailing spaces (using
joinfor concatenation is safe)Source Code
N, K = map(int, input().split())
A = list(map(int, input().split()))
result = []
for i in range(K-1, N, K):
result.append(A[i])
print(' '.join(map(str, result)))
This editorial was generated by qwen3-coder-480b.
posted:
last update: