(数位dp)HDU2098 不要62

HDU2098 不要62

思路:

对数位dp完全不懂一知半解,参考了大佬的代码才能勉强理解。
dfs+记忆化。
len记录数位,p6记录此位是不是6,limit记录是否达到数位上界。

代码:

#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
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;
int dp[20][2]={0},a[20];
int dfs(int len,bool p6,bool limit)
{
	if(len==0)	return 1;
	if(!limit && dp[len][p6])	return dp[len][p6];
	int i,tmp=0,up=limit?a[len]:9;
	for(i=0;i<=up;i++)
	{
		if(p6 && i==2)	continue;
		if(i==4)	continue;
		tmp+=dfs(len-1,i==6,limit && i==a[len]);
	}
	if(!limit)	dp[len][p6]=tmp;
	return tmp;
}
int solve(int x)
{
	int l=0;
	while(x)
	{
		a[++l]=x%10;
		x/=10;
	}
	return dfs(l,false,true);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n,m;
	while(cin>>n>>m && n+m)
		cout<<solve(m)-solve(n-1)<<endl;
	return 0;
}

发布了78 篇原创文章 · 获赞 0 · 访问量 1408

猜你喜欢

转载自blog.csdn.net/Z7784562/article/details/103900386
今日推荐