POJ 3252 Round Numbers

标签(空格分隔): 数位DP


题目链接

枚举第一位不为零的数

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
ll read()
{
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int f[35][35],dit[35];
int dp(int step,int r,int lim)
{
	if(step==0)return 1;
	if(!lim&&f[step][r]!=-1)return f[step][r];
	int res=dp(step-1,r,lim&&dit[step]==0);
	if(r)if(!lim||dit[step])res+=dp(step-1,r-1,lim&&dit[step]==1);
	if(!lim)f[step][r]=res;
	return res;
}
int calc(int x)
{
	int len=0;
	while(x)dit[++len]=x%2,x/=2;
	int res=1;
	for(int i=2;i<=len;i++)res+=dp(i-1,i/2-1,i==len);
	return res;
}
int main()
{
	memset(f,-1,sizeof(f));
	int st=read(),ed=read();
	printf("%d\n",calc(ed)-calc(st-1));
	return 0;
}
posted @ 2018-05-10 11:39  LJZ_C  阅读(91)  评论(0编辑  收藏  举报