Official

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

gpt-6-astra-medium

Overview

We determine whether it is possible to reach square \(G\) on a circular board by using the given movement distances any number of times.
Letting \(d\) be the greatest common divisor of \(N, A_1, A_2, \dots, A_M\), the answer depends on whether \(G\) is a multiple of \(d\).

Analysis

Searching the Board Directly is Difficult

If we consider each square as a vertex and the movement by buttons as edges, we could search for reachable squares using BFS or similar methods.

However, the number of squares \(N\) is up to \(10^9\). Checking \(M\) types of moves from each square would result in a time complexity of up to \(O(NM)\) and a space complexity of \(O(N)\), which will not finish in time.

Therefore, let us consider the common properties of the indices of reachable squares.

Reachable Squares Are Always Multiples of the Greatest Common Divisor

If we press the \(i\)-th button \(k_i\) times, the total distance moved is \(k_1A_1+k_2A_2+\cdots+k_MA_M\). The final square we end up on is the remainder when this value is divided by \(N\).

Here, let \(d=\gcd(N,A_1,A_2,\dots,A_M)\).

Since all \(A_i\) are multiples of \(d\), the total distance moved is also a multiple of \(d\). Moreover, since \(N\) is also a multiple of \(d\), subtracting \(N\) each time we complete a lap around the board does not change the fact that it is a multiple of \(d\).

Therefore, the index of any reachable square is always a multiple of \(d\). If \(G\) is not a multiple of \(d\), it cannot be reached.

Any Multiple of the Greatest Common Divisor Can Always Be Reached

Conversely, we can also show that any square can be reached if \(G\) is a multiple of \(d\).

The greatest common divisor has the following property:

Using integer coefficients, the greatest common divisor can be represented as a linear combination of those numbers.

This property is known as “Bézout’s identity.” In this case, using some integers \(b,c_1,\dots,c_M\), it can be expressed as \(d=bN+c_1A_1+\cdots+c_MA_M\).

If \(G\) is a multiple of \(d\), multiplying this entire equation by \(G/d\) allows us to express it similarly as \(G=b'N+k_1A_1+\cdots+k_MA_M\). In other words, \(k_1A_1+\cdots+k_MA_M\equiv G\pmod N\).

However, as it stands, the coefficients \(k_i\) could be negative. We cannot press a button a negative number of times.

To fix this, for any negative \(k_i\), we can add \(N\) until it becomes non-negative. Adding \(N\) to the coefficient increases the total movement distance by \(NA_i\), but since this is a multiple of \(N\), the final square reached remains unchanged.

Thus, all coefficients can be made non-negative—that is, valid numbers of times the buttons can actually be pressed. Therefore, if \(G\) is a multiple of \(d\), it can always be reached.

From the above, the necessary and sufficient condition for reachability is \(G\bmod d=0\).

Example

Consider the case where \(N=12\) and the movement distances of the buttons are \(8,6\).

The greatest common divisor is \(d=\gcd(12,8,6)=2\), so the reachable squares are \(0,2,4,6,8,10\).

For example, square \(2\) can be reached by pressing each button once. This is because the total distance moved is \(8+6=14\), and \(14\bmod12=2\).

On the other hand, square \(3\) cannot be reached because it is not a multiple of \(2\).

Algorithm

  1. Initialize a variable \(d\) to store the greatest common divisor with \(N\).
  2. Read each \(A_i\) and update \(d\leftarrow\gcd(d,A_i)\).
  3. After reading all inputs, output Yes if \(G\bmod d=0\), and No otherwise.

Since the greatest common divisor can be computed sequentially, there is no need to store \(A_i\) in an array.

Complexity

Let \(V=\max(N,A_1,\dots,A_M)\). Computing the greatest common divisor using the Euclidean algorithm takes \(O(\log V)\) per call.

  • Time complexity: \(O(M\log V)\)
  • Space complexity: \(O(1)\)

Implementation Notes

  • Include \(N\) in the calculation of the greatest common divisor. Because the board is circular, what matters is not the total distance moved itself, but the remainder modulo \(N\).

  • In C++17 and later, you can use gcd from <numeric>.

  • If \(G=0\), it can be reached without pressing any buttons. Since \(0\bmod d=0\) holds in the condition, no special handling is required.

  • The code does not compute the total distance moved. Since all values handled are at most \(10^9\), a 32-bit integer type (int) is sufficient.

  • Since \(M\) can be as large as \(10^6\), it is recommended to speed up standard I/O as done in the sample code.

    Source Code

#include <iostream>
#include <numeric>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M, G;
    cin >> N >> M >> G;

    int d = N;
    for (int i = 0; i < M; ++i) {
        int A;
        cin >> A;
        d = gcd(d, A);
    }

    cout << (G % d == 0 ? "Yes" : "No") << '\n';
    return 0;
}

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

posted:
last update: