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
andBBPPPBPBPPPBB
(rotated 90 degrees), whereB
andP
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 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.
题意是给你一个递推字符串,
然后给你一个n,k,求对于
前k个字母里有几个p
暴力跑dfs,ok、找好关系,对于询问的k位于哪个位置(第一次体验ak的感觉~整个人都飞起来了、(*Ü*)ノ☆)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+7;
ll p[100]={1ll},b[100],l[100]={1ll},n,k;///p[i]-->s[i]中有几个p,b[i]-->s[i]中有几个b,l[i]-->s[i]中有几个字母
void init(){
for(int i=1;i<55;i++){
p[i] = 2*p[i-1]+1,b[i] = 2*b[i-1]+2;
l[i] = p[i]+b[i];
}
}
ll dfs(ll cur,ll len){
if(!cur)return 1ll;///s[0]='p'
if(len<=1)return 0ll;///'b'或者空
if(len == 1+l[cur-1])///'b'+s[i-1]
return p[cur-1];
if(len == 2+l[cur-1])///'b'+s[i-1]+'p'
return p[cur-1]+1;
if(len == l[cur] || len == l[cur]-1)///'b'+s[i-1]+'p'+s[i-1]或者'b'+s[i-1]+'p'+s[i-1]+'p'
return p[cur];
if(len<1+l[cur-1])///'b'+****
return dfs(cur-1,len-1);
return p[cur-1]+1+dfs(cur-1,len-l[cur-1]-2);///'b'+s[i-1]+'p'+***
}
int main(){
init();
cin>>n>>k;
cout<<dfs(n,k)<<endl;
return 0;
}