http://blog.csdn.net/angon823/article/details/51484906
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace std; typedef long long LL; LL solve(LL x){ if(x<=0)return 0; LL cur=1,ret=0; while(cur<x){ cur=cur*2+1; } cur=(cur-1)/2; ret=(cur+1)/2; LL res=x-cur-1; return ret+1+res-(ret-solve(cur-res)); } int main(){ int T; scanf("%d",&T); while(T--){ LL l,r; scanf("%I64d%I64d",&l,&r); printf("%I64d ",solve(r)-solve(l-1)); } return 0; }