【35.56%】【727A】Transformation: from A to B

来源:互联网 发布:手机淘宝买家秀设置 编辑:程序博客网 时间:2024/05/12 01:05

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations:

multiply the current number by 2 (that is, replace the number x by 2·x);
append the digit 1 to the right of current number (that is, replace the number x by 10·x + 1).
You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible.

Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b.

Input
The first line contains two positive integers a and b (1 ≤ a < b ≤ 109) — the number which Vasily has and the number he wants to have.

Output
If there is no way to get b from a, print “NO” (without quotes).

Otherwise print three lines. On the first line print “YES” (without quotes). The second line should contain single integer k — the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, …, xk, where:

x1 should be equal to a,
xk should be equal to b,
xi should be obtained from xi - 1 using any of two described operations (1 < i ≤ k).
If there are multiple answers, print any of them.

Examples
input
2 162
output
YES
5
2 4 8 81 162
input
4 42
output
NO
input
100 40021
output
YES
5
100 200 2001 4002 40021

【题解】

有两个操作*2,*10+1,不管是哪个操作。最后的总操作数都不会很大。
毕竟是log2,log10级别的。开个1W肯定不会有事了。
然后就深搜啦。因为题目强行降低难度,不要最短着。所以随便搜了。
还是加了个map判重来优化。

#include <cstdio>#include <map>#define LL long longusing namespace std;LL a, b;map <LL, int> frequent;LL ans[10000] = { 0 };int maxl = 0;bool dfs(LL key, int now){    if (frequent[key]==1)        return false;    frequent[key] = 1;    if (key > b)        return false;    if (key == b)    {        ans[now] = key;        maxl = now;        return true;    }    //*2    bool temp;    temp = dfs(key * 2, now + 1);    if (temp)    {        ans[now] = key;        return true;    }    temp = dfs(key * 10 + 1, now + 1);    if (temp)    {        ans[now] = key;        return true;    }    return false;}int main(){    //freopen("F:\\rush.txt", "r", stdin);    scanf("%I64d%I64d", &a, &b);    if (dfs(a, 1))    {        puts("YES");        ans[1] = a;        printf("%d\n", maxl);        printf("%I64d", a);        for (int i = 2; i <= maxl; i++)            printf(" %I64d", ans[i]);        printf("\n");    }    else        puts("NO");    return 0;}
0 0
原创粉丝点击