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;
    }
  • 相关阅读:
    【C++缺省函数】 空类默认产生的6个类成员函数
    iOS 关于 UIKit 专栏应该写在最前面的话
    openssl之EVP系列之8---EVP_Digest系列函数具体解释
    二进制中1的个数
    UVA 548(二叉树重建与遍历)
    Leetcode Best Time to Buy and Sell Stock III
    javascript new Date()函数在不同浏览器上返回不同的值
    【网络流】 HDU 3468 Treasure Hunting
    树莓派玩耍笔记1 -- 开箱 &amp; 安装系统以及简单配置
    POJ 1195 Mobile phones (二维树状数组)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632151.html
Copyright © 2011-2022 走看看