【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 ...
随机推荐
- javascript的组成
ECMAScript:(3/5/6/7) 它是JS语言的标准,规定了JS的编程语法和基础核心知识. DOM:document object model 文档对象模型,提供给JS很多的操作页面中元素的属 ...
- (转)bat批处理的注释语句
在批处理中,段注释有一种比较常用的方法: goto start = 可以是多行文本,可以是命令 = 可以包含重定向符号和其他特殊字符 = 只要不包含 :start 这一行,就都是注释 :start 另 ...
- 【Codeforces Round #445 (Div. 2) A】ACM ICPC
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 三重循环 [代码] #include <bits/stdc++.h> using namespace std; int ...
- (转)chrome浏览器收藏夹(书签)的导出与导入
导出chrome浏览器的书签到一个文件中.首先选择chrome浏览器的书签管理器菜单.然后点击“整理”,然后选择“将书签导出到html文件”. 步骤阅读 2 将导出的html文件保存,用于下次导入,这 ...
- 【b091&&z11】潜伏者
Time Limit: 1 second Memory Limit: 128 MB [问题描述] R国和S国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动. 历尽艰险后,潜伏于S国的R国间谍小 ...
- shrio 权限管理filterChainDefinitions过滤器配置(转)
shrio 权限管理filterChainDefinitions过滤器配置 /** * Shiro-1.2.2内置的FilterChain * @see ======================= ...
- openCV 和GDI画线效率对比
一. 由于项目需要,原来用GDI做的画线的功能,新的项目中考虑到垮平台的问题,打算用openCV来实现,故此做个效率对比. 二. 2点做一条线,来测试效率. 用了同样的画板大小---256*256的大 ...
- [CSS] Use Generated Content to Augment Information
When you create a pseudo element, you have access to the parent HTML attributes. They can be used in ...
- Android开发之SpannableString具体解释
在实际的应用开发过程中常常会遇到.在文本的不同部分显示一些不同的字体风格的信息如:文本的字体.大小.颜色.样式.以及超级链接等. 普通情况下,TextView中的文本都是一个样式.对于类似的情况.能够 ...
- [Angular2 Router] Get activated router url
getActivatedRoutePath(r: ActivatedRoute) { return r.url .subscribe(p => this.curtPath = p[0].path ...