RT.
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int l,r,dp[33][33][33],ret=0,bit[33]; void get_bit(int x) { ret=0; while (x) {bit[++ret]=(x&1);x>>=1;} } int dfs(int pos,int cnt1,int cnt2,bool flag) { if (!pos) return cnt1>=cnt2; if ((!flag) && (~dp[pos][cnt1][cnt2])) return dp[pos][cnt1][cnt2]; int ans=0,up=flag?bit[pos]:1; for (int i=0;i<=up;i++) { if (i&1) ans+=dfs(pos-1,cnt1,cnt2+1,flag&&i==up); else ans+=dfs(pos-1,cnt1+1,cnt2,flag&&i==up); } if (!flag) dp[pos][cnt1][cnt2]=ans; return ans; } int work(int x) { get_bit(x);int ans=0; for (int i=1;i<=ret-1;i++) ans+=dfs(i-1,0,1,0); ans+=dfs(ret-1,0,1,1); return ans; } int main() { memset(dp,-1,sizeof(dp)); scanf("%d%d",&l,&r); printf("%d ",work(r)-work(l-1)); return 0; }