C - 電子すごろく / Electronic Sugoroku Editorial by admin Editorial for printing
Claude Opus 5 (High)Overview
In this problem, we are given a circular board with \(N\) squares and need to determine whether we can reach square \(G\) starting from square \(0\) by repeatedly performing the operation “advance by \(+A_i\)” any number of times and in any order. The answer is determined by whether \(G\) is a multiple of \(\gcd(N, A_1, \dots, A_M)\).
Analysis
Expressing reachable squares with a formula
Suppose we press button \(i\) a total of \(c_i\) times (\(c_i \ge 0\)). The order in which the buttons are pressed does not matter, and the final position is given by:
\[\left(\sum_{i=1}^{M} c_i A_i \right) \bmod N\]
That is, “reaching square \(G\)” is equivalent to being able to write
\[c_1 A_1 + c_2 A_2 + \dots + c_M A_M = G + kN\]
for some non-negative integers \(c_1,\dots,c_M\) and an integer \(k\).
Why a naive approach fails
One might first think of using Breadth-First Search (BFS) from square \(0\) to explore all reachable squares. However, since \(N \le 10^9\), there are up to \(10^9\) vertices and up to \(10^9 \times 10^6\) edges, which will completely exceed the time and memory limits (TLE / MLE). Therefore, we need to mathematically characterize the set of reachable squares.
Characterization using the Greatest Common Divisor
Let \(g = \gcd(N, A_1, A_2, \dots, A_M)\).
(1) Any reachable square must be a multiple of \(g\)
Since each \(A_i\) is a multiple of \(g\), the sum \(c_1A_1 + \dots + c_MA_M\) is also a multiple of \(g\). Moreover, because \(N\) is a multiple of \(g\), for \(G + kN\) to be a multiple of \(g\), \(G\) must also be a multiple of \(g\). Thus, if \(G \bmod g \ne 0\), square \(G\) cannot be reached.
(2) All squares that are multiples of \(g\) can be reached
By Bézout’s identity (a consequence of the extended Euclidean algorithm), there exist integer coefficients \(x_i\) and \(y\) such that:
\[x_1A_1 + \dots + x_MA_M + yN = g\]
Here, some \(x_i\) could be negative, but this is not an issue when working modulo \(N\). Pressing button \(i\) exactly \(N\) times advances us by \(A_i \times N \equiv 0 \pmod N\), returning us to the original position. Hence, the number of button presses can be freely adjusted modulo \(N\). In other words, replacing each \(x_i\) with \(x_i \bmod N\) (a non-negative value) does not change the destination modulo \(N\). Therefore, square \(g\) is reachable. By repeating this process \(t\) times, we can reach square \((tg) \bmod N\). Since \(g \mid N\), letting \(t = 0, 1, \dots, N/g - 1\) covers all squares that are multiples of \(g\).
From the above, the condition is \(G \bmod g = 0\).
Concrete Examples
For \(N = 12\), \(A = \{4, 10\}\), we have \(g = \gcd(12, 4, 10) = 2\). Thus, all even-numbered squares can be reached. Indeed, \(4+10 = 14 \equiv 2\), confirming that square \(2\) can be reached. If \(G = 3\), it is odd, so the answer is No.
For \(N = 6\), \(A = \{2, 3\}\), we have \(g = \gcd(6, 2, 3) = 1\), meaning all squares are reachable, so the answer is always Yes.
Algorithm
- Initialize \(g \leftarrow N\).
- For each \(A_i\), update \(g \leftarrow \gcd(g, A_i)\).
- If \(G \bmod g = 0\), output
Yes; otherwise, outputNo.
The \(\gcd\) can be computed in \(O(\log)\) time using the Euclidean algorithm (std::gcd in C++, math.gcd in Python).
Note that when \(G = 0\), we do not need to press any button; since \(0 \bmod g = 0\), it automatically outputs Yes (since the initial value is \(g = N \ge 2\), \(g\) will never be \(0\), so there is no risk of division by zero).
Complexity
- Time complexity: \(O(M \log \max(N, A_i))\)
- Space complexity: \(O(1)\) (by processing \(A_i\) on the fly without storing them in an array)
Implementation Details
Initializing \(g\) to \(N\) is the key point. Taking the \(\gcd\) of only the elements of \(A_i\) would be incorrect (for example, when \(N = 6\) and \(A = \{4\}\), \(\gcd(A) = 4\), but we can reach all even squares via \(4, 8 \equiv 2, 12 \equiv 0, \dots\), so the correct value is \(g = \gcd(6, 4) = 2\)).
Since \(N, A_i \le 10^9\), they individually fit in a standard 32-bit
int, but it is safer to use a 64-bit integer (long long).Because the input size is large with \(M \le 10^6\), it is recommended to use fast I/O:
scanforstd::ios::sync_with_stdio(false)in C++, or bulk-reading viasys.stdinin Python. There is no need to store elements in an array; updating \(g\) as you read the values achieves \(O(1)\) memory.If \(g = 1\) at any point, it will not change further, so an early exit optimization can be made by skipping the \(\gcd\) computation for the remaining inputs (though reading the input is still necessary).
Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
long long N, M, G;
if(!(cin >> N >> M >> G)) return 0;
long long g = N;
for(long long i = 0; i < M; i++){
long long a; cin >> a;
g = std::gcd(g, a);
}
cout << (G % g == 0 ? "Yes" : "No") << "\n";
return 0;
}
This editorial was generated by claude5opus-high.
posted:
last update: