【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\n", maxl);
printf("%I64d", a);
for (int i = 2; i <= maxl; i++)
printf(" %I64d", ans[i]);
printf("\n");
}
else
puts("NO");
return 0;
}
【35.56%】【727A】Transformation: from A to B的更多相关文章
- NOIP2017 列队 题解报告【56行线段树】
题目描述 Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n \times mn×m名学生,方阵的行数 ...
- UI设计师零基础入门到精通精品视频教程【155课高清完整版】
[福吧资源网分享]课程是非常完整的,也是非常零基础的,适合任何学员,有需要的可以下载看看!课程目录:第1章 Adobe Photoshop CS6课时1 Adobe Photoshop CS6入门基础 ...
- 【Android】【录音】Android录音--AudioRecord、MediaRecorder
[Android][录音]Android录音--AudioRecord.MediaRecorder Android提供了两个API用于实现录音功能:android.media.AudioRecord. ...
- 【剑指Offer学习】【全部面试题汇总】
剑指Offer学习 剑指Offer这本书已经学习完了.从中也学习到了不少的东西,如今做一个总的文件夹.供自已和大家一起參考.学如逆水行舟.不进则退.仅仅有不断地学习才干跟上时候.跟得上技术的潮流! 全 ...
- 【Xamarin开发 Android 系列 3】循序渐进的学习顺序
原文:[Xamarin开发 Android 系列 3]循序渐进的学习顺序 指定合理的学习步骤,将各个技术点进行强化.慢慢 的就从点到线 到面的飞跃,一切仅仅是时间问题,开始前,请记住,学习是最佳的投资 ...
- 【iScroll源码学习01】准备阶段 - 叶小钗
[iScroll源码学习01]准备阶段 - 叶小钗 时间 2013-12-29 18:41:00 博客园-原创精华区 原文 http://www.cnblogs.com/yexiaochai/p/3 ...
- 【剑指Offer学习】【所有面试题汇总】
剑指Offer学习 剑指Offer这本书已经学习完了,从中也学习到了不少的东西,现在做一个总的目录,供自已和大家一起参考,学如逆水行舟,不进则退.只有不断地学习才能跟上时候,跟得上技术的潮流! 所有代 ...
- 【Python】【容器 | 迭代对象 | 迭代器 | 生成器 | 生成器表达式 | 协程 | 期物 | 任务】
Python 的 asyncio 类似于 C++ 的 Boost.Asio. 所谓「异步 IO」,就是你发起一个 IO 操作,却不用等它结束,你可以继续做其他事情,当它结束时,你会得到通知. Asyn ...
- 【Python】【有趣的模块】tqdm | inspect
tqdm """ [tqdm] 显示循环的进度条,再也不用担心程序跑到哪里还要跑多久了 tqdm 可以直接包裹iterable对象 from tqdm import tq ...
随机推荐
- 微服务实战(三):深入微服务架构的进程间通信 - DockOne.io
原文:微服务实战(三):深入微服务架构的进程间通信 - DockOne.io [编者的话]这是采用微服务架构创建自己应用系列第三篇文章.第一篇介绍了微服务架构模式,和单体式模式进行了比较,并且讨论了使 ...
- Access WMI via Python from Linux
You can use Impacket (https://github.com/CoreSecurity/impacket) that has WMI implemented in Python. ...
- POJ 2479 Maximum sum POJ 2593 Max Sequence
d(A) = max{sum(a[s1]..a[t1]) + sum(a[s2]..a[t2]) | 1<=s1<=t1<s2<=t2<=n} 即求两个子序列和的和的最大 ...
- 【47.76%】【Round #380B】Spotlights
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- php课程 8-29 gd库能够画哪些东西
php课程 8-29 gd库能够画哪些东西 一.总结 一句话总结:文字,点,线,圆,弧线,矩形,各种形状都是可以的,和html5中的canva能画的东西很像,使用也很像,参数怎么记呢,参数完全不用记, ...
- POJ 2590 Steps (ZOJ 1871)
http://poj.org/problem?id=2590 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1871 题目大 ...
- Vim技巧之四大模式_插入模式
Vim技巧之四大模式_插入模式 在插入模式中及时更正错误 插入-普通模式 在插入模式模式以下直接粘贴指定寄存器的内容 插入模式中做运算 用字符编码插入很常使用字符 替换已有的文本 Vim技巧之四大模式 ...
- crx 【 集合 】
Vimium dbepggeogbaibhgnhhndojpepiihcmeb-1.64-Crx4Chrome.com.crx https://www.crx4chrome.com/down/731/ ...
- [HTTP] Understand 2xx HTTP Status Code Responses
The 2xx family of status codes are used in HTTP responses to indicate success. Beyond the generic 20 ...
- [Angular] Configurable NgModules
You probably have seen 'foorRoot()' method a lot inside Angular application. Creating a configurable ...