Official

C - 電子すごろく / Electronic Sugoroku Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

We need to determine whether square \(G\) can be reached from square \(0\) by using the given step sizes any number of times. Instead of searching the board, we solve this using the greatest common divisor of \(N\) and each step size.

Analysis

Directly searching the board is difficult

By treating each square as a vertex and each button movement as an edge, we could find reachable squares using methods like breadth-first search (BFS).

However, the number of squares can be up to \(10^9\). Storing the visited state of each square alone would require a massive amount of memory, and there is not enough time to explore all transitions.

Therefore, we consider the properties shared by all reachable square numbers.

Reachable squares are multiples of the greatest common divisor

Let \(d=\gcd(N,A_1,A_2,\ldots,A_M)\). Then the following holds:

Square \(G\) is reachable if and only if \(G\) is a multiple of \(d\).

For example, when \(N=12\) and the step sizes are \(8, 6\), we have \(d=\gcd(12,8,6)=2\). The reachable squares are \(0,2,4,6,8,10\), and odd squares cannot be reached. In fact, square \(2\) can be reached since \((8+6)\bmod 12=2\).

Let us verify this property in both directions.

If square \(G\) is reachable, \(G\) is a multiple of \(d\)

Let the number of times each button is pressed be non-negative integers \(c_1,c_2,\ldots,c_M\). The total distance moved is \(c_1A_1+c_2A_2+\cdots+c_MA_M\).

Since each \(A_i\) is a multiple of \(d\), this sum is also a multiple of \(d\). Furthermore, since \(N\) is also a multiple of \(d\), the remainder when divided by \(N\) is also a multiple of \(d\).

Therefore, the index of any reachable square is always a multiple of \(d\).

If \(G\) is a multiple of \(d\), square \(G\) is reachable

The greatest common divisor has the property that it can be represented as a linear combination of the original numbers with integer coefficients (Bézout’s identity).

If \(G\) is a multiple of \(d\), it can be represented using some integers \(b_0,b_1,\ldots,b_M\) as \(G=b_0N+b_1A_1+\cdots+b_MA_M\).

Working modulo \(N\), the term \(b_0N\) does not affect the position. However, if any \(b_i\) is negative, it cannot directly represent the number of times a button is pressed.

To resolve this, we can add a sufficiently large multiple of \(N\) to any negative coefficient \(b_i\) to make it non-negative. When a coefficient increases by \(N\), the distance moved increases by \(NA_i\), which is a multiple of \(N\) and thus does not change the final position.

Hence, all coefficients can be made non-negative, meaning square \(G\) can indeed be reached using only valid button presses.

Early exit with Yes

The same property holds even when considering only the first \(k\) types of buttons.

Letting \(d_k=\gcd(N,A_1,\ldots,A_k)\), if \(G\) is a multiple of \(d_k\), square \(G\) can be reached using only the buttons processed so far. There is no need to use the remaining buttons.

For this reason, the code checks the condition whenever the GCD is updated and terminates as soon as the condition is satisfied.

Algorithm

  1. If \(G=0\), it can be reached without pressing any buttons, so output Yes and terminate.
  2. Set \(d=N\).
  3. Read each step size \(A_i\) in order and perform the following:
    • Update \(d\leftarrow\gcd(d,A_i)\).
    • If \(G\bmod d=0\), output Yes and terminate.
  4. If the condition is not satisfied by the end, output No.

After processing all step sizes, we have \(d=\gcd(N,A_1,\ldots,A_M)\). Therefore, the answer can be determined correctly based on the property shown above.

Complexity

Let \(V=\max(N,A_1,\ldots,A_M)\).

  • Time Complexity: \(O(M\log V)\)
    The greatest common divisor is computed at most \(M\) times.
  • Space Complexity: \(O(M)\)
    In the provided code, line.split() holds all tokens of the input line at once. The additional space required for the decision process itself, such as computing the GCD, is \(O(1)\).

Implementation Notes

  • Include \(N\) in the greatest common divisor. On a circular board, moving \(N\) squares brings you back to the original position, so taking the GCD of only the step sizes is insufficient.

  • \(G=0\) is always Yes. By handling this case first, the provided code can terminate before even reading the step sizes.

  • Even if \(A_i\geq N\), we can compute the GCD directly without modification. Since \(\gcd(d,A_i)=\gcd(d,A_i\bmod N)\) holds, it is not necessary to reduce the step sizes modulo \(N\) beforehand (note that here \(d\) is a divisor of \(N\)).

  • Even if \(G\bmod d\neq 0\) at an intermediate step, we cannot conclude No yet. Later buttons might reduce the GCD further, making the square reachable.

    Source Code

import sys
from math import gcd


def main():
    input_buffer = sys.stdin.buffer
    n, m, g = map(int, input_buffer.readline().split())

    if g == 0:
        print("Yes")
        return

    d = n
    for line in input_buffer:
        for token in line.split():
            d = gcd(d, int(token))
            if g % d == 0:
                print("Yes")
                return

    print("No")


if __name__ == "__main__":
    main()

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

posted:
last update: