#P6232. Balanced Hanger Clothing Placement

    ID: 19451 Type: Default 1000ms 256MiB

Balanced Hanger Clothing Placement

Balanced Hanger Clothing Placement

A hanger is constructed with (n) layers of connecting rods. The (i)-th layer ((i\in[0,n-1])) contains (2^i) rods. The midpoint of the rod in layer (0) is fixed to a wall. For every other layer, the midpoint of the (j)-th rod ((j\in[1,2^i])) is fixed by the (\left\lceil\frac{j}{2}\right\rceil)-th rod in the previous layer. Specifically, if (j) is odd, the fixation is at the left endpoint of the parent rod; if even, at the right endpoint. In the bottom layer, each rod has two hooks attached (one at the left endpoint and one at the right endpoint), and each hook can hold at most one clothing.

Mojca wants to hang all her clothes on this hanger. Every piece of clothing weighs exactly one unit. To avoid damaging the fragile structure, she must hang the clothes sequentially following a specific rule: after hanging each clothing, for every rod, let (w_l) be the total weight hanging below its left endpoint and (w_r) the weight hanging below its right endpoint. It is required that (w_l - w_r \in {0, 1}) (i.e. it can be either (0) or (1), but never (-1)).

It turns out that under these rules, the order in which the hooks are used is uniquely determined. Number the hooks from (1) to (2^n) from left to right. It can be shown that the order in which clothing is hung corresponds to the bit-reversal order of the numbers (0) to (2^n - 1). In other words, if we express (k-1) in binary with (n) digits, and then reverse its bits, we obtain a number (x); the (k)-th piece of clothing must be hung on hook number (x+1).

Your task is: given two integers (n) and (k), determine the hook number where the (k)-th clothing should be placed.

The key observation is that the answer is (\text{answer} = \text{bit_reverse}(k-1, n) + 1), where (\text{bit_reverse}(a, n)) reverses the binary representation of (a) in an (n)-bit field. For example, when (n=2), the order is: 1, 3, 2, 4. (Hook 1 for (k=1), hook 3 for (k=2), hook 2 for (k=3), and hook 4 for (k=4)).

inputFormat

The input consists of a single line containing two integers (n) and (k) (with (1 \le n \le 30) and (1 \le k \le 2^n)).

outputFormat

Output a single integer representing the hook number on which the (k)-th clothing should be hung.

sample

1 1
1