题意:算出区间内二进制中0的个数大于等于1的个数的数字有多少个
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 50
int dp[N][N][N], bit[N];
int dfs(int pos, int num0, int num1, bool pre, bool limit)
{
if(pos==-1)
{
return num0>=num1;
}
if(!limit&&!pre&&dp[pos][num0][num1]!=-1) return dp[pos][num0][num1];
int up=limit?bit[pos]:1;
int ret=0;
for(int i=0; i<=up; i++)
{
if(i==0)
{
if(pre) ret+=dfs(pos-1, num0, num1, pre, limit&&i==up);
else ret+=dfs(pos-1, num0+1, num1, pre, limit&&i==up);
}
else
ret+=dfs(pos-1, num0, num1+1, false, limit&&i==up);
}
if(!limit&&!pre) dp[pos][num0][num1]=ret;
return ret;
}
int solve(int x)
{
int len=0;
while(x)
{
bit[len++]=x%2;
x>>=1;
}
return dfs(len-1, 0, 0, true, true);
}
int main()
{
int L, R;
memset(dp, -1, sizeof(dp));
while(cin>>L>>R)
{
printf("%d\n", solve(R)-solve(L-1));
}
return 0;
}