【玄2关】数位dp求调
查看原帖
【玄2关】数位dp求调
659165
mysterys楼主2024/7/7 09:46

rtrt。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
int x,len,T;
ll num[25], f[2][2][25][25];
ll dfs(ll pos,ll pre,bool limit,bool flag){
	if(flag)
		return 0;
	if(pos==0){
		return !flag;
	}
	if(f[flag][limit][pos][pre]!=-1)
		return f[flag][limit][pos][pre];
	int up=limit?num[pos]:9;
	ll ans=0;
	for(int i=0;i<=up;i++){
		ans+=dfs(pos-1,i,limit&&i==num[pos],i==x||flag);
	}
	f[flag][limit][pos][pre]=ans;
	return ans;
}
ll solve(ll u){
	len = 0;
	memset(f, -1, sizeof(f));
	memset(num,0,sizeof(num));
	while(u>0){
		num[++len] = u % 10;
		u/= 10;
		if(num[len]==x) return 0;
	}
	ll s=0;
	for(int i=0;i<=num[len];i++){
		s+=dfs(len-1,i,i==num[len],i==x);
	}
	return s;
}
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	cout.tie(nullptr);
	cin>>T;
	while(T--){
		cin>>n>>x;
		cout<<solve(n)<<'\n';
	}
	return 0;
}

WA了,有事踹我。

2024/7/7 09:46
加载中...