Under construction

Still being built. Things may change

Philosopher's Walk

Interview

Time limit0.5sMemory limit512 MB

Summary
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 2k2^k meters by one rule. When kk is 1, the pathway takes a right turn of 90 degrees after every 1 meter step. When kk is greater than 1, the pathway is built from four sub-pathways of size k−1k - 1 in a fractal style. Figure F.1 shows the Philosopher's Walks for kk equal to 1, 2, and 3. The walk W2W_2 consists of four W1W_1 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 W1W_1. The same holds for every WkW_k with kk 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 2k2^k, and every Philosopher's Walk is designed by that method.

(a) W1W_1(b) W2W_2(c) W3W_3

Figure F.1. Three Philosopher's Walks with side (a) 21=22^1 = 2, (b) 22=42^2 = 4, and (c) 23=82^3 = 8.

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 (x,y)(x, y), 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 (1,1)(1, 1). The entrance of a Philosopher's Walk is always at (1,1)(1, 1) and the exit is always at (n,1)(n, 1), where nn 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 W2W_2 of Figure F.1(b) has taken 10 meter steps, his location is (3,4)(3, 4).

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 nn and mm separated by a space. nn is the side length of the square of the Philosopher's Walk and mm is the number of meter steps taken by the lost philosopher. Here n=2kn = 2^k and 1≤m≤22k1 \le m \le 2^{2k} for an integer kk with 0<k≤150 < k \le 15.

Output

Your program writes to standard output. Print a single line with two integers xx and yy separated by a space, where (x,y)(x, y) is the location of the lost philosopher.

Examples2

  1. Example 1

    Input
    4 10
    
    Expected output
    3 4
    
  2. Example 2

    Input
    8 19
    
    Expected output
    2 6