题目地址:http://ac.jobdu.com/problem.php?cid=1040&pid=32
C语言源码:
#include<stdio.h>
#include<math.h>
int depth(int m)
{
return (int)(log(m)/log(2));
}
int main()
{
int m,n,li,bi;
scanf("%d %d",&m,&n);
while(m!=0||n!=0)
{
li=m;
while(li*2<=n)
li=li*2;
bi=m;
while(bi*2+1<=n)
bi=bi*2+1;
if(depth(li)<depth(n))
printf("%d\n",(int)pow(2,depth(li)-depth(m)+1)-1);
else
if(depth(bi)==depth(n))
printf("%d\n",(int)pow(2,depth(bi)-depth(m)+1)-1);
else
printf("%d\n",(int)pow(2,depth(bi)-depth(m)+1)-1+n-li+1);
scanf("%d %d",&m,&n);
}
}