B. 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.

Examples
input

Copy
4 6
output

Copy
2
input

Copy
10 1
output

Copy
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.

本题的题意,有台带两个不同颜色按钮的装置,按红色按钮对当前数乘2,按蓝色按钮对当前数减1,且得到的数为正数。

给定一个数n,用该装置进行操作,进行若干次操作后使该数正好等于数m,问所需最少的操作步数。

这题我的想法是用bfs来做,网上有说用贪心做,没细看。前几次交的代码各种tlm,没进行良好的剪枝。

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
const int MAX_N = ;
int vis[MAX_N]; int main(){
// freopen("D:\\in.txt","r",stdin);
// freopen("D:\\out.txt","w",stdout);
memset(vis,,sizeof(vis));
queue<int> q;
int n,m;
cin>>n>>m;
bool ans = false;
vis[n] = ;
q.push(n);
if(n >= m)
cout<<n-m<<endl;
else{
while(q.size()){
int a = q.front();
q.pop(); if(a == m){
ans = true;
break;
}
int temp = a*;
if(temp > && temp < m * && vis[temp]==){
vis[temp] = vis[a]+;
q.push(temp);
}
temp = a-;
if(temp > && temp < m * && vis[temp]==){
vis[temp] = vis[a]+;
q.push(temp);
}
}
} if(ans)
cout<<vis[m]-<<endl;
return ;
}

Codeforces Round #295 (Div. 2) B. Two Buttons 520B的更多相关文章

  1. 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 ...

  2. 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 ...

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

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

  4. 【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons

    题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m: 随后发篇随笔普及下memset函数的初始化问题.自己也是涨了好多姿势. 代码 #include<iostream> ...

  5. Codeforces Round #295 (Div. 2) B. Two Buttons (DP)

    题意:有两个正整数\(n\)和\(m\),每次操作可以使\(n*=2\)或者\(n-=1\),问最少操作多少次使得\(n=m\). 题解:首先,若\(n\ge m\),直接输出\(n-m\),若\(2 ...

  6. Codeforces Round #295 (Div. 2)

    水 A. Pangram /* 水题 */ #include <cstdio> #include <iostream> #include <algorithm> # ...

  7. codeforces 521a//DNA Alignment// Codeforces Round #295(Div. 1)

    题意:如题定义的函数,取最大值的数量有多少? 结论只猜对了一半. 首先,如果只有一个元素结果肯定是1.否则.s串中元素数量分别记为a,t,c,g.设另一个串t中数量为a',t',c',g'.那么,固定 ...

  8. Codeforces Round #295 (Div. 2)C - DNA Alignment 数学题

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

  9. Codeforces Round #295 (Div. 2)A - Pangram 水题

    A. Pangram time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

随机推荐

  1. 郑宇以城市计算研究膺选 MIT 科技创新35俊杰 (TR35)

    MIT 科技创新35俊杰 (TR35)"> 编者按:<MIT Technology Review>于8月22日发布了令人瞩目的2013年全球杰出青年创新者(MIT TR35 ...

  2. @Transactional回滚问题(try catch、嵌套)

    Spring 事务注解 @Transactional 本来可以保证原子性,如果事务内有报错的话,整个事务可以保证回滚,但是加上try catch或者事务嵌套,可能会导致事务回滚失败.测试一波. 准备 ...

  3. python之接口开发

    一.接口开发的思路 1.启动一个服务: 2.接受客户端传过来的数据: 3.登录,注册,支付等功能 4.操作数据库,拿到数据: 5.返回数据: import flask server=flask.Fla ...

  4. WebElement--定位经验

    通常,我们这页面中定位一个元素,如果HTML中明明有却定位不到,我们一定会从这两个方面考虑. 第一:是不是页面中有多个iframe/frame结构,很多情况下我们需要通过切换iframe/frame结 ...

  5. 通俗易懂JSONP讲解

    原文地址:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html JSON的格式或者叫规则: JSON能够 ...

  6. iOS 仿看了吗应用、指南针测网速等常用工具、自定义弹出视图框架、图片裁剪、内容扩展等源码

    iOS精选源码 扩展内容的cell - folding-cell 一个近乎完整的可识别中国身份证信息的Demo 可自动快速... JPImageresizerView 仿微信的图片裁剪 带年月和至今以 ...

  7. 因为AI,所以爱

    作为技术驱动型公司 自我颠覆的核心就是技术上有所突破 2019技术奇点大会上,创始人行在提出 「未来,大数据和人工智能 将成为商业升级的智能发动机」 这与我们的使命不谋而合 时间退回到2016年的冬天 ...

  8. FPGA时序分析

    更新于20180823 时序检查中对异步复位电路的时序分析叫做()和()? 这个题做的让人有点懵,我知道异步复位电路一般需要做异步复位.同步释放处理,但不知道这里问的啥意思.这里指的是恢复时间检查和移 ...

  9. F5 BIG-IP之二LTM术语

    Node : Pool:中有设置负载均衡方式 Virtual Server :监听客户端请求的 根据 IP 和 端口号 Profile: VS 调用 Profile

  10. Zabbix - 修改默认的 Web 访问URL

    背景 CentOS 7 64 Zabbix 4 LTS 使用RPM在线方式安装 修改过程 # 修改默认的访问路径:http://x.x.x.x/zabbix 修改为 http://x.x.x.x/ v ...