AtCoder Beginner Contest 115-D-Christmas

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 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.

 

题意是给你一个递推字符串,

S[0] = 'p'

s[i]='b'+s[i-1]+'p'+s[i-1]+'b'

然后给你一个n,k,求对于s[n]前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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值