Problem:
Score : 400 points

Problem Statement

In some other world, today is Christmas.

Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing:

A level-0 burger is a patty.

A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom.

For example, a level-1 burger and a level-2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.

The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?

Constraints

1 \leq N \leq 50

1 \leq X \leq ( the total number of layers in a level-N burger )

N and X are integers.

Input

Input is given from Standard Input in the following format:

N X

Output

Print the number of patties in the bottom-most X layers from the bottom of a level-N burger.

Sample Input 1

2 7

Sample Output 1

4

There are 4 patties in the bottom-most 7 layers of a level-2 burger (BBPPPBPBPPPBB).

Sample Input 2

1 1

Sample Output 2

0

The bottom-most layer of a level-1 burger is a bun.

Sample Input 3

50 4321098765432109

Sample Output 3

2160549382716056

A level-50 burger is rather thick, to the extent that the number of its layers does not fit into a 32-bit integer.

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_macros)]

// cf. [Rustで競技プログラミングの入力をスッキリ記述するマクロ - Qiita](https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8)
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        N: usize,
        X: usize,
    }
    let mut height = vec![None; N+1];
    let mut num_p = vec![None; N+1];
    num_p[0] = Some(1);
    height[0] = Some(1);
    calc(&mut height, &mut num_p, N);

    let height = height.into_iter().map(|x| x.unwrap()).collect::<Vec<_>>();
    let num_p = num_p.into_iter().map(|x| x.unwrap()).collect::<Vec<_>>();

    let mut ans = 0;
    let mut rest_x = X - 1; // eat bun at the bottom
    let mut cur_level = N - 1;
    while rest_x > 0 {
        if rest_x == height[cur_level] {
            ans += num_p[cur_level];
            rest_x -= height[cur_level];
        } else if rest_x == height[cur_level] + 1 {
            ans += 1 + num_p[cur_level];
            rest_x -= 1 + height[cur_level];
        } else if rest_x == 2 * height[cur_level] + 1 {
            ans += 1 + 2 * num_p[cur_level];
            rest_x -= 1 + 2 * height[cur_level];
        } else if rest_x == 2 * height[cur_level] + 2 {
            ans += 1 + 2 * num_p[cur_level];
            rest_x -= 2 + 2 * height[cur_level];
        } else if rest_x < height[cur_level] {
            // inside the bottom L(N-1) burger
            cur_level -= 1;
            rest_x -= 1;
        } else {
            // inside the upper L(N-1) burger
            ans += 1 + num_p[cur_level];
            rest_x -= 2 + height[cur_level];
            cur_level -= 1;
        }
    }
    println!("{}", ans);
}

fn calc(height: &mut Vec<Option<usize>>, num_p: &mut Vec<Option<usize>>, level: usize) -> (usize, usize) {
    if height[level].is_some() && num_p[level].is_some() {
        return (height[level].unwrap(), num_p[level].unwrap());
    }

    let (h, n) = calc(height, num_p, level - 1);
    let next_h = 2 * h + 3;
    let next_n = 2 * n + 1;
    height[level] = Some(next_h);
    num_p[level] = Some(next_n);
    (next_h, next_n)
}