AcCoder Contest-115 D - Christmas

D - Christmas


Time limit : 2sec / Memory limit : 1024MB

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≥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≤N≤50
  • 1≤X≤( 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.

  

  题意: 一个L级别的汉堡由 一个b ,一个L-1级别的汉堡, 一个p, 一个L-1级别的汉堡, 一个b 共五层组成。

  0 级别的汉堡单独由 p 组成。

  例如 1级别的汉堡由 b, 1-1, p, 1-1, b 也就是bpppb

  2级别的汉堡由 b, 2-1, p, 2-1, b 组成 也就是b(bpppb)p(bpppb)b

  问: 现在有一个N级别的汉堡,从最底层(最顶层)开始吃 吃掉x层后,一共吃掉了多少层p

  

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 long long arr[55];
 6 long long stu[55];
 7 long long ans = 0;
 8 int n;
 9 long long x;
10 void f(int d){
11     if(x==arr[d]){//如果吃x层会整好吃完级别为 d 的汉堡
12         ans += stu[d];
13         return ;
14     }else if(x){
15         x--;// 吃掉最底层的 b
16         if(x>arr[d-1]){// x 落在了上面的 L-1 级的汉堡
17             x-=arr[d-1];//吃掉下面的 L-1 级的汉堡
18             ans += stu[d-1];// L-1级别的汉堡有stu[l-1]层 p
19             x--;// 吃掉中间的 p
20             ans++;
21             if(x)f(d-1);// 进入上面的 L-1 级的汉堡
22             //这里可以不进行if判断直接进入 因为进入后还会判断x非零
23         } else f(d-1);// 落点在下面的 L-1 级汉堡
24     }
25 }
26 int main(){
27     arr[0] = 1;
28     arr[1] = 5;
29     arr[2] = 13;
30     stu[0] = 1;
31     stu[1] = 3;
32     stu[2] = 7;
33     for(int i=3;i<51;i++){
34         //arr[i] = 1+arr[i-1]+1+arr[i-1]+1;
35         arr[i] = arr[i-1]*2+3;// 级别为 i 的汉堡的层数 b+p
36         stu[i] = stu[i-1]*2+1;// 级别为 i 的汉堡的 p 的数量
37     }
38     scanf("%d%lld",&n,&x);
39     f(n);
40     printf("%lld\n",ans);
41     return 0;
42 }
View Code

 

posted @ 2018-12-08 21:38  kongbb  阅读(513)  评论(0编辑  收藏  举报