Ouroboros Snake

Time limit1sMemory limit128 MB

Summary
Given n and k, find the n-bit number starting at position k in the de Bruijn circle built from the smallest Ouroboros number of size n.
Level

Hard8 of 10

Topics
Combinatorics, Bit manipulation, Implementation, Math
Solved
No attempts yet

Problem

Ouroboros is a mythical snake from ancient Egypt. It holds its tail in its mouth and continuously devours itself.

The Ouroboros numbers are binary numbers of 2n2^n bits that have the property of "generating" every number from 00 to 2n−12^n-1. The generation works as follows: given an Ouroboros number, place its 2n2^n bits around a circle. Then, moving the starting position one step at a time, read 2n2^n groups of nn consecutive bits along the circle. Such a circle is called the Ouroboros circle for size nn. For each nn we work only with the smallest Ouroboros number.

For example, for n=2n = 2 there are only four Ouroboros numbers: 00110011, 01100110, 11001100, and 10011001. The smallest one is 00110011. The figure below shows the Ouroboros circle for 00110011.

The function o(n;k)o(n; k) returns the value of the group that starts at position kk in the Ouroboros circle of the smallest Ouroboros number of size nn. Positions are numbered from 00; starting at position kk, read nn consecutive bits along the circle and interpret them as a binary number (the bit at the starting position kk is the most significant bit). Your program must compute this function o(n;k)o(n; k).

Input

The input consists of several test cases. Each test case is a line containing two integers nn and kk (1≤n≤151 \le n \le 15, 0≤k<2n0 \le k < 2^n). The end of the input is indicated by a line containing two zeros, which must not be processed.

Output

For each test case, output the value of o(n;k)o(n; k) on a line by itself.

Examples4

  1. Example 1

    Input
    2 0
    2 1
    2 2
    2 3
    0 0
    
    Expected output
    0
    1
    3
    2
    
  2. Example 2

    Input
    1 0
    1 1
    0 0
    
    Expected output
    0
    1
    
  3. Example 3

    Input
    3 0
    3 1
    3 2
    3 3
    3 4
    3 5
    3 6
    3 7
    0 0
    
    Expected output
    0
    1
    2
    5
    3
    7
    6
    4
    
  4. Example 4

    Input
    4 0
    4 1
    4 2
    4 3
    4 4
    4 5
    4 6
    4 7
    4 8
    4 9
    4 10
    4 11
    4 12
    4 13
    4 14
    4 15
    0 0
    
    Expected output
    0
    1
    2
    4
    9
    3
    6
    13
    10
    5
    11
    7
    15
    14
    12
    8