1. 程式人生 > >Atcoder Beginner Contest 115 D Christmas 模擬,遞迴 B

Atcoder Beginner Contest 115 D Christmas 模擬,遞迴 B

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.

 

分析:

比較好玩的一個題目,給你一種關於漢堡的排序方法,然後順著吃X個問能吃多少個麵包,首先看到比較大的數先算了一下會不會爆long long,發現不會之後直接寫遞迴,寫完樣例沒過,檢查了半天又把吃一個N等級的所有的個數用一個數組進行儲存,中間換了一種形式實現,又找到了一個bug(腦殘少算了一個-1),最後AC了。

AC程式碼:

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <string>
 7 #include <time.h>
 8 #include <queue>
 9 #include <string.h>
10 #define sf scanf
11 #define pf printf
12 #define lf double
13 #define ll long long
14 #define p123 printf("123\n");
15 #define pn printf("\n");
16 #define pk printf(" ");
17 #define p(n) printf("%d",n);
18 #define pln(n) printf("%d\n",n);
19 #define s(n) scanf("%d",&n);
20 #define ss(n) scanf("%s",n);
21 #define ps(n) printf("%s",n);
22 #define sld(n) scanf("%lld",&n);
23 #define pld(n) printf("%lld",n);
24 #define slf(n) scanf("%lf",&n);
25 #define plf(n) printf("%lf",n);
26 #define sc(n) scanf("%c",&n);
27 #define pc(n) printf("%c",n);
28 #define gc getchar();
29 #define re(n,a) memset(n,a,sizeof(n));
30 #define len(a) strlen(a)
31 #define LL long long
32 #define eps 1e-6
33 using namespace std;
34 
35 ll length[100];
36 ll sum0[100];
37 ll sum = 0;
38 ll f(ll n,ll x0){
39     if(n == 1){
40         if(x0 == 1){
41             return 0;
42         }else if(x0 == 2){
43             return 1;
44         }else if(x0 == 3){
45             return 2;
46         }else if(x0 == 4){
47             return 3;
48         }else if(x0 == 5){
49             return 3;
50         }
51     }
52     if(x0 == 1){
53         return 0;
54     }else if(x0 == length[n]){
55         return sum0[n];
56     }if(x0 == ((length[n]+1)>>1)){
57         return sum0[n-1]+1;
58     }else if(x0 < ((length[n]+1)>>1)){
59         return f(n-1,x0-1);
60     }else{
61         return f(n-1,(x0-length[n-1]-2))+sum0[n-1]+1;
62     }
63 }
64 
65 
66 int main() {
67     length[0] = 1;
68     sum0[0] = 1;
69     for(ll i = 1; i <= 50; i ++){
70         length[i] = (length[i-1] *2 ) + 3;
71         sum0[i] = sum0[i-1]*2+1;
72     }
73     //pld(sum0[50]); pn
74     ll n,x;
75     sld(n) sld(x);
76     pld(f(n,x)); pn
77     return 0;
78 }
79 //10 11 12 14 15