這道題顯然可以直接模擬前 \(I\) 個小球的掉落,最終即可得解.但是,很明顯,這麼做會使時間複雜度直接爆炸成 \(O(l\times D\times I)\),已然是力不從心.
仔細觀察,可以簡單地發現:我們只需模擬第 \(I\) 個小球的運動即可,通過判斷當前節點上已經經過了的小球數的奇偶性,可以輕鬆判斷第 \(I\) 個小球的運動路線(這句話是整道題解題方法的精髓,請仔細理解後看下面的代碼).
Code:
#include <bits/stdc++.h>
using namespace std;
int l,D,I;
void rd(int&);
void wt(int);
int main() {
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
rd(l);
while (l--) {
rd(D),rd(I);
int node=1,total=(1<<D)-1;
for (;;) {
I&1?(node<<=1,I=I+1>>1):(node=node<<1|1,I>>=1);
if (node>total) {
wt(node>>1);
putchar('\n');
break;
}
}
}
return 0;
}
void rd(int& x) {
x=0;
char ch=getchar();
while (!isdigit(ch))
ch=getchar();
while (isdigit(ch)) {
x=x*10+ch-48;
ch=getchar();
}
}
void wt(int x) {
if (x>9)
wt(x/10);
putchar(x%10+48);
}