小球下落

Description

 有一棵二叉树,最大深度为D,且所有的叶子深度都相同。所有结点从上到下从左到右编号为1,2,3,…,2eD-1。在结点1处放一个小球,它会往下落。每个结点上都有一个开关,初始全部关闭,当每次有小球落到一个开关上时,它的状态都会改变。当小球到达一个内结点时,如果该结点的开关关闭,则往上走,否则往下走,直到走到叶子结点,如下图所示。
    一些小球从结点1处依次开始下落,最后一个小球将会落到哪里呢?输入叶子深度D和小球个数I,输出第I个小球最后所在的叶子编号。假设I不超过整棵树的叶子数;D<=20。输出最多包含1000组数据。

Examples

Input

4 2
3 4
10 1
2 2
8 128
16 12345

Output

12
7
512
3
255
36358

 

正确解法:

[图论]二叉树的编号

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<cstring>
 5 #include<map>
 6 #include<set>
 7 #include<vector>
 8 #include<queue>
 9 #include<algorithm>
10 #include<cmath>
11 using namespace std;
12 typedef long long ll;
13 const int N = 30;
14 int d,t;
15 int a[100010];
16 int main()
17 {
18     while(scanf("%d %d",&d,&t)!=EOF)
19     {
20         int ans=1;
21         d--;
22         d=1<<d;
23         memset(a,0,sizeof(a));
24         //cout<<d<<endl;
25         while(t--)
26         {
27             ans=1;
28             while(ans<d)
29             {
30                 a[ans]=!a[ans];
31                 ans= a[ans]? ans*2:ans*2+1;
32             }
33         }
34         printf("%d\n",ans);
35     }
36     return 0;
37 }
View Code

相关文章:

  • 2022-12-23
  • 2021-09-02
  • 2022-12-23
  • 2021-07-22
  • 2021-09-15
  • 2021-08-05
  • 2021-09-21
  • 2021-07-07
猜你喜欢
  • 2021-07-06
  • 2022-12-23
  • 2022-12-23
  • 2022-01-19
  • 2021-08-26
  • 2021-04-23
  • 2022-01-13
相关资源
相似解决方案