#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <queue> #include <cstring> #include <set> #include <stack> #include <map> #define LL long long using namespace std; int dp[50][50][50]; int bit[40]; int dfs(int pos,int num0,int num1,bool flag,bool first) { if(pos == 0) return num0 >= num1; if(!flag && dp[pos][num0][num1] != -1) return dp[pos][num0][num1]; int ans = 0; int end = flag ? bit[pos] : 1; for(int i=0;i<=end;i++) { ans += dfs(pos-1, (first && i == 0) ? 0 : num0 + (i==0), (first && i == 0) ? 0 : num1 + (i == 1), flag &&(i == end), first && (i == 0)); } if(!flag) dp[pos][num0][num1] = ans; return ans; } int solve(int n) { int len = 0; memset(bit,0,sizeof(bit)); while(n) { bit[++len] = n & 1; n >>= 1; } return dfs(len,0,0,1,1); } int main() { int l, r; while(scanf("%d%d", &l, &r)!=EOF) { memset(dp, -1, sizeof(dp)); printf("%d\n", solve(r) - solve(l-1)); } return 0; }
时间: 2024-10-13 04:21:13