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. day17-反射

    #反射最常用的两个方法:hasattr getattr # 1. 反射对象属性,反射对象方法: class Goods: def __init__(self,name): self.name = na ...

  2. scala slick mysql utf8mb4 支持

    语言  scala sql包  slick 3.2.0 数据库  mysql https://stackoverflow.com/questions/36741141/scala-slick-jdbc ...

  3. 正则表达式sed学习(二)

    sedsed是一个流编辑器,非交互式的编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space)接着用 sed 命令处理缓冲区的内容,处理完成 ...

  4. wireshark的过滤命令

    1.ip.addr == 192.168.1.1 这种是目标地址和源地址都是 后面指定的IP

  5. Date类与SimpleDateFormat类中parse()方法和format()方法

    package ppt11util类; import java.text.ParseException; import java.text.SimpleDateFormat; import java. ...

  6. 未来科技城 x 奇点云打造「企业数据大脑」,助力1.3万家企业服务

    “当前,政府数字化和数字政府建设已成为一种趋势.一种必然,并且有了一条水到渠成式的实现路径.” 上升为国家战略的数字中国建设加速了”智慧政务“的生动实践,杭州未来科技城的「企业数据大脑」就是一个典型. ...

  7. [LC] 23. Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  8. 106)PHP,缩略图代码和结果展示

    首先是 代码展示: <?php class CImage { /** * 生成保持原图纵横比的缩略图,支持.png .jpg .gif * 缩略图类型统一为.png格式 *@param $src ...

  9. 57)PHP,自动加载类注意项

    关于这个里面的那个if  里面判断是否是核心启动类,一般会想到,用in_array或者是isset              in_array判断是数组里面有没有这个值,但是isset判断的是数组里面 ...

  10. SHELL用法一(基本概念)

    1. SHELL 编程概念入门 1)一个完整 Linux 操作系统(Linux 内核),默认用户是不能直接操作 Linux 内核,需要借助第三方的程序或者软件去操作,例如桌面工具.命令行工具(SHEL ...