1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 7 int n,m; 8 int num[40],dp[40][80]; //表示第i个位置,0的个数减去1的个数的差值 9 10 int DFS(int pos,int res,bool ok,bool F){ 11 if(pos==-1) return res>=32; 12 if(!F&&!ok&&dp[pos][res]!=-1) return dp[pos][res]; 13 14 int maxv=F?num[pos]:1; 15 int ans=0; 16 for(int i=0;i<=maxv;i++){ 17 if(ok&&i==0) ans=ans+DFS(pos-1,res,ok,F&&i==maxv); 18 else ans=ans+DFS(pos-1,res+(i==0?1:-1),ok&&i==0,F&&i==maxv); 19 } 20 21 if(!F&&!ok) dp[pos][res]=ans; 22 return ans; 23 } 24 25 int Solve(int temp){ 26 if(!temp) return 1; 27 int cnt=0; 28 while(temp){ 29 num[cnt++]=temp%2; 30 temp/=2; 31 } 32 memset(dp,-1,sizeof(dp)); 33 return DFS(cnt-1,32,true,true); //为了避免产生负数,所以哈希一下,都加上32 34 } 35 36 int main() 37 { 38 while(~scanf("%d%d",&n,&m)) cout<<Solve(m)-Solve(n-1)<<endl; 39 return 0; 40 }