0
点赞
收藏
分享

微信扫一扫

AtCoder Beginner Contest 115 D - Christmas 不错的递归

mm_tang 2023-02-07 阅读 70


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

Copy


2 7


Sample Output 1

Copy


4


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

Sample Input 2

Copy


1 1


Sample Output 2

Copy


0


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

Sample Input 3

Copy


50 4321098765432109


Sample Output 3

Copy


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.

哎,比赛的时候写出来了,就错了一个测试样例,结果以为是大数出的错误,想不到是一个终止条件判断错了。

题意:

定义这么一串字母:

1.i=0时,字符串str[0]为p

2.i>=1时,字符串str[i]:B+str[i-1]+P+str[i-1]+B

 

给你n,m,问[1 , m]中有多少p。

分析:

其实就是一个递归,

str[n]=B+str[n-1]+P+str[n-1]+B,

我们先定义a[i]:表示str[i]的字符数量   

                  b[i]:表示str[i]的中P的数量

递推关系:

a[i]=2*a[i-1]+3;

b[i]=2*b[i-1]+1;

 

然后写递归函数,

AtCoder Beginner Contest 115  D - Christmas  不错的递归_java

 

我们发现就是一个递归,

如果m>p,进入第2个str[n-1](注意位置改变,因为要抛除B),ans=ans+b[n-1]+1;

如果m<p,进入第1个str[n-1],ans不变化;

如果m==p,退出;

如果n==1,逐条分析就行;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

static long ans=0;
static long [] a=new long[52];
static long [] b=new long[52];
static void dfs(long p,int n)
{
//System.out.println(p+" "+n);
//System.out.println(a[n-1]+" "+b[n-1]);
if(p==1) return ;
else if(n==1)
{
if(p==2)
{
ans+=1;
}
else if(p==3)
{
ans+=2;
}
else if(p>=4)
{
ans+=3;
}
return ;
}
else
{
if(p==a[n-1]+2)
{
ans=ans+1+b[n-1];
return ;
}
else if(p>a[n-1]+2)
{
ans=ans+b[n-1]+1;
dfs(p-a[n-1]-2,n-1);
}
else
{
dfs(p-1,n-1);
}
}
return ;
}
public static void main(String args[])throws IOException {


InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);

int n=in.nextInt();
a[1]=5;
b[1]=3;
for(int i=2;i<=50;i++)
{
a[i]=2*a[i-1]+3;
b[i]=2*b[i-1]+1;
}
long m=in.nextLong();


dfs(m,n);
out.println(ans);

out.close();
// while(in.hasNext())
// {
//
//
// }
//
//
// }
//
}
static class InputReader {
BufferedReader in;
StringTokenizer tok;

public String nextString() {
while (!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine(), " ");
} catch (IOException e) {
throw new InputMismatchException();
}
}
return tok.nextToken();
}

public int nextInt() {
return Integer.parseInt(nextString());
}

public long nextLong() {
return Long.parseLong(nextString());
}

public double nextDouble() {
return Double.parseDouble(nextString());
}

public int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}

public int[] nextIntArrayDec(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt() - 1;
}
return res;
}

public int[] nextIntArray1Index(int n) {
int[] res = new int[n + 1];
for (int i = 0; i < n; i++) {
res[i + 1] = nextInt();
}
return res;
}

public long[] nextLongArray(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}

public long[] nextLongArrayDec(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong() - 1;
}
return res;
}

public long[] nextLongArray1Index(int n) {
long[] res = new long[n + 1];
for (int i = 0; i < n; i++) {
res[i + 1] = nextLong();
}
return res;
}

public double[] nextDoubleArray(int n) {
double[] res = new double[n];
for (int i = 0; i < n; i++) {
res[i] = nextDouble();
}
return res;
}

public InputReader(InputStream inputStream) {
in = new BufferedReader(new InputStreamReader(inputStream));
tok = new StringTokenizer("");
}
}


}

 

举报

相关推荐

0 条评论