Walaweh
Time limit1sMemory limit128 MB
Each Walaweh list W_L is built from W_{L-1} by a fixed 8-step cycle of append/prepend and optional reversal operations; convert between (length, index) and the binary string. The recursion only needs O(log N) work per level, but the reversal and leading-zero handling make the index bit-mapping non-obvious.
- Level
Hard8 of 10
- Topics
- Recursion, Bit manipulation, Math, Implementation
- Solved
- No attempts yet
Problem
A Walaweh number is an entry in a numbering sequence that is deliberately troublesome — which is exactly where the name comes from ("Walaweh!"). A Walaweh number looks like a binary number (it uses only the digits 0 and 1), but unlike an ordinary binary number its length matters, so leading zeros are kept. Here the length of a Walaweh number is simply its number of digits.
To keep the notation short, the Walaweh numbers of length are written : the ordered list of every Walaweh number with exactly digits. The smallest one, , is fixed and equals the two numbers "0" and "1", in that order. For , is generated from : make two clones and of , apply one operation (chosen as described below) to turn them into and , then concatenate the list followed by the list to obtain .
There are 8 possible operations on and :
- Append digit 0 to the end of every number in , and append digit 1 to the end of every number in .
- Prepend digit 0 to the beginning of every number in , and prepend digit 1 to the beginning of every number in .
- Append digit 1 to the end of every number in , and append digit 0 to the end of every number in .
- Prepend digit 1 to the beginning of every number in , and prepend digit 0 to the beginning of every number in .
- Reverse the order of the list , then apply operation 1.
- Reverse the order of the list , then apply operation 2.
- Reverse the order of the list , then apply operation 3.
- Reverse the order of the list , then apply operation 4.
The operations are used in a repeating cycle. is fixed; is obtained by applying operation 1 to , by applying operation 2 to , and so on, wrapping back to operation 1 after operation 8. Thus comes from operation 8 applied to , from operation 1 applied to , and so forth. Walaweh!
Here are , , , and :
To illustrate "reverse the order of the list " used by operations 5–8, here are the last 5 numbers of :
Your task is to convert a length together with a sequence number into the Walaweh number, and vice versa.
Input
The input contains one or more queries, one per line, until end of file. Each line has one of two forms:
Walaweh L N— a length with and a sequence number with .Sequence S— the binary string of a Walaweh number, whose length is less than 64 (the length is evident from itself).
Output
For every Walaweh L N line, output the -th Walaweh number of length , printed with its leading zeros so that it has exactly digits. For every Sequence S line, output the sequence number of the Walaweh number . Print one answer per input line, in the same order as the input.