Philosopher's Walk
InterviewTime limit0.5sMemory limit512 MB
Given the step index m along a Hilbert curve of side n = 2^k, find the cell coordinates (x, y) of that step.
- Level
Medium6 of 10
- Topics
- Divide and conquer, Recursion, Math, Implementation
- Solved
- No attempts yet
Problem

In Programming Land there are several pathways called Philosopher's Walks where philosophers take a rest. A Philosopher's Walk is a pathway inside a square region full of woods. The woods help philosophers think, but they are planted so densely that the region turns into a maze and philosophers lose their way.
Every Philosopher's Walk has a similar structure. It is laid out inside a square of side meters by one rule. When is 1, the pathway takes a right turn of 90 degrees after every 1 meter step. When is greater than 1, the pathway is built from four sub-pathways of size in a fractal style. Figure F.1 shows the Philosopher's Walks for equal to 1, 2, and 3. The walk consists of four structures: the lower left one is rotated 90 degrees clockwise, the lower right one is rotated 90 degrees counterclockwise, and the upper two keep the orientation of . The same holds for every with greater than 1. The mathematical philosopher David Hilbert (1862-1943) devised this rule, and the resulting pathway is called a Hilbert curve after him. He once described a space filling method that uses this kind of curve to fill up a square of side , and every Philosopher's Walk is designed by that method.
Figure F.1. Three Philosopher's Walks with side (a) , (b) , and (c) .
Tae-Cheon is in charge of rescuing philosophers lost in Philosopher's Walks with a hot air balloon. Every lost philosopher can report to Tae-Cheon the number of meter steps he has taken, and Tae-Cheon knows the side length of the square of the Philosopher's Walk. He has to identify the location of the lost philosopher as coordinates , assuming that the Philosopher's Walk is placed in the first quadrant of a Cartesian plane with one meter as the unit length. The lower left corner block has coordinates . The entrance of a Philosopher's Walk is always at and the exit is always at , where is the side length. The philosopher has already walked one meter step when he is at the entrance, and he always goes forward to the exit without back steps.
For example, if a philosopher lost in the walk of Figure F.1(b) has taken 10 meter steps, his location is .
Write a program that reports the location of the lost philosopher, given the number of meter steps he has taken and the side length of the square of the Philosopher's Walk.
Input
Your program reads from standard input. The input consists of a single line containing two positive integers and separated by a space. is the side length of the square of the Philosopher's Walk and is the number of meter steps taken by the lost philosopher. Here and for an integer with .
Output
Your program writes to standard output. Print a single line with two integers and separated by a space, where is the location of the lost philosopher.


