吐槽
今天真的是蠢哭了,A题错了2个小时,很强势。主要原因在于我认为这道题不用long long存也可以,但是事实并不是这样的,不用long long中途可能会tle,re,我用了深搜和广搜2种方法去写,但是还是写不出了,第五组错了,赛后数据一看,第五组是一个大数据,我发现了问题,改成long long后过了。留个文章提醒自己。。。
DFS版
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL S[105], flag, h;
void dfs(LL a, LL b){
if(a > b || flag) return ;
if(a == b){
flag = 1;
printf("YES\n%I64d\n", h);
for(int i = 0; i < h; i++){
if(i) printf(" ");
printf("%I64d", S[i]);
}
printf("\n");
return ;
}
S[h++] = 2*a;
dfs(2*a, b);
h--;
S[h++] = 10*a+1;
dfs(10*a+1, b);
h--;
}
int main(){
LL a, b;
while(scanf("%I64d%I64d", &a, &b) != EOF){
flag = h = 0;
S[h++] = a;
dfs(a, b);
if(!flag) printf("NO\n");
}
return 0;
}
BFS 版
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL a,b;
vector <LL> vec;
void bfs(){
priority_queue <LL, vector <LL>, greater<LL> > que;
vec.clear();
que.push(a);
vec.push_back(a);
bool flag = false;
while(!que.empty()){
LL cur = que.top(); que.pop();
if(2*cur < b) que.push(2*cur), vec.push_back(2*cur);
else if(2*cur == b){ que.push(2*cur), vec.push_back(2*cur); flag = true; break;}
if(10*cur+1 < b) que.push(10*cur+1), vec.push_back(10*cur+1);
else if(10*cur+1 == b){ que.push(10*cur+1), vec.push_back(10*cur+1); flag = true; break;}
}
if(flag){
printf("YES\n");
int another[1005], cnt = 0;
int cur = vec.size()-1;
another[cnt++] = b;
while(true){
for(int i = cur-1; i >= 0; i--){
if(vec[i]*2 == vec[cur] || vec[i]*10 + 1 == vec[cur]){
cur = i;
another[cnt++] = vec[i];
break;
}
}
if(!cur) break;
}
printf("%d\n", cnt);
int first = 1;
for(int i = cnt-1; i >= 0; i--)
if(first) printf("%I64d", another[i]), first = 0;
else printf(" %I64d", another[i]);
printf("\n");
}
else printf("NO\n");
}
int main(){
while(scanf("%I64d%I64d", &a, &b) != EOF){
bfs();
}
return 0;
}