非打表 50分 TLE #2#7#8#9#10 悬6个关
查看原帖
非打表 50分 TLE #2#7#8#9#10 悬6个关
549846
dpACerFXing楼主2024/5/9 23:25
# include<iostream>
# include<algorithm>
# include<cmath>
# include<map>
# define int long long
# define endl "\n"
using namespace std;
int a, b, ans=0;
int magic[10000001], cnt=0, queue[10000001], head=1, tail=0; 
map<int, int> m;
void push(int a) {
	queue[++tail]=a;
}
int front() {
	return queue[head];
}
void pop() {
	head++;
}
void dfs(int n, int target) { // target[n]+-magic[n] -> target[n+1] 
	if(target>b) return ; 
	int temp=target/pow(10, n-1);
	if(n<=cnt) {
		if(temp+magic[n]<=9) {
			int dxj=target+(temp+magic[n])*pow(10, n);
			dfs(n+1, dxj);
		}
		if(target/pow(10, n-1)-magic[n]>=0) {
			int dxj=target+(temp-magic[n])*pow(10, n);
			dfs(n+1, dxj);
		}
	} else if(temp){
		while(target<=b) {
			if(a<=target && target<=b)
				if(m.count(target)==0)
					m.insert(make_pair(target, 1));
			push(target);
			n++;
			target+=pow(10, n-1)*temp;
		}
	}
}
signed main() {
	cin >> a >> b;
	push(7);
	if(a<=7 && 7<=b) m.insert(make_pair(7, 1));
	while(head<=tail) {
		int u=front(); pop();
		int hlysb=u;
		cnt=0;
		while(u>0) {
			magic[++cnt]=u%10;
			u/=10;
		}
		for(int i=0; i<=9; i++) {
			dfs(1, i);
		}
	}
	cout << m.size() << endl;
	return 0;
}
2024/5/9 23:25
加载中...