zoukankan      html  css  js  c++  java
  • 【35.56%】【727A】Transformation: from A to B

    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 long
    
    using 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
    ", maxl);
            printf("%I64d", a);
            for (int i = 2; i <= maxl; i++)
                printf(" %I64d", ans[i]);
            printf("
    ");
        }
        else
            puts("NO");
        return 0;
    }
  • 相关阅读:
    给文件夹添加命令行窗口菜单,直接在命令行下打开当前目录
    "路径的形式不合法"错误的解决方法
    常用性能计数器说明
    DataTable 和 DataView 的使用技巧
    Maven + Sonar + Jacoco扫描代码覆盖率
    JIRA中自定义Dom4j依赖,引起”SAXParserFactoryImpl cannot be SAXParserFactory“ 异常
    "给你第二次机会"——小议PushbackInputStream
    怎么处理警告:编码 GBK 的不可映射字符
    Dom4j解析XML中遇到的一些问题
    JIRA开发之——Programming Issues
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632151.html
Copyright © 2011-2022 走看看