1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include<iostream> #include<cstring> using namespace std; #define ll long long ll dp[50][50][50]; int a[50]; ll dfs(int pos,int num0,int num1,bool limit,bool zero) { if(pos==-1)return num0>=num1; if(!limit&&dp[pos][num0][num1]!=-1)return dp[pos][num0][num1]; int up = limit?a[pos]:1; ll temp = 0; for(int i=0;i<=up;i++) { if(zero) { if(i)temp+=dfs(pos-1,num0,num1+1,limit&&i==up,0); else temp+=dfs(pos-1,num0,num1,limit&&i==up,1); } else { if(i)temp+=dfs(pos-1,num0,num1+1,limit&&i==up,0); else temp+=dfs(pos-1,num0+1,num1,limit&&i==up,0); } } return limit?temp:dp[pos][num0][num1]=temp; } ll solve(ll x) { int pos = 0; while(x) { a[pos++]=x%2; x/=2; } return dfs(pos-1,0,0,1,1); } int main() { memset(dp,-1,sizeof(dp)); ll l,r; while(cin>>l>>r) { cout<<solve(r)-solve(l-1)<<endl; } return 0; }
|