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. kubernetes flannel 网卡绑定错误,故障排查

    kubernetes 新加了个node,状态Ready,但调度过去的任务,都执行异常 查看异常节点日志 `Error adding net work: open run/flannel/subnet. ...

  2. StartDT AI Lab | 视觉智能引擎——从Face ID说起,浅析顾客数字化

    “顾客就是上帝”,这句西谚揭示了顾客占据着商业活动中心地位这一客观规律.为了能更好地服务顾客,优化商家自身的服务与产品,对顾客的分析与需求调研一直是商业经营分析中的重中之重. 在商业互联网化.社会数字 ...

  3. 复习break、continue、while、do-while的运用

    一.复习: 循环.反复执行某段语句一种语法形式. 1.基本语法: for( 初始条件 ; 循环条件 ; 状态的改变 ) { 循环体 } 循环的四要素. 循环的执行过程.初始条件--循环条件--循环体- ...

  4. python数据类型:Number数字

    Python Number数据类型用于存储数值 数据类型是不允许改变的,如果改变Number数据类型的值,将从新分配内存空间 整型(int):整型或整数,是正或者负整数,不带小数点 长整型(long) ...

  5. python语法基础-基础-变量和数据类型

    ###############   第一个python程序   ############### print("hello python") # 打印hello python # 分 ...

  6. jmeter后置处理器之Json提取器详解

    此提取器用于提取请求返回结果中的某个值或者某一组值,用法比正则表达式要简单,标准写法为$.key,其中key为返回结果map中的一个键,如果是多层则继续用.key进行即可,如果遇到key的value值 ...

  7. Hexo快速搭建博客

    1. 准备工作 ~ 安装node.js -> npm -> hexo-cli ~ 安装git -> 版本控制工具 2. 更换npm(nodejs package manager)源 ...

  8. 接口测试-chap2-关于重定向

    重定向: 301:资源永久转移 302:资源暂时转移到另一个地址了 1.如果请求接口时发生了重定向,python会自动跟随重定向,对重定向地址发起请求,最后只能得到重定向地址的响应结果.而看不到重定向 ...

  9. 编译gcc报错make[3]: Leaving directory `/usr/local/src/gcc-7.4.0/build/gcc' make[2]: *** [all-stage1-gcc] Error 2 处理

    因业务需要安装7.4高版本gcc时报错: configure: error: in `/usr/local/src/gcc-7.4.0/build/gcc': configure: error: C+ ...

  10. Java实现Luhm算法--银行卡号合法性校验

    银行卡是由"发卡行标识代码 + 自定义 + 校验码 "等部分组成的. 银联标准卡与以往发行的银行卡最直接的区别就是其卡号前6位数字的不同.   银行卡卡号的前6位是用来表示发卡银行 ...