题目链接:http://poj.org/problem?id=3252
数位DP专题:http://blog.csdn.net/chy20142109/article/details/50930004
题意:求区间内round number的个数,所谓round number就是用二进制表示时,0的个数不小于1的个数。
思路:记录从最高层dfs到当前层的0的个数,1的个数,以及是否用1开过头了,还是前面选的全是0。
如果前面全是0,可以继续选0或者在当前层以1开头。或者就当前层选1/0。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;
#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod %100000007
int dp[35][35][35];
int bit[35];
int st,ed;
int dfs(int k , int zero , int one , bool f , bool flag)
{
if ( k == 0 ) return ( f && zero>=one );
if ( !flag && dp[k][zero][one] != -1 ) return dp[k][zero][one];
int ed = flag?bit[k]:1;
int ans = 0;
rep(i,0,ed)
{
if( f )
ans+=(i==0)?dfs( k - 1 , zero + 1 , one , f , flag && i == ed ):dfs( k - 1 , zero , one + 1 , f , flag && i == ed );
else ans+=(i==0)?dfs( k - 1 , 0 , 0 , 0 , 0 ):dfs( k - 1 , 0 , 1 , 1 , flag && i == ed );
}
if ( !flag ) dp[k][zero][one] = ans;
return ans;
}
int cal(int x)
{
int len = 0;
while(x)
{
bit[++len] = x % 2;
x /= 2;
}
return dfs(len,0,0,0,1);
}
int main()
{
Clean(dp,-1);
cin>>st>>ed;
cout<<cal(ed)-cal(st-1);
return 0;
}