Programming contests

ACM ICPC programozó csapatverseny, házi forduló, 2014. október 22.

October 22, 2014 10:05 AM – October 22, 2014 3:05 PM

A Flea on a Chessboard

An infinite chessboard is obtained by extending a finite chessboard to the right and up infinitely. Each square of the chessboard is either black or white with the side of S millimiters (0 < S < 1000). The leftmost bottom square of the chessboard is black. A flea is positioned on the chessboard at the point (xy) (given in millimeters) and makes jumps by jumping dx millimeters to the right and dy millimiters up (0 < dx, dy), that is, a flea at position (xy) after one jump lands at position (x + dxy + dy).

Given the starting position of the flea on the board, your task is to find out after how many jumps the flea will reach a white square. If the flea lands on a boundary between two squares, then it does not count as landing on the white square. Note that it is possible that the flea never reaches a white square.

Input Specification

Each test case consists of one line of input containing five nonnegative numbers separated by whitespace and giving S, x, y, dx, and dy. An input line containing five zeroes follows the last test case.

Output Specification

For each test case, print one line of output in the format shown in the sample.

Sample Input

  1. 10 2 3 3 2
  2. 100 49 73 214 38
  3. 25 0 0 5 25
  4. 407 1270 1323 1 1
  5. 18 72 6 18 6
  6. 407 1270 1170 100 114
  7. 0 0 0 0 0
download as text file

Output for Sample Input

  1. After 3 jumps the flea lands at (11, 9).
  2. After 1 jumps the flea lands at (263, 111).
  3. The flea cannot escape from black squares.
  4. After 306 jumps the flea lands at (1576, 1629).
  5. The flea cannot escape from black squares.
  6. After 0 jumps the flea lands at (1270, 1170).
download as text file
University of Debrecen; Faculty of Informatics; v. 03/01/2019