A Flea on a Chessboard
Time limit1sMemory limit128 MB
The flea jumps by (dx, dy) from (x, y) on a grid of square size S; find the first jump landing strictly inside a white square, or report that none exists.
- Level
Medium6 of 10
- Topics
- Math, Number theory, Implementation
- Solved
- No attempts yet
Problem
An infinite chessboard is obtained by extending a finite chessboard infinitely to the right and up. Each square is either black or white and has side length millimeters (). The bottom-left square is black. A flea sits on the board at the point (in millimeters) and, with each jump, moves millimeters to the right and millimeters up (); that is, a flea at lands at after one jump.
Given the flea's starting position, determine how many jumps it takes for the flea to reach a white square. If the flea lands on the boundary between two squares, that does not count as landing on a white square. It is possible that the flea never reaches a white square.
Input
The input consists of several test cases. Each test case is one line containing five non-negative integers , , , , and separated by whitespace. A line containing five zeros follows the last test case and is not processed.
Output
For each test case print one line. If the flea first reaches a white square at after jumps, print After n jumps the flea lands at (a, b).. If the flea never reaches a white square, print The flea cannot escape from black squares..