Two Buttons
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.

Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?

Input

The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .

Output

Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n.

Sample test(s)
input
4 6
output
2
input
10 1
output
9
Note

In the first example you need to push the blue button once, and then push the red button once.

In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.

刚开始T了,后来加入了剪枝,如果当前这个时间量搜过了那么就不再搜。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cctype>
#include <queue>
using namespace std; const int MAX = ;
bool VIS[MAX + ];
struct Node
{
int n;
int time;
}; void bfs(int,int);
int main(void)
{
int n,m; while(scanf("%d%d",&n,&m) != EOF)
{
if(n == m)
puts("");
else if(n > m)
printf("%d\n",n - m);
else
bfs(n,m);
}
} void bfs(int n,int m)
{
fill(VIS,VIS + MAX,false);
queue<Node> que;
Node first;
first.time = ;
first.n = n;
que.push(first); while(!que.empty())
{
Node cur = que.front();
que.pop();
for(int i = ;i < ;i ++)
{
Node next = cur;
if(!i)
{
next.n *= ;
next.time ++;
if(next.n > MAX)
continue;
}
else
{
next.n --;
next.time ++;
if(next.n <= )
continue;
}
if(next.n == m)
{
printf("%d\n",next.time);
return ;
} if(VIS[next.n])
continue;
VIS[next.n] = true; que.push(next);
}
} return ;
}

CF Two Buttons (BFS)的更多相关文章

  1. CF 520 B. Two Buttons(bfs)

    /*题意:一个数,就是输入的第一个数,让它变成第二个数最少用几步.可以点红色按钮,蓝色按钮来改变数字,红色:*2,蓝色:-1,如果变成负数,就变成原来的数.CF 520 B. Two Buttons思 ...

  2. cf.295.B Two Buttons (bfs)

     Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. Codeforces Round #295 (Div. 2)B - Two Buttons BFS

    B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  4. CF div2 D BFS

    http://codeforces.com/contest/676/problem/D 题目大意: 勇者去迷宫杀恶龙.迷宫是有n*m的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...

  5. CodeForces 520B Two Buttons(用BFS)

     Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

  7. cf520B-Two Buttons 【BFS】

    http://codeforces.com/contest/520/problem/B Two Buttons Vasya has found a strange device. On the fro ...

  8. Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )

    B. Two Buttons time limit per test : 2 seconds memory limit per test :256 megabytes input :standard ...

  9. 【打CF,学算法——二星级】CF 520B Two Buttons

    [CF简单介绍] 提交链接:Two Buttons 题面: B. Two Buttons time limit per test 2 seconds memory limit per test 256 ...

随机推荐

  1. Apache Spark GraphX

    GraphX基于BSP模型,在Spark之上封装类似Pregel的接口,进行大规模同步全局的图计算,尤其是当用户进行多轮迭代时,基于Spark内存计算的优势尤为明显.

  2. Foreach与Random

    [记忆贴] 1)foreach语句可以用于数据或者其他任何Iterable,但是并不意味着数组肯定也是一个Iterable,而任何自动包装也不会自动发生. package thinking.in.ja ...

  3. JSF 2 multiple select listbox example

    In JSF, <h:selectManyListbox /> tag is used to render a multiple select listbox – HTML select ...

  4. 使用paramiko进行打包操作

    使用paramiko执行ssh命令的时候有一个很坑爹的地方:它无法准确的识别你的系统环境变量,所以使用一些命令的时候会发现,直接在系统中执行该命令的时候可以,但是换成paramiko执行的时候会报错说 ...

  5. oracle 11g 之 result cache

    oracle 11g 之 result cache 今天是2013-10-12,打算最近时间研究一下shared pool的相关原理以及awr报告分析.今天学习一下在oracle 11g shared ...

  6. Struts2内建校验器(基于校验框架的文件校验)

    位于xwork-2.0.4.jar压缩包中( com.opensymphony.xwork2.validator.validators)有个文件default.xml ,该文件中定义了Struts2框 ...

  7. 在Linux(Ubuntu/openSUSE/CentOS)下配置ASP.NET(Apache + Mono)转载+补充

    错误:Network error: Connection refused 解决办法: 执行 $sudo apt-get install openssh-server 安装ssh协议 执行ifconfi ...

  8. IOS 7 开发范例 - UISwitch的使用

    Creating and Using Switches with UISwitch You would like to give your users the ability to turn an o ...

  9. alternatives命令使用方法

    alternatives命令使用方法 alternatives是Linux下的一个功能强大的命令.仅仅能在root权限下运行.如系统中有几个命令功能十分相似,却又不能任意删除,那么能够用 altern ...

  10. android拦截短信并屏蔽系统的Notification

    拦截短信有几个关键点: 1.android接收短信时是以广播的方式 2.程序只要在自己的Manifest.xml里加有"接收"SMS的权限 <uses-permission  ...