Dropping Balls

#include<bits/stdc++.h>

using namespace std;

int D, N;
int main() {
    while (scanf("%d%d", &D, &N)!=EOF) {
         int k = 1;
        for (int i = 0; i < D - 1; i++) {
            if (N % 2) {
                k = k * 2;
                N = (N + 1) / 2;
            } else {
                k = k * 2 + 1;
                N /= 2;
            }
        }
        printf("%d\n", k);
}
  }

  

时间: 2024-08-01 22:46:11

Dropping Balls的相关文章

[2016-02-08][UVA][679][Dropping Balls]

UVA - 679 Dropping Balls Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description 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 f

UVa679:Dropping Balls

Dropping Balls 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, o

UVA679 Dropping Balls【二叉树结点编号】

Dropping Balls 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, o

679 - Dropping Balls

Dropping Balls PS:因为该题排版较麻烦,这里给出OJ网址:UVa679 - Dropping Balls 有一棵二叉树,最大深度为D,且所有叶子的深度都相同.所有结点从上到下从左到右编号为1, 2, 3,-, 2D-1.在结点1处放一个小球,它会往下落.每个内结点上都有一个开关,初始全部关闭,当每次有小球落到一个开关上时,状态都会改变.当小球到达一个内结点时,如果该结点上的开关关闭,则往左走,否则往右走,直到走到叶子结点,如图6-2所示. 一些小球从结点1处依次开始下落,最后一个

Uva679 Dropping Balls

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 p

UVA - 679 Dropping Balls 规律

题目大意:给出一棵完全二叉树,每个节点上都有一个开关,刚开始开关都是开的,球经过该节点后开关就关上了(根节点除外).如果球经过该节点的兄弟节点,那么该节点的开关就又开了 现在往根节点放球,优先选择左边的节点走,如果左边的节点的开关关了,就选择右边的.球落到叶节点后就继续另一颗球,问第n颗球落到了哪个叶节点上 解题思路:其实这题蛮水的,因为是二叉树,所以可以由球的奇偶行来判断球往哪个分支走,如果n % 2 == 0就表示第n颗往右边走了,反之就是往左边走了,以此类推即可推出最后一颗球落到哪了 #i

UVa 679 Dropping Balls (例题 6-6)

传送门:https://uva.onlinejudge.org/external/6/p679.pdf 题意:在一颗结点带开关的完全二叉树上扔球,初始时开关为关闭状态,树的深度为D(1 <= D <= 20), 根结点为1(节点从1开始到2D-1),开关为关闭向左子结点走,否则往右子结点走,每到一个结点改变该结点开关状态.问第 I 颗球落在哪. 当 I 是奇数时, 它是往当前结点的左子结点走的第 (I + 1) / 2 颗球; 当 I 是偶数时, 它是往当前结点的右子结点走的第 I / 2 颗

UVa 679 - Dropping Balls

称号:有一个完整的二叉树,每个节点是一个开关,最初的全封闭,球从顶点丢弃. 每次通过开关球将将其状态反转.现在先问k球落到d当层交换机经过号. 分析:进制编码.经过模拟几次能够看出,球会让开关形成连续二进制数的表示(根是低位). 当放入第k个球时.开关状态正好是二进制的k.利用模2的余数推断走向就可以. 说明:观察规律模拟处理就可以. #include <iostream> #include <cstdlib> #include <cstring> #include &

Dropping Balls(小球下落)

紫书P148,例题6-6 Sample Input 4 2 3 4 10 1 2 2 8 128 Sample Output 12 7 512 3 255 这应该不仅仅是一棵完全二叉树,题目中说保证所有叶子节点的深度都相同,所以这是一颗满二叉树. 这里要弄清满二叉树的一些概念和性质,首先,对于一颗满二叉树来说,他每一层的节点数都达到最大,那么对于一个K层的满二叉树来说,他的节点数有(2^k)-1个 而且研究满二叉树和完全二叉树的一个好处在于他可以实现顺序存储,如图中的可以表示为 值   :1 2