CodeForces--TechnoCup--2016.10.15--ProblemA--Transformation: from A to B
http://codeforces.com/contest/727/problem/A
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.
InputThe 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.
OutputIf 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.
ExamplesInput2 162OutputYES
5
2 4 8 81 162Input4 42OutputNOInput100 40021OutputYES5100 200 2001 4002 40021
看到这道题的第一眼,我想到的是用DFS搜索解决,当然搜索完全可以解决.但交上去就出问题了--空间不足--无论怎么优化空间复杂度都会爆栈
最后Lzy突然想到了规律:若某个数是奇数,必然是通过*10+1变来的,而如果某个数是偶数,一定是通过*2得来的.
这样,从b往前倒推,若能得到a,则说明可以,否则不行
#include<iostream>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
const int MAXN=;
unsigned int aa[MAXN];
long long a,b;
long long n;
bool flag;
int main()
{
//freopen("data.in","r",stdin);
while(cin>>a>>b) {
flag=;
n=;
aa[]=b;
while(b>) {
if(b==a) {
flag=;
break;
}
if(b%) {
n++;
b=b-;
if(b%)
break;
b=(b/);
aa[n]=b;
} else {
n++;
b=b/;
aa[n]=b;
}
}
if(flag) {
cout<<"YES"<<endl;
cout<<n<<endl;
for(int i=n; i>=; i--) {
cout<<aa[i]<<" ";
}
cout<<endl;
} else cout<<"NO"<<endl;
}
}
CodeForces--TechnoCup--2016.10.15--ProblemA--Transformation: from A to B的更多相关文章
- My latest news (--2016.10)
2016.10.31 22:44 一个“程序”,打代码占40%.思考占60% 2016.10.30 20:53 周末,话说今天有晚上讲座,还点名,了,悲催.之前学习的Qt有点问题,悲催.推荐个博文:h ...
- 2016 10 28考试 dp 乱搞 树状数组
2016 10 28 考试 时间 7:50 AM to 11:15 AM 下载链接: 试题 考试包 这次考试对自己的表现非常不满意!! T1看出来是dp题目,但是在考试过程中并没有推出转移方程,考虑了 ...
- 2016.8.15上午纪中初中部NOIP普及组比赛
2016.8.15上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1333 这次比赛不怎么好,因为这套题目我并不是很擅长. 可同学们 ...
- 背水一战 Windows 10 (15) - 动画: 缓动动画
[源码下载] 背水一战 Windows 10 (15) - 动画: 缓动动画 作者:webabcd 介绍背水一战 Windows 10 之 动画 缓动动画 - easing 示例演示缓动(easing ...
- Linux Kernel 3.11.4/3.10.15/3.4.65/3.0.99
Linux 今天又发布了4个更新版本,分别是: 3.11.4 2013-10-05 [tar.xz] [pgp] [patch] [view patch] [view inc] [cgit] [cha ...
- CVE-2015-1328 Ubuntu 12.04, 14.04, 14.10, 15.04 overlayfs Local Root
catalog . 引言 . Description . Effected Scope . Exploit Analysis . Principle Of Vulnerability . Patch ...
- Oracle安全漏洞2016.10报告
Oracle安全漏洞2016.10报告 http://www.cnvd.org.cn/webinfo/show/3950
- Codeforces Beta Round #10 D. LCIS
题目链接: http://www.codeforces.com/contest/10/problem/D D. LCIS time limit per test:1 secondmemory limi ...
- WTL汉化版2013.10.15
汉化内容: 2013.10.15 版本:当前可下载Trunk最新版,wtl-code-467-trunk.zip 汉化内容: 1.应用向导的部分汉化,考虑到部分词汇的表述问题,只汉化无影响部分 2.资 ...
- [Mon Feb 10 15:21:06 2014] [notice] child pid 7101 exit signal File size limit exceeded (25)
今天遇到的问题: LAMP的LOG里报如下错误. 然后IE和FIREFOX里显示连接被重置或是无法访问. 但自己建一个正常的PHP测试探针倒可以. 原来是PHP错误日志太多,无法写入LOG导致. [r ...
随机推荐
- IE6下的bug
一.IE6双倍边距bug 当页面上的元素使用float浮动时,不管是向左还是向右浮动:只要该元素带有margin像素都会使该值乘以2,例如“margin-left:10px” 在IE6中,该值就会被解 ...
- 利用python httplib模块 发送Post请求测试web服务是否正常起来!
最近在学习python,恰好老大最近让我搞个基于post请求测试web服务是否正常启用的小监控,上网查了下资料,发现强大的Python恰好能够用上,所以自己现学现卖,顺便锻炼下自己. 由于本人也刚接触 ...
- ural 1261. Tips(进制运算)
1261. Tips Time limit: 1.0 secondMemory limit: 64 MB The favorite resting place of the Ural programm ...
- GitHub使用指导
GitHub 用过Git的小伙伴想必都知道GitHub是个什么东东,我这里就简单介绍一下吧.Git是一个分布式的版本控制系统,而GitHub可以托管各种Git库,并提供一个Web界面,方便查看Git库 ...
- 判断pc浏览器和手机浏览器方法
一 //平台.设备和操作系统 var system = { win: false, mac: false, xll: f ...
- Windows API 之 GetModuleHandle
Retrieves a module handle for the specified module. The module must have been loaded by the calling ...
- hdu_2955_Robberies(01背包)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意:给一个概率p和n个银行,每个银行有一些钱和被抓的概率,问在满足被抓的概率在p以下,抢到的最 ...
- IoC容器Autofac正篇之简单实例(四)
先上一段代码. namespace ConsoleApplication3 { class Program { static void Main(string[] args) { ContainerB ...
- HttpRequest中常见的四种ContentType【转载】
本文转自:http://www.aikaiyuan.com/6324.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.T ...
- passwd总结
1.当前用户是root root用户修改密码 ,直接 passwd[不要输入当前用户密码] 如果修改其他用户密码,需要 passwd 用户名 如: passwd sc 短短的密码,如123也能通过,因 ...