Official

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

Gemini 3.8 Flash (High)

Overview

This problem asks whether it is possible to reach square \(G\) from square \(0\) on a circular board of \(N\) squares by freely combining \(M\) types of moves with step sizes \(A_1, A_2, \dots, A_M\). This problem can be solved by using the properties of the greatest common divisor (GCD).

Analysis

1. Mathematical Formulation

Let \(c_i\) (an integer where \(c_i \ge 0\)) be the number of times button \(i\) is pressed. The square reached after the moves can be expressed as: $\( \left( \sum_{i=1}^M c_i A_i \right) \bmod N \)\( Being able to move the piece to square \)G\( is equivalent to the existence of non-negative integers \)c_1, c_2, \dots, cM\( and an integer \)k\( such that: \)$ \sum{i=1}^M c_i A_i - k N = G $$

2. Achieving “Backward” Moves

At first glance, the constraint \(c_i \ge 0\) (meaning we can only move forward) seems restrictive. However, since the board is a circle of \(N\) squares, pressing button \(i\) a total of \(N - 1\) times gives: $\( (N - 1) A_i \equiv -A_i \pmod N \)\( which effectively realizes a move of going backward by \)A_i$ squares. Therefore, the number of times each button is pressed can be treated as an arbitrary integer (either positive or negative).

3. Bézout’s Identity and the Greatest Common Divisor

Let us consider whether there exist integers \(x_1, x_2, \dots, x_M, y\) satisfying: $\( \sum_{i=1}^M x_i A_i + y N = G \)\( By Bézout's identity (the foundational theorem behind the extended Euclidean algorithm), the values that can be represented as a linear combination of integers \)\sum x_i A_i + y N$ are precisely the multiples of their greatest common divisor (GCD).

That is, if we let \(g = \gcd(N, A_1, A_2, \dots, A_M)\), all reachable squares are multiples of \(g\), and conversely, any multiple of \(g\) between \(0\) and \(N - 1\) inclusive is guaranteed to be reachable. Therefore, the condition for reachability is whether \(G\) is a multiple of \(g\) (\(G \bmod g = 0\)).

Why a Naive Approach Does Not Work

One might consider using breadth-first search (BFS) or dynamic programming (DP) starting from square \(0\). However, since the number of squares \(N\) can be as large as \(10^9\), we cannot allocate an array to track visited squares, leading to Memory Limit Exceeded (MLE) or Time Limit Exceeded (TLE). By leveraging number-theoretic properties, we can determine the answer instantly without performing any search.

Algorithm

  1. Initialize a variable \(g\) tracking the greatest common divisor with \(N\).
  2. Read \(A_1, A_2, \dots, A_M\) one by one from the input, updating \(g \leftarrow \gcd(g, A_i)\).
    • If \(g\) becomes \(1\) at any point, taking the GCD with any subsequent value will keep it \(1\), so further updates may be skipped.
  3. After considering all buttons, if \(G \bmod g = 0\), output Yes; otherwise, output No.

Complexity

  • Time Complexity: \(O(M + \log N)\)
    • Computing the GCD of two integers takes logarithmic time using the Euclidean algorithm.
    • The value of \(g\) starts at \(N\), and it changes at most \(O(\log N)\) times. Moreover, since updates can be skipped once \(g = 1\), the overall time complexity is \(O(M + \log N)\), which easily passes within the time limit.
  • Space Complexity: \(O(1)\)
    • Since we do not need to store the values of \(A_i\) in an array and can update \(g\) while reading the input one by one, only \(O(1)\) additional memory is required.

Implementation Notes

  • Fast I/O: Since the input size is very large with \(M\) up to \(10^6\), it is important in C++ to use cin.tie(nullptr); ios::sync_with_stdio(false); to speed up standard I/O.

  • Overflow Considerations: Although \(N\) and \(A_i\) are at most \(10^9\) and fit within a 32-bit signed integer, using a 64-bit integer type (long long) ensures safety against overflow.

    Source Code

#include <iostream>
#include <numeric>

using namespace std;

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

    long long N, M, G;
    if (!(cin >> N >> M >> G)) return 0;

    long long g = N;
    for (int i = 0; i < M; ++i) {
        long long a;
        cin >> a;
        if (g > 1) {
            g = std::gcd(g, a);
        }
    }

    if (G % g == 0) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }

    return 0;
}

This editorial was generated by gemini-3.8-flash-high.

posted:
last update: