HDU2089 不要62(数位DP)

传送门

【题目分析】

和windy数一题类似,不过限制条件改为了连续的62和4,枚举当前位填的数字的时候判断一下即可。

【代码~】

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=15;

LL l,r;
int num[MAXN],cnt;
int dp[MAXN][MAXN];

LL Read(){
	LL i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

int DP(int pos,int last,int iszero,int limit){
	if(pos==0)
	  return !iszero;
	if(!iszero&&!limit&&dp[pos][last]!=-1)
	  return dp[pos][last];
	int sx=limit?num[pos]:9,ret=0;
	for(int i=0;i<=sx;++i){
		if(!iszero){
			if((last==6&&i==2)||(i==4))
			  continue;
			ret+=DP(pos-1,i,iszero,limit&&i==num[pos]);
		}
		else{
			if(i==4)
			  continue;
			ret+=DP(pos-1,i,i==0,limit&&i==num[pos]);
		}
	}
	if(!iszero&&!limit)
	  return dp[pos][last]=ret;
	return ret;
}

int solve(int x){
	cnt=0;
	while(x){
		num[++cnt]=x%10;
		x/=10;
	}
	return DP(cnt,0,1,1);
}

int main(){
	l=Read(),r=Read();
	while(l&&r){
		memset(dp,-1,sizeof(dp));
		cout<<solve(r)-solve(l-1)<<'\n';
		l=Read(),r=Read();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/g21glf/article/details/83832397