为什么要特判11位数,萌新求助
查看原帖
为什么要特判11位数,萌新求助
1088691
pugong楼主2024/7/22 17:16

dfs求出来的不应该是:不大于数组a同时满足条件的数的个数吗。
如果不特判11也不去管前导0,相减以后不应该正好抵消得到正确结果,为什么还是会错 #1,#4,#5。

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[100],f[20][20][20][3][3][3],tt;

//st1:4,st2:8,st3:是否有3个相同的数,last:上一位数,sum:上一位数出现的次数
int dfs(int pos,int limit,int st1,int st2,int st3,int last,int sum){
	if(pos==0){
		if(st1&&st2) return 0;
		return st3;
	}
	if(!limit&&f[pos][last][sum][st1][st2][st3]!=-1) return f[pos][last][sum][st1][st2][st3];
	
	int ans=0,maxd=limit?a[pos]:9;
	for(int i=0;i<=maxd;i++){
		//if(pos==tt&&i==0) continue;
		ans+=dfs(pos-1,limit&&i==a[pos],st1||i==4,st2||i==8,st3||(i==last&&sum==2),i,i==last?sum+1:1);
	}
	if(!limit) f[pos][last][sum][st1][st2][st3]=ans;
	return ans;
}

int cal(int x){
	memset(f,-1,sizeof f);
	tt=0;
	while(x){
		a[++tt]=x%10;
		x=x/10;
	}
	//if(tt!=11) return 0;
	return dfs(tt,1,0,0,0,0,0);
}

signed main(){
	int l,r;
	cin>>l>>r;
	cout<<cal(r)-cal(l-1);
	return 0;
}
2024/7/22 17:16
加载中...