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;
    }
  • 相关阅读:
    转载:[Oracle]杀死正在执行的sql语句
    转载:记录一次MySQL两千万数据的大表优化解决过程
    转载:logback日志详解
    转载:MySQL千万级大表优化攻略
    使用dbUnit的 IDataSet 因乱序造成assert失败而采取的措施
    解锁用户scott并授权
    两表连接各种Join图示,SQL及查询结果
    一句Delete..In.. 删除语句的优化
    大数据技术之_08_Hive学习_02_DDL数据定义(创建/查询/修改/删除数据库+创建表+分区表+修改表+删除表)+DML数据操作(数据导入+数据导出+清除表中数据)
    大数据技术之_08_Hive学习_01_Hive入门+Hive安装、配置和使用+Hive数据类型
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632151.html
Copyright © 2011-2022 走看看