A - お菓子の分配 / Distribution of Sweets Editorial by admin
GPT 5.2 HighOverview
This problem asks you to divide the number of sweets of each type \(A_i\) by the number of people \(M\), and for all types \(i=1..N\), compute and output the “number of sweets each person receives (quotient)” and the “remainder.”
Analysis
Since each type of sweet is “distributed equally to everyone,” the result of dividing \(A_i\) equally among \(M\) people directly gives the answer. In other words, the pair \((Q_i, R_i)\) satisfying \(A_i = M \times Q_i + R_i\) (where \(0 \le R_i < M\)) is simply the quotient and remainder of division.
For example, when \(M=4, A_i=14\): - Each person receives \(Q_i = 3\) sweets (\(3 \times 4 = 12\) sweets distributed) - The remainder is \(R_i = 2\) sweets (\(14-12=2\))
A naive simulation such as “subtract one at a time for the distributable amount” would be extremely slow for even a single type since \(A_i\) can be up to \(10^9\), resulting in TLE. However, since the quotient and remainder can be obtained in a single division operation, each \(A_i\) can be processed in \(O(1)\).
Algorithm
- Read \(N, M\) and the array \(A\) from input.
- For each \(a \in A\):
- Compute \((q, r) = (a // M, a \bmod M)\) (in Python,
divmod(a, M)is convenient).
- Compute \((q, r) = (a // M, a \bmod M)\) (in Python,
- Output
q ron each line.
Complexity
- Time complexity: \(O(N)\) (one division per \(A_i\))
- Space complexity: \(O(N)\) (if accumulating output to print all at once at the end)
Implementation Notes
In Python, using
divmod(a, M)obtains both the quotient and remainder simultaneously, making the implementation concise.Since \(N\) can be up to \(2 \times 10^5\), using
sys.stdin.buffer.read()and"\n".join(...)to handle input/output in bulk is faster.Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
if not data:
return
N, M = data[0], data[1]
A = data[2:2+N]
out = []
for a in A:
q, r = divmod(a, M)
out.append(f"{q} {r}")
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: