【35.56%】【727A】Transformation: from A to B
来源:互联网 发布:手机淘宝买家秀设置 编辑:程序博客网 时间:2024/05/12 01:05
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 longusing 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
- 【codeforces 727A】Transformation: from A to B 题解
- [codeforces Technocup 2017 727A]Transformation: from A to B
- CodeForces 727A Transformation: from A to B 暴力
- codeforces Transformation: from A to B (水题)
- codeforces A. Transformation: from A to B(水题)
- codeforces Transformation: from A to B dfs+bfs
- Technocup 2017 - Elimination Round 1: Transformation: from A to B
- Transformation: from A to B 暴力加dfs
- 【dfs/bfs+记录路径】Transformation: from A to B
- experiment : get prime's counter from a to b
- copy redis data from server A to server B
- There was a problem getting an AppDomain to run the transformation from the host. The process cannot
- A Word Transformation Map
- How to migrate DTS package from Server A to Server B
- To setup an SSH connection from A to B using key-pair
- Why is “a^=b^=a^=b;” different from “a^=b; b^=a; a^=b;”?
- from A import B 和 import A.B 的区别
- js获取元素的方法
- 关于eclipse 老版本的下载 最近找老版本 终于找到了链接
- 计时器
- 推进"五通一平":手淘技术"三大容器 五大方案"首次整体亮相 百川开放升级
- 用Delphi即时判断当前的网络的连接方式
- 【35.56%】【727A】Transformation: from A to B
- 数组操作函数
- MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建
- 重拾Python 八
- android极光推送】—从客户端到后台,一文通吃
- 字符串操作函数
- yuv
- [ 模板 ] 堆优化Dijkstra
- 牛客网-救援队