C - 電子すごろく / Electronic Sugoroku Editorial by admin Editorial for printing
Claude Opus 5 (High)Overview
This problem asks whether we can reach square \(G\) starting from square \(0\) on a circular board of \(N\) squares by performing operations of the form “advance by \(A_i\) squares” in any order and any number of times. The answer depends on whether \(d = \gcd(N, A_1, A_2, \dots, A_M)\) divides \(G\).
Analysis
Expressing Reachable Squares as a Formula
If we press the \(i\)-th button \(c_i\) times (where \(c_i \geq 0\) is an integer), regardless of the order in which we press them, the final position is
\[\left(\sum_{i=1}^{M} c_i A_i \right) \bmod N\]
In other words, “reaching square \(G\) is possible” can be rephrased as the existence of non-negative integers \(c_1, \dots, c_M\) and an integer \(k\) such that
\[c_1 A_1 + c_2 A_2 + \dots + c_M A_M = G + kN\]
holds.
Issues with the Naive Approach
A naive idea that might immediately come to mind is “traverse all reachable squares using BFS”. However, since \(N \leq 10^9\), there would be \(10^9\) vertices and \(10^9 \times 10^6\) edges, which is far too large to fit within the time and memory limits. Therefore, instead of graph traversal, we need to determine the answer directly using mathematical properties.
Key Observation: Only Multiples of \(\gcd\) are Reachable
Let \(d = \gcd(N, A_1, \dots, A_M)\).
(1) Reachable squares must be multiples of \(d\)
Since each \(A_i\) and \(N\) are all multiples of \(d\), \(c_1A_1 + \dots + c_MA_M - kN\) is always a multiple of \(d\). Therefore, if the target square \(G\) is not a multiple of \(d\), it is impossible, which means the answer is No.
(2) All squares that are multiples of \(d\) are reachable
First, by Bézout’s identity (extended Euclidean algorithm), there exist integer coefficients \(x_i, y\) such that
\[x_1 A_1 + \dots + x_M A_M + yN = d\]
Here, \(x_i\) can be negative, but this is not an issue because we are working modulo \(N\). If \(x_i\) is negative, adding a sufficiently large multiple of \(N\) to \(x_i\) makes it non-negative without changing the value modulo \(N\) (pressing button \(A_i\) \(N\) times results in \(N \cdot A_i \equiv 0 \pmod N\), returning to the original position, so we can arbitrarily pad the number of button presses).
Thus, we can generate \(d \bmod N\), and by generating \(d\) a total of \(G/d\) times (i.e., repeating the sequence of operations above \(G/d\) times), we can reach square \(G\).
Therefore, the answer is determined solely by whether \(G\) is a multiple of \(d\).
Concrete Examples
- When \(N = 10, A = \{4, 6\}\), \(d = \gcd(10,4,6) = 2\). The reachable squares are only \(0,2,4,6,8\). Indeed, it visits only even squares like \(4 \to 8 \to 2 \to 6 \to 0\). If \(G=5\), the answer is
No; if \(G=6\), the answer isYes. - When \(N = 10, A = \{3\}\), \(d = \gcd(10,3) = 1\), so the answer is
Yesfor any \(G\) (it visits all squares: \(3,6,9,2,5,8,1,4,7,0\)). - \(G = 0\) can always be achieved by “pressing \(0\) times”. Since \(0\) is a multiple of any \(d\), this condition correctly returns
Yeseven for \(G=0\).
Algorithm
- Set \(d \leftarrow N\).
- For each \(i\), update \(d \leftarrow \gcd(d, A_i)\).
- If \(G \bmod d = 0\), output
Yes; otherwise, outputNo.
Use the Euclidean algorithm (or math.gcd in Python) to compute the \(\gcd\). If \(d = 1\) at any point, it cannot become any smaller, so you can break early from the loop (this is an effective optimization since \(M \leq 10^6\)).
Complexity
- Time Complexity: \(O(M \log \max(N, A_i))\) (\(O(\log)\) per \(\gcd\) computation, performed \(M\) times)
- Space Complexity: \(O(M)\) (to store the input; can be \(O(1)\) if processed on the fly)
Implementation Notes
Set the initial value to \(N\): It is important to initialize \(\gcd\) with \(N\) rather than \(A_1\). Because the board is circular, “advancing by \(N\) returns to the same position”, which corresponds to effectively being able to add or subtract \(N\); thus, \(N\) must also be included in the \(\gcd\).
Fast I/O: Since the input is large with \(M \leq 10^6\), in Python you should read the entire input at once using
sys.stdin.buffer.read().split()instead of repeatedly callinginput(). Also, converting toint()only as needed will make it faster.Early Break: Breaking out of the loop as soon as \(d = 1\) can significantly speed up execution in many cases (if \(d=1\), the answer is always
Yes).math.gcdis available in Python 3.5 and later. In C++, usestd::__gcdorstd::gcd(from C++17). Since the values are around \(10^9\), they fit in 32-bit integers, but usinglong longfor safety is also fine.Source Code
import sys
from math import gcd
def main():
data = sys.stdin.buffer.read().split()
n = int(data[0]); m = int(data[1]); g = int(data[2])
d = n
for i in range(3, 3 + m):
d = gcd(d, int(data[i]))
if d == 1:
break
sys.stdout.write("Yes\n" if g % d == 0 else "No\n")
main()
This editorial was generated by claude5opus-high.
posted:
last update: