公式

A - リボンカット / Ribbon Cut 解説 by admin

Claude 4.6 Opus (Thinking)

Overview

This problem asks you to find the number of parts obtained when cutting each ribbon to a specified length. If the leftover piece is at least half the specified length, it counts as an additional part.

Analysis

This is a straightforward problem where each ribbon can be computed independently.

Understanding through concrete examples:

  • Ribbon length \(L = 25\), specified part length \(T = 7\):

    • \(\lfloor 25 / 7 \rfloor = 3\) parts can be cut (using \(7 + 7 + 7 = 21\) millimeters)
    • Leftover \(R = 25 \bmod 7 = 4\) millimeters
    • \(\lfloor T / 2 \rfloor = \lfloor 7 / 2 \rfloor = 3\)
    • \(R = 4 \geq 3\), so the leftover can also be used as a part → 4 parts
  • Ribbon length \(L = 20\), specified part length \(T = 7\):

    • \(\lfloor 20 / 7 \rfloor = 2\) parts, leftover \(R = 6\)
    • \(\lfloor 7 / 2 \rfloor = 3\), \(R = 6 \geq 3\)3 parts
  • Ribbon length \(L = 14\), specified part length \(T = 7\):

    • \(\lfloor 14 / 7 \rfloor = 2\) parts, leftover \(R = 0\)
    • No leftover remains (\(R = 0\)), so no addition → 2 parts
  • Ribbon length \(L = 2\), specified part length \(T = 7\):

    • \(\lfloor 2 / 7 \rfloor = 0\) parts, leftover \(R = 2\)
    • \(\lfloor 7 / 2 \rfloor = 3\), \(R = 2 < 3\)0 parts

Is a naive approach sufficient?

Since we only need to compute one division and one modulo operation for each of the \(N\) ribbons, \(O(N)\) is fast enough. Although \(L_i, T_i\) can be as large as \(10^{18}\), Python natively supports arbitrary-precision integers, so there is no concern about overflow.

Algorithm

For each ribbon \(i\), do the following:

  1. Compute the quotient \(q = \lfloor L_i / T_i \rfloor\) and the remainder \(R_i = L_i \bmod T_i\).
  2. If \(R_i > 0\) and \(R_i \geq \lfloor T_i / 2 \rfloor\), then the number of parts is \(q + 1\).
  3. Otherwise (no leftover, or the leftover is too short), the number of parts is \(q\).

The check \(R_i > 0\) in the condition is important. When \(R_i = 0\) (i.e., it divides evenly), we should not count an additional part even in the case where \(\lfloor T_i / 2 \rfloor = 0\) (when \(T_i = 1\)).

Complexity

  • Time complexity: \(O(N)\) — constant-time computation for each ribbon
  • Space complexity: \(O(N)\) — storing results in a list for output

Implementation Notes

  • Output optimization: Calling print \(N\) times can be slow, so we accumulate the results in a list and output them all at once using '\n'.join(...).

  • Input optimization: Using sys.stdin.readline allows for fast reading of large inputs.

  • Integer overflow: In languages like C++, since \(L_i, T_i\) can be up to \(10^{18}\), multiplication could overflow even with long long. In Python, however, there is no limit on integer size, so computations can be done directly.

  • Condition ordering: Writing r > 0 before r >= T // 2 in the condition r > 0 and r >= T // 2 prevents a bug where, when \(T = 1\) and \(\lfloor T/2 \rfloor = 0\), an extra part would be incorrectly added even though there is no leftover.

    Source Code

import sys
input = sys.stdin.readline

N = int(input())
out = []
for _ in range(N):
    L, T = map(int, input().split())
    q = L // T
    r = L % T
    if r > 0 and r >= T // 2:
        out.append(q + 1)
    else:
        out.append(q)
print('\n'.join(map(str, out)))

This editorial was generated by claude4.6opus-thinking.

投稿日時:
最終更新: