A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each time the ball being dropped first visits a non-terminal node. It then keeps moving down, either follows the path of the left subtree, or follows the path of the right subtree, until it stops at one of the leaf nodes of FBT. To determine a ball’s moving direction a flag is set up in every non-terminal node with two values, either false or true. Initially, all of the flags are false. When visiting a non-terminal node if the flag’s current value at this node is false, then the ball will first switch this flag’s value, i.e., from the false to the true, and then follow the left subtree of this node to keep moving down. Otherwise, it will also switch this flag’s value, i.e., from the true to the false, but will follow the right subtree of this node to keep moving down. Furthermore, all nodes of FBT are sequentially numbered, starting at 1 with nodes on depth 1, and then those on depth 2, and so on. Nodes on any depth are numbered from left to right.

  For example, Fig. 1 represents a fully binary tree of maximum depth 4 with the node numbers 1, 2, 3, ..., 15. Since all of the flags are initially set to be false, the first ball being dropped will switch flag’s values at node 1, node 2, and node 4 before it finally stops at position 8. The second ball being dropped will switch flag’s values at node 1, node 3, and node 6, and stop at position 12. Obviously, the third ball being dropped will switch flag’s values at node 1, node 2, and node 5 before it stops at

position 10.

  Fig. 1: An example of FBT with the maximum depth 4 and sequential node numbers.

  Now consider a number of test cases where two values will be given for each test. The first value is D, the maximum depth of FBT, and the second one is I, the I-th ball being dropped. You may assume the value of I will not exceed the total number of leaf nodes for the given FBT. Please write a program to determine the stop position P for each test case.

  For each test cases the range of two parameters D and I is as below:

2≤ D ≤20, and 1≤ I ≤524288.

Input

Contains l +2 lines.

Line 1 l the number of test cases

Line 2 D1 I1 test case #1, two decimal numbers that are separated by one blank

...

Line k +1 Dk Ik test case #k Line l +1 Dl Il test case #l

Line l +2 -1 a constant ‘-1’ representing the end of the input file

Output

Contains l lines.

Line 1 the stop position P for the test case #1

...

Line k the stop position P for the test case #k

...

Line l the stop position P for the test case #l

Sample Input

5
4 2
3 4
10 1
2 2
8 128
-1

Sample Output

12
7
512
3
255

HINT

这个题目最重要的是他的结论:

  1. 给定一棵包含2 d 个结点(其中d 为树的高度)的完全二叉树,如果把结点从上到下从左到右编号为1,2,3……,则结点k 的左右子结点编号分别为2k 和2k +1。
  2. 如果使用题目中给出的编号I ,则当I 是奇数时,它是往左走的第(I +1)/2个小球;当I 是偶数时,它是往右走的第I /2个小球。

Accepted

#include <bits/stdc++.h>
using namespace std;
int main()
{
int s, m, n;
while (cin >> s&&s!=-1) {
while (s--){
cin >> m >> n;
unsigned long long int k = 1;
for (int i = 0;i < m - 1;i++)
if (n % 2) { k = k * 2;n = (n + 1) / 2; }
else { k = k * 2 + 1;n /= 2; }
cout<<k<<endl;
}
}
}

