http://poj.org/problem?id=3252
求区间[l,r]中二进制0的个数不小于1的个数的数的数目。
简单数位dp。
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<iostream>
#include<set>
#include<map>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
int dp[35][70];
int a[35];
int dfs(int pos, int sta, bool lead, bool limit) {
if(pos == -1) return sta >= 32;
if(!lead && !limit && dp[pos][sta] >= 0) return dp[pos][sta];
int up = limit ? a[pos] : 1, ans = 0;
for(int i = 0; i <= up; i++) {
if(lead && i == 0) ans += dfs(pos-1, sta, true, limit && i == a[pos]);
else {
ans += dfs(pos-1, sta+(i==0?1:-1), false, limit && i == a[pos]);
}
}
if(!lead && !limit) dp[pos][sta] = ans;
return ans;
}
int solve(int x)
{
int pos = 0;
while(x) {
a[pos++] = x&1;
x >>= 1;
}
return dfs(pos-1, 32, true, true);
}
int main()
{
int l, r;
memset(dp, -1, sizeof dp);
scanf("%d%d", &l, &r);
printf("%d\n", solve(r)-solve(l-1));
}