Official

A - 化繊のリボン / Synthetic Fiber Ribbon Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

We determine whether the remaining length becomes \(0\) after cutting off as many ribbons of length \(B\) as possible from a ribbon of length \(A\).

Analysis

Each time we cut off a ribbon of length \(B\), the remaining length decreases by \(B\). Therefore, we can find the answer by directly simulating the operations described in the problem statement.

For example, when \(A=12,\ B=4\), the remaining length transitions as: \(12 \rightarrow 8 \rightarrow 4 \rightarrow 0\) Since there is no remainder, the answer is Yes.

On the other hand, when \(A=14,\ B=4\), it transitions as: \(14 \rightarrow 10 \rightarrow 6 \rightarrow 2\) The remaining length \(2\) is shorter than \(B\) and cannot be cut any further, so the answer is No.

Even with this naive approach, under the given constraints, the number of cuts is at most \(1000\), so it runs fast enough. No special optimizations are necessary.

Note that the final remaining length is equal to the remainder of \(A\) divided by \(B\). Therefore, we could also determine the answer using A % B == 0, but the presented code computes it through repeated subtraction.

Algorithm

  1. Read \(A,\ B\), and keep track of the remaining length in a variable a.
  2. While a >= b, we can cut off a ribbon of length \(B\), so subtract b from a.
  3. When the loop terminates, the remaining length is at least \(0\) and strictly less than \(B\).
    • If a == 0, there is no remainder, so output Yes.
    • Otherwise, a remainder is left, so output No.

Each iteration corresponds to the operation of cutting off one ribbon, and we continue the operation as long as a ribbon can be cut. Therefore, the value of a upon termination is the remaining length after cutting off as many ribbons as possible.

Complexity

  • Time Complexity: \(O(1+\lfloor A/B \rfloor)\)
    The subtraction is performed \(\lfloor A/B \rfloor\) times.
  • Space Complexity: \(O(1)\)
    A constant number of variables is used.

Implementation Notes

  • The loop condition should be a >= b. If it is set to a > b, the last ribbon cannot be cut when the remaining length is exactly \(B\), leading to an incorrect result.

  • If \(A<B\), the loop is not executed at all. The positive length \(A\) remains as is, correctly resulting in No.

  • Since \(B \geq 1\), a is guaranteed to decrease with each operation, ensuring that the loop will terminate.

  • Ensure the output strictly matches the specified format, including capitalization (Yes or No).

    Source Code

// 問題文から独立に実装: B ずつ引いていき、残りが 0 で終わるか
#include <iostream>
int main() {
    int a, b; std::cin >> a >> b;
    while (a >= b) a -= b;
    std::cout << (a == 0 ? "Yes" : "No") << std::endl;
}

This editorial was generated by gpt-6-astra-medium.

posted:
last update: