题目链接:https://vjudge.net/problem/UVA-679
题解:这题读完以后很明显的思路是用一个数组来模拟小球掉落的过程,但是通过极限分析的话,时间根本不能满足要求。因此需要另外寻求规律来解题。在对题目进行深入的理解之后,只需根据小球的奇偶性就可以确定它最终会落到哪棵子树中了【参考了书中代码】,这也体现在对题目深入建模的重要性。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int solve(int D, int I) {
int pos = 1;
for (int i = 0; i < D - 1; i++) {
if (I % 2) { pos = pos * 2; I = (I + 1) / 2; }
else { pos = pos * 2 + 1; I /= 2; }
}
return pos;
}
int main() {
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int kase = 0;
cin >> kase;
while (kase--) {
int D = 0, I = 0;
cin >> D >> I;
cout << solve(D, I) << endl;
}
return 0;
}