1、 如果满二叉树的深度为 D, 那么第 i 个小球下落经过的叶子节点
和 第 i + 2^(D-1) 个小球下落经过的叶子节点 是同一个叶子节点。
也就是周期性是 2^(D-1)
2、找规律,设 求第 n 个小球下落经过的叶子节点, 那么小球每进过一个节点
当 n % 2 == 1, 往左走, 并且 n = (n + 1) / 2;
当 n % 2 == 0, 往左走, 并且 n = n / 2;
#include <cstdio>
#include <cstring>
using namespace std;
int D, length;
int main()
{
scanf("%d%d", &D, &length);
int cycle = 1 << (D - 1);
length %= cycle;
int n = length, depth = 1;
int leaf = 1; //记录叶子节点
while(depth < D)
{
if(n % 2 == 1)
{
leaf = leaf * 2; //往左走
n = (n + 1) / 2;
}else{
leaf = leaf * 2 + 1; //往右走
n /= 2;
}
++depth;
}
printf("%d\n", leaf);
return 0;
}
/*
4 2
*/
/*
12
*/