Walk Like an Egyptian
Time limit1sMemory limit128 MB
Stones fill an N by N grid along a spiraling quarter-circle path; find the number placed in the top-right cell. Multiple N values per input.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, Math, Array
- Solved
- No attempts yet
Problem

Walk Like an Egyptian is an old multiplayer board game once played by children of the Sahara nomad tribes. Children would collect stones and number each one. A game with players needs stones, and each player picks of them. The stones are laid out on an grid in a peculiar order, as shown in Figure (a) (for ). The player whose stone lands in the top-right corner loses the round. A new round is then played with players, and rounds in total decide the winner.
The layout follows an old rule that the Pharaohs' workers used to cross a dark room inside a pyramid without losing anyone (see Figure (b)):
- The first worker stands in the lower-left corner of the room.
- The next three workers stand around the first one, forming a quarter circle in the anti-clockwise direction.
- The next five workers stand around those three, this time forming a quarter circle in the clockwise direction.
- They keep repeating the previous two steps until the room is full. Whenever they reach the left wall or the bottom wall, they begin a larger quarter circle and alternate the direction between clockwise and anti-clockwise.
The stones are numbered in the exact order in which the workers take their places. Given , determine the number written on the stone that ends up in the top-right corner of the grid.
Input
The input contains one or more test cases. Each test case is given on its own line as a single integer (), the number of players.
The input ends with a line containing a single zero, which is not part of the test data.
Output
For each test case, print one line in the form N => result, where is the number of players for that test case and result is the number on the stone at the top-right corner of the grid.