Dropping Balls UVA - 679的更多相关文章

  1. 小球下落(Dropping Balls, Uva 679)

    题目描述 有一棵二叉树,最大深度为D,且所有的叶子深度都相同.所有结点从上到下从左到右编号为1,2,3,-,2eD-1.在结点1处放一个小球,它会往下落.每个结点上都有一个开关,初始全部关闭,当每次有 ...

  2. Dropping Balls UVA - 679(二叉树的遍历)

    题目链接:https://vjudge.net/problem/UVA-679 题目大意:t组样例,每组包括D M   层数是D   问第M个小球落在哪个叶子节点?    每个节点有开关  刚开始全都 ...

  3. uva-679 Dropping Balls UVA - 679

    题目大意 总共有一个深度为D的满二叉树,I个小球,每个节点具有开关,小球经过节点后节点开关会有变化,初始都关闭,若关闭往左右否则往右走 只需要循环一下每层的情况即可 代码 #include <b ...

  4. UVA.679 Dropping Balls (二叉树 思维题)

    UVA.679 Dropping Balls (二叉树 思维题) 题意分析 给出深度为D的完全二叉树,按照以下规则,求第I个小球下落在那个叶子节点. 1. 默认所有节点的开关均处于关闭状态. 2. 若 ...

  5. UVA 679 Dropping Balls 由小见大,分析思考 二叉树放小球,开关翻转,小球最终落下叶子编号。

    A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each ...

  6. Dropping Balls (二叉树+思维)

      Dropping Balls  A number of K balls are dropped one by one from the root of a fully binary tree st ...

  7. UVa 679 【思维题】

    UVA 679 紫书P148例题. 题目大意:小球从一棵所有叶子深度相同的二叉树的顶点开始向下落,树开始所有节点都为0.若小球落到节点为0的则往左落,否则向右落.并且小球会改变它经过的节点,0变1,1 ...

  8. UVa 679 Dropping Balls (例题 6-6)

    传送门:https://uva.onlinejudge.org/external/6/p679.pdf 题意:在一颗结点带开关的完全二叉树上扔球,初始时开关为关闭状态,树的深度为D(1 <= D ...

  9. Uva 679 Dropping Balls

    这道题如果模拟着来写,思路很简单 #include <iostream> #include <cstring> using namespace std; int T,D,I,c ...

随机推荐

  1. 将日志发送到log日志文件中

    log.debug("toUser:"+toUser+",subject:"+subject+",content:"+content);

  2. Deep Unfolding Network for Image Super-Resolution 论文解读

    Introduction 超分是一个在 low level CV 领域中经典的病态问题,比如增强图像视觉质量.改善其他 high level 视觉任务的表现.Zhang Kai 老师这篇文章在我看到的 ...

  3. Java程序员必备后台前端框架--Layui【从入门到实战】(一)

    layui入门使用及图标的使用 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] [编程工具:IDEA] 下载Layui与文件分析 下载直接去官网下载即可 文件分析 下载完成后,解压会 ...

  4. 1.4 数据库和常用SQL语句(正文)——MySQL数据库命令和SQL语句

    前面我们已经讲述了,登录时,我们使用mysql –u root –p命令进行,此时如果设置了密码,则需要输入密码. 输入密码后即进入MySQL的操作界面,此时,命令行窗体左侧显示"mysql ...

  5. SQL SERVER跨数据库服务,联表进行查询

    SELECT * FROM 数据库A..表A a, 数据库B..表B b WHERE a.field=b.field

  6. Java 重入锁和读写锁

    本文部分摘自<Java 并发编程的艺术> 重入锁 重入锁 ReentrantLock,顾名思义,就是支持重进入的锁,它表示该锁能够支持一个线程对资源的重复加锁.除此之外,该锁还支持获取锁时 ...

  7. 基于ABP框架的SignalR,使用Winform程序进行功能测试

    在ABP框架里面,默认会带入SignalR消息处理技术,它同时也是ABP框架里面实时消息处理.事件/通知处理的一个实现方式,SignalR消息处理本身就是一个实时很好的处理方案,我在之前在我的Winf ...

  8. 社区 正式发布了 CoreWCF 0.1.0 GA

    CoreWCF 项目在2021.2.19 正式发布了0.1.0 GA版本:https://github.com/CoreWCF/CoreWCF/releases/tag/v0.1.0 ,这个版本号虽然 ...

  9. 利用jmeter对WebRTC应用进行压力测试(java)

    利用jmeter对WebRTC应用进行压力测试(java) 说明:WebRTC是一款开源的多人即时视频API,与一般的http请求不同,webrtc应用实际压力主要是码流 最近负责了一个WebRTC的 ...

  10. 基础篇:JAVA引用类型和ThreadLocal

    前言 平时并发编程,除了维护修改共享变量的场景,有时我们也需要为每一个线程设置一个私有的变量,进行线程隔离,java提供的ThreadLocal可以帮助我们实现,而讲到ThreadLocal则不得不讲 ...