涉及二叉树的问题, 可以尝试用二进制简化问题
类似于哈夫曼编码问题, 由第一个球全部走左子树可以猜测到
走左子树代表0, 右子树代表1, 则第i个球所走的路径就是i-1的二进制码的逆序
由此可以直接模拟第i个球, 而不用模拟前面所有的球的运动
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <cassert>
#include <iomanip>
using namespace std;
const int MAXN = (2<<20)+1;
/*
uva 679
涉及二叉树的问题, 可以尝试用二进制简化问题
类似于哈夫曼编码问题, 由第一个球全部走左子树可以猜测到
走左子树代表0, 右子树代表1, 则第i个球所走的路径就是i-1的二进制码的逆序
由此可以直接模拟第i个球, 而不用模拟前面所有的球的运动
*/
int main(){
int t,D,I;
while( (cin>>t) && t!=-1 ){
while( t-- ){
cin >> D >> I;
I = I-1; // I-1的逆序
int node = 1; // 当前所在的结点
while( --D ){
if( I&1 ){
node = (node<<1) + 1;
}else{
node <<= 1;
}
I >>= 1;
}
cout << node << endl;
}
}
return 0;
}