采用层次遍历的方法会TLE
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n, m;
while(cin >> m >> n && n != 0)
{
int rchild = m, lchild = m, cnt = 0;
//表示以m为根结点的子树层数
int level = 1;
while(rchild <= n)
{
cnt += (int)pow(2.0, level-1);
rchild = 2*rchild + 1; //最右结点的编号
lchild = 2*lchild; //最左结点的编号
level++;
}
if(lchild > n)
cout << cnt << endl;
else
cout << cnt + n-lchild+1 << endl;
}
return 0;
}