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.

 #include<stdio.h>
#include<queue>
#include<set>
using namespace std;
int n , m ; int main ()
{
//freopen ("a.txt" , "r" , stdin) ;
while (~ scanf ("%d%d" , &n , &m)) {
queue <pair <int , int> > q ;
set <int> s ;
q.push ( {n , } ) ;
while (!q.empty ()) {
pair <int , int> p = q.front () ;
q.pop () ;
if (p.first <= )
continue ;
if (p.first == m) {
printf ("%d\n" , p.second) ;
break ;
}
else {
s.insert (p.first) ;
if (s.count (p.first - ) == ) {
q.push ( {p.first - , p.second + } ) ;
}
if (p.first < m && s.count (p.first * ) == ) {
q.push ( {p.first * , p.second + } ) ;
}
}
}
}
return ;
}

我是用bfs(看别人的),见识了set 和 pair 的用法。

There is, however, an even faster solution. The problem can be reversed as follows: we should get the number n starting from m using the operations "add 1 to the number" and "divide the number by 2 if it is even".

cf.295.B Two Buttons (bfs)的更多相关文章

  1. CF 520 B. Two Buttons(bfs)

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

  2. CF Two Buttons (BFS)

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

  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 295 div 2 B (bfs)

    题意:给出 n.m 两数,可以对 n 进行两种操作 减一或者乘二,操作过程中 n 必须保证非负,问使 n 变为 m 至少需要几步操作. 这是我练水题的时候做到的,题目不难,只是我 bfs 一直没怎么用 ...

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

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

  6. CF 986A Fair——多源bfs

    题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...

  7. CF G. Orientation of Edges BFS

    来两遍 $BFS,$ 都贪心一下即可. #include <bits/stdc++.h> #define maxn 300009 using namespace std; void set ...

  8. cf.295.C.DNA Alignment(数学推导)

    DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. CF#541 D. Gourmet choice /// BFS 拓扑

    题目大意: 给定n m 第一行有n个数 第二行有m个数 接下来n行每行m列 有 = < > 位于 i j 的符号表示 第一行第i个数与第二行第j个数的大小关系 1.将n+m个数 当做按顺序 ...

随机推荐

  1. Android开发的那些坑和小技巧

    1.android:clipToPadding 意思是控件的绘制区域是否在padding里面.默认为true.如果你设置了此属性值为false,就能实现一个在布局上事半功陪的效果.先看一个效果图. 上 ...

  2. Java学习笔记(二十)——Java 散列表_算法内容

    [前面的话] 周末,本来打算找人去玩,结果没找到,所以我只好有学习了. 为什么会学习散列表,因为要使用HashMap?因为在做项目的时候,在服务器和客户端需要传输DTO,而传输的属性是动态增加的,所以 ...

  3. tsql的奇特语法

    也许是离开t-sql太久了,突然发现很多t-sql的奇特语法 用一句sql解决多种排序: ; SELECT C1, C2 FROM T ORDER BY THEN C1 END ASC, THEN C ...

  4. JSON Web Token - 在Web应用间安全地传递信息(zhuan)

    来自 http://blog.leapoahead.com/2015/09/06/understanding-jwt/ JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用 ...

  5. 配置域从DNS服务器以及缓存DNS服务器

    一.域从DNS服务器的作用 我们在之前上一篇随笔里有提到,DNS服务器一般有三种类型,一个是Primary DNS Server(主DNS服务器),一个是Secondary DNS Server(从D ...

  6. popupwindow与软键盘的问题

    1.软键盘弹出时不会改变布局(覆盖页面的效果)android:windowSoftInputMode="adjustPan" 2.软键盘弹出时不会覆盖popuwindowsetSo ...

  7. Ibatis中传List参数

    Ibatis中用list传参数的方式. Java代码  select count(id) from `user` where id in #[]# and status=1 . <select ...

  8. new-nav-html

    <header id="masthead" class="masthead" role="banner"> <h1 cla ...

  9. .net架构设计读书笔记--第三章 第9节 域模型实现(ImplementingDomain Model)

        我们长时间争论什么方案是实现域业务领域层架构的最佳方法.最后,我们用一个在线商店案例来说明,其中忽略了许多之前遇到的一些场景.在线商店对很多人来说更容易理解. 一.在线商店项目简介 1. 用例 ...

  10. @RestController注解下返回到jsp视图页面(转)(转)

    这个问题我也遇到过,下面的方法可以试试 蓝萝卜blu @RestController注解下返回到jsp视图页面 spring4.1中添加了@RestController注解很方便,集成了@Respon ...