UVA

/*
  该题如果用模拟数组,运算量很大,复杂度极高,必须想办法改进
  可以发现,小球落入叶子结点的路线,其实和小球标号(准确说,是小球标号的奇偶性,有关),可通过找规律,极大减小程序运算量
  
  思路在白书P140-150写的很清楚,手边没书可以看这个博客
  http://blog.csdn.net/qq_24489717/article/details/49514869
  也写得很清楚,最重要的是,他在博客里用到的位运算写法,值得学习
*/


#include <iostream>
using namespace std;
int n, D, I;

int main()
{
	cin.tie(0);
	cin.sync_with_stdio(false);
	
	while (cin >> n && n != -1)
	{
		while (n--)
		{
			cin >> D >> I;
			int k = 1;
			for (int i = 0; i < D - 1; i++) //每个球严格下落(D-1)层
			if (I % 2) k <<= 1, I = (I + 1) >> 1;
			else k = k << 1 | 1, I = I >> 1; // 此处用位运算的写法,比较巧妙,值得琢磨 
			
			cout << k << endl; 
		}
	}
	return 0;
}


原文地址:https://www.cnblogs.com/mofushaohua/p/7789397.html