Ouroboros Snake
Time limit1sMemory limit128 MB
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 bits that have the property of "generating" every number from to . The generation works as follows: given an Ouroboros number, place its bits around a circle. Then, moving the starting position one step at a time, read groups of consecutive bits along the circle. Such a circle is called the Ouroboros circle for size . For each we work only with the smallest Ouroboros number.
For example, for there are only four Ouroboros numbers: , , , and . The smallest one is . The figure below shows the Ouroboros circle for .

The function returns the value of the group that starts at position in the Ouroboros circle of the smallest Ouroboros number of size . Positions are numbered from ; starting at position , read consecutive bits along the circle and interpret them as a binary number (the bit at the starting position is the most significant bit). Your program must compute this function .
Input
The input consists of several test cases. Each test case is a line containing two integers and (, ). 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 on a line by itself.