跳到主要内容
图灵 OJTURING / ONLINE JUDGE

#F0535. A flea on a chessboard

    ID: 541 传统题 1000ms 128MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>待分类整理状态难度待定难度共享题库高校与竞赛共享题英文题面题目来源题面语言

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 milimiters, 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 milimeters) and makes jumps by jumping dx milimeters to the right and dy milimiters up, 0 < dxdy, 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.

输入格式

Each test case consists of one line of input containing five non-negative numbers separated by white space and giving integers S, x, y, dx, and dy. An input line containing five zeroes follows the last test case. For test case print one line of output in the format shown in the sample.

样例 1

输入

10 2 3 3 2
100 49 73 214 38
25 0 0 5 25
407 1270 1323 1 1
18 72 6 18 6
407 1270 1170 100 114
0 0 0 0 0

输出

After 3 jumps the flea lands at (11, 9).
After 1 jumps the flea lands at (263, 111).
The flea cannot escape from black squares.
After 306 jumps the flea lands at (1576, 1629).
The flea cannot escape from black squares.
After 0 jumps the flea lands at (1270, 1170).

题目来源:http://plg1.cs.uwaterloo.ca/~acm00/040131/C.html;FPS 共享题包,题包内第 3 题。保留原作者与原赛事署名。