B. Two Buttons

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

两种思路,数学方法算出结果or暴力bfs

以下为数学算法(逆推思路)

if n>m ans=ans+n-m

if n<=m

if m为奇数 m+1后再除2

if m为偶数 m直接除2

若n==m ans=ans

若n>m ans=ans+n-m

代码如下

 #include <iostream>
using namespace std;
int a[];
int main(){
int n,m,ans(),j();
cin>>n>>m;
if(n>m){
cout<<n-m;
return ;
}
a[m]++;
while(m!=n){
if(n>m){
cout<<ans+n-m;
return ;
}
if(m%==&&a[m/]==) {
m=m/;
a[m]++;
ans++;
}
else {
m++;
a[m]++;
ans++;
}
}
cout<<ans<<'\n';
}

Codeforces-Two Buttons-520problemB(思维题)的更多相关文章

  1. CF--思维练习-- CodeForces - 215C - Crosses(思维题)

    ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...

  2. Codeforces 675C Money Transfers 思维题

    原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...

  3. Codeforces 1090D - Similar Arrays - [思维题][构造题][2018-2019 Russia Open High School Programming Contest Problem D]

    题目链接:https://codeforces.com/contest/1090/problem/D Vasya had an array of n integers, each element of ...

  4. codeforces 1140D(区间dp/思维题)

    D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Codeforces 957 水位标记思维题

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  6. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  7. codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题

    http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...

  8. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  9. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  10. CodeForces - 631C ——(思维题)

    Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...

随机推荐

  1. make和rpm的编译、打包总结

    1  make工具使用 1.1 makefile基本规则 Make工具最主要也是最基本的功能就是通过makefile文件来描述源程序之间的相互关系并自动维护编译工作. Makefile的规则: tar ...

  2. [golang]写了一个可以用 go 来写脚本的工具:gosl

    转自:https://golangtc.com/t/53cca103320b52060a000030 写了一个可以用 go 来写脚本的工具:gosl 代码和使用说明可以看这里: http://gith ...

  3. Pytest权威教程22-优质集成实践

    目录 优质集成实践 使用pip安装包 Python测试发现的约定 选择测试布局结构/导入规则 在应用程序代码外测试 测试作为应用程序代码的一部分 tox 返回: Pytest权威教程 优质集成实践 使 ...

  4. python 日志内容提取

    问题:如下,一个很大的日志文件,提取 start: 到 end: 标志中间的内容 日志文件a.log xxxxx yyyyy start: start: hahahaha end: start: ha ...

  5. Thingsboard Gateway开发环境

    源码下载地址:https://github.com/thingsboard/thingsboard-gateway 国内大神源码地址:https://github.com/guodaxia103/th ...

  6. gitlab 默认端口修改文件

    vim /var/opt/gitlab/nginx/conf/gitlab-http.conf listen *:80;

  7. android studio: 快捷键生成getter/setter方法时自动加m的问题

    平时使用Android Studio 在写实体类的时候,习惯给实体类的成员变量前面加上一个"m" 修饰符表示这是一个成员变量,这也是搞java的一种约定俗成的写法,本来这是没有问题 ...

  8. mysql - ERROR 1114 (HY000): The table is full

    mysql - ERROR 1114 (HY000): The table is full - Stack Overflowhttps://stackoverflow.com/questions/73 ...

  9. 安装mysql报错:Can't find messagefile '/usr/share/mysql/english/errmsg.sys'和/usr/bin/mysqladmin: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or

    使用yum安装mysql服务端: [root@centos ~]# yum -y install mysql-server Loaded plugins: fastestmirror, securit ...

  10. 【FreeMarker】FreeMarker快速入门(一)

    什么是 FreeMarker FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具. 它不是面向最终用户 ...