POJ3252 Round Numbers
题意&思路:
规定在二进制数下,0的个数大于等于1的个数的数字为“round number”,给你一个区间,让你找“round number”。
数位dp,太难了啊啊啊啊……
dp[i][s0][s1]表示第i位的0数量和1数量。记录下前导0的情况。
代码:
#define ll long long
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<ctype.h>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
const int N=1e6+10;
const int mod=1e7+9;
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
ll dp[40][40][40]={0},a[40];
ll dfs(ll len,ll s0,ll s1,bool lead,bool limit)
{
if(len==0) return s0>=s1;
if(!lead && !limit && dp[len][s0][s1]) return dp[len][s0][s1];
ll i,temp=0,up=limit?a[len]:1;
for(i=0;i<=up;i++)
temp+=dfs(len-1,s0+(!lead && i==0),s1+(i==1),lead && i==0,limit && i==a[len]);
if(!lead && !limit) dp[len][s0][s1]=temp;
return temp;
}
ll solve(int x)
{
ll l=0;
while(x)
{
a[++l]=x&1;
x>>=1;
}
return dfs(l,0,0,true,true);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
ll x,y;
cin>>x>>y;
cout<<solve(y)-solve(x-1)<<endl;
return 0;
}