poj3532 Round Numbers

 
题意:给出l、r,求区间[l,r]内二进制中0的个数大于等于1的个数的数字有多少个.
简单的数位dp。
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=100+10;
int l,r,ans,len;
int a[maxn],dp[maxn][2*maxn][2];

void get_s(int x) { len=0; while(x) { a[++len]=x&1; x>>=1; } }

int f(int pos,int cha,int now) {//2:lim 1:fir
	if(!pos) return cha>=0;
	if(!(now&2)&&dp[pos][cha+100][now&1]!=-1) return dp[pos][cha+100][now&1];
	int p= (now&2)&&(!a[pos])? 0:1,rs=0;
	for(int i=0;i<=p;++i) rs+=f(pos-1,i||(!(now&1))? cha+(!i)-i:0,now&((!i)|((i==p)<<1)));
	if(!(now&2)) dp[pos][cha+100][now&1]=rs;
	return rs;
}

int main() {
	scanf("%d%d",&l,&r);l--;
	memset(dp,-1,sizeof(dp));
	get_s(r);ans+=f(len,0,3);
	get_s(l);ans-=f(len,0,3);
	printf("%d",ans);
	return 0;
}

  

posted @ 2017-10-05 11:17  shixinyi  阅读(146)  评论(0编辑  收藏  举报