This page is still under construction.

Parts of this page are still being built. What you see may change.

Walaweh

Time limit1sMemory limit128 MB

Summary
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 LL are written WLW_L: the ordered list of every Walaweh number with exactly LL digits. The smallest one, W1W_1, is fixed and equals the two numbers "0" and "1", in that order. For L≥2L \ge 2, WLW_L is generated from WL−1W_{L-1}: make two clones C1C_1 and C2C_2 of WL−1W_{L-1}, apply one operation (chosen as described below) to turn them into C1′C_1' and C2′C_2', then concatenate the list C1′C_1' followed by the list C2′C_2' to obtain WLW_L.

There are 8 possible operations on C1C_1 and C2C_2:

  1. Append digit 0 to the end of every number in C1C_1, and append digit 1 to the end of every number in C2C_2.
  2. Prepend digit 0 to the beginning of every number in C1C_1, and prepend digit 1 to the beginning of every number in C2C_2.
  3. Append digit 1 to the end of every number in C1C_1, and append digit 0 to the end of every number in C2C_2.
  4. Prepend digit 1 to the beginning of every number in C1C_1, and prepend digit 0 to the beginning of every number in C2C_2.
  5. Reverse the order of the list C2C_2, then apply operation 1.
  6. Reverse the order of the list C2C_2, then apply operation 2.
  7. Reverse the order of the list C2C_2, then apply operation 3.
  8. Reverse the order of the list C2C_2, then apply operation 4.

The operations are used in a repeating cycle. W1W_1 is fixed; W2W_2 is obtained by applying operation 1 to W1W_1, W3W_3 by applying operation 2 to W2W_2, and so on, wrapping back to operation 1 after operation 8. Thus W9W_9 comes from operation 8 applied to W8W_8, W10W_{10} from operation 1 applied to W9W_9, and so forth. Walaweh!

Here are W1W_1, W2W_2, W3W_3, and W4W_4:

W1W_1

Sequence NumberWalaweh Number
10
21

W2W_2

Sequence NumberWalaweh Number
100
210
301
411

W3W_3

Sequence NumberWalaweh Number
1000
2010
3001
4011
5100
6110
7101
8111

W4W_4

Sequence NumberWalaweh Number
10001
20101
30011
40111
51001
61101
71011
81111
90000
100100
110010
120110
131000
141100
151010
161110

To illustrate "reverse the order of the list C2C_2" used by operations 5–8, here are the last 5 numbers of W6W_6:

W6W_6

Sequence NumberWalaweh Number
60110011
61101111
62100111
63101011
64100011

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 LL with 1≤L<641 \le L < 64 and a sequence number NN with 1≤N≤2L1 \le N \le 2^L.
  • Sequence S — the binary string SS of a Walaweh number, whose length is less than 64 (the length is evident from SS itself).

Output

For every Walaweh L N line, output the NN-th Walaweh number of length LL, printed with its leading zeros so that it has exactly LL digits. For every Sequence S line, output the sequence number of the Walaweh number SS. Print one answer per input line, in the same order as the input.

Examples1

  1. Example 1

    Input
    Walaweh 1 1
    Walaweh 3 6
    Walaweh 4 13
    Sequence 1100
    Walaweh 5 14
    Sequence 1110
    Sequence 01010
    Walaweh 6 1
    Walaweh 6 20
    Walaweh 6 32
    
    Expected output
    0
    110
    1000
    14
    11100
    16
    31
    100010
    001110
    011100