Reachability Under Sum-Replacement Moves on a Grid
Math & Bit ManipulationAuto-graded · 26 test casesLast updated
Decide whether one positive integer point can become another by repeatedly replacing a coordinate with the coordinate sum, for values up to 10^18, by reversing moves with modulo.
Reaching a Point by Sum Moves
You are standing at a point (x, y) on a grid, where x and y are positive integers. In one move, you may do exactly one of the following:
- Replace
xwithx + y, giving(x + y, y). - Replace
ywithx + y, giving(x, x + y).
You are given a starting point (start_x, start_y) and a goal point (target_x, target_y). Return True if the goal can be reached from the start using zero or more moves, and False otherwise.
Zero moves is allowed, so if the start and goal are the same point, the answer is True.
Input
start_x,start_y: the coordinates of the starting point.target_x,target_y: the coordinates of the goal point.
Output
- A boolean:
Trueif the goal is reachable from the start,Falseotherwise.
Constraints
- All four values are integers.
1 <= start_x, start_y, target_x, target_y <= 10^18- The solution must be fast enough for values near
10^18, so step-by-step simulation of individual moves will not finish in time.
Examples
Example 1
Input: is_reachable(1, 2, 3, 5)
Output: True
Example 2
Input: is_reachable(1, 1, 2, 2)
Output: False
Hints
Hint 1
Searching forward branches into two options every step. Think about whether, from the goal, there is only one way the last move could have happened.
Hint 2
Since all coordinates stay positive, the larger coordinate of a point must have been produced by adding the smaller one to it. Undoing a move is therefore deterministic, except when both coordinates are equal.
Hint 3
Undoing moves one at a time can take about 10^18 steps. A long run of identical undo steps can be collapsed with the modulo operator, as in Euclid's algorithm. Be careful not to skip past the start coordinate: once one coordinate matches the start, check divisibility instead of taking a full modulo.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Searching forward from the start is hopeless, because each point has two successors and the number of reachable points grows exponentially. Going backward from the goal, however, there is no branching.
Suppose the point (x, y) was created by a move from a positive point (a, b):
- If the move was
(a + b, b), thenx = a + b > b = y. - If the move was
(a, a + b), theny > x.
So the larger coordinate is always the one that was just formed, and the previous point is unique: (x - y, y) if x > y, or (x, y - x) if y > x. If x == y, no valid predecessor exists, since one coordinate would have to be zero.
Undoing moves one at a time may take about 10^18 steps, for example from (1, 10^18) back to (1, 1). A run of repeated subtractions of the same value collapses into a single modulo, which is exactly Euclid's algorithm.
The modulo must not overshoot the start. The loop therefore runs only while both x > start_x and y > start_y. In that regime, the start's coordinate cannot lie inside the run being collapsed: every point in the run shares the fixed coordinate (say y), and y > start_y, so none of them is the start. Skipping them loses nothing.
When the loop stops, one coordinate equals its start counterpart (or has dropped below it, or became 0 after an equal-coordinates modulo). If x == start_x, the start can be reached only if y - start_y is a non-negative multiple of start_x, because the only remaining undo steps subtract x from y. The case y == start_y is symmetric. Anything else is unreachable.
Why it is correct
- Each positive point with unequal coordinates has exactly one predecessor, so the backward walk traces the only possible history of the goal. The start is reachable if and only if it lies on that chain.
- Collapsing a run into a modulo is safe while the fixed coordinate differs from the start's, as argued above.
- Once one coordinate matches the start, the rest of the chain keeps it fixed and subtracts it from the other coordinate, which gives the divisibility test.
- If both coordinates become equal while above the start, the modulo yields
0, which fails every final check, correctly returningFalse.
Complexity
- Time:
O(log(max(target_x, target_y))), since the modulo steps follow the Euclidean algorithm. - Space:
O(1).
Reference solution
def is_reachable(start_x: int, start_y: int, target_x: int, target_y: int) -> bool:
x, y = target_x, target_y
# Coordinates never decrease going forward, so the goal cannot be below the start.
if x < start_x or y < start_y:
return False
# Undo moves in bulk while both coordinates are still strictly above the start.
while x > start_x and y > start_y:
if x > y:
x %= y
else:
y %= x
# One coordinate now equals the start's; the other must differ by a multiple of it.
if x == start_x and y >= start_y:
return (y - start_y) % start_x == 0
if y == start_y and x >= start_x:
return (x - start_x) % start_y == 0
return False