公式

A - お菓子の分配 / Distribution of Sweets 解説 by admin

Qwen3-Coder-480B

Overview

There are \(N\) types of sweets, with \(A_i\) pieces of each type. The problem asks to find the number of pieces per person and the remainder when distributing these sweets equally among \(M\) people.

Analysis

This problem simply requires computing the quotient and remainder when dividing each \(A_i\) by \(M\). This can be solved using basic modular arithmetic (division).

The important thing to note is that the constraints are large. Since \(N\) can be up to \(2 \times 10^5\) and \(A_i\) and \(M\) can be up to \(10^9\), performing complex operations inside a loop could be too slow. However, since we only need to perform simple division, we can compute the answer in constant time for each element.

In Python, the // operator computes the quotient, and the % operator computes the remainder. Using these, we can easily find \(Q_i\) and \(R_i\) satisfying \(A_i = M \times Q_i + R_i\) for each \(A_i\).

Algorithm

  1. Read \(N\) and \(M\) from input.
  2. Read each number of sweets \(A_i\) in order, and for each, repeat the following:
    • \(Q_i = A_i // M\) (quotient)
    • \(R_i = A_i \% M\) (remainder)
    • Output \(Q_i\) and \(R_i\).

Since each element can be processed in constant time, this is sufficiently fast.

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(1)\) (a list is needed for the input, but no additional memory beyond that is required besides a constant amount)

Implementation Notes

  • In Python, // computes the quotient and % computes the remainder.

  • Reading all input at once into a list using input().split() makes the subsequent loop more concise.

  • No special tricks are needed; simply performing the division straightforwardly is sufficient.

    Source Code

N, M = map(int, input().split())
A = list(map(int, input().split()))

for a in A:
    Q = a // M
    R = a % M
    print(Q, R)

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

投稿日時:
最終更新: