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. Redash(开源轻量级商业智能) 生产环境部署及实践 (without docker)

    一直在调研一个轻量级开源的 BI 系统.之前我们生产环境使用的 aliyun 的 QuickBi,也调研了另外一个 airflow 的开源商业智能 superset.不得不承认 QuickBI 正在日 ...

  2. linux protobuf 测试官方例子遇到报错及解决办法。

    测试例子时出现报错如下,在最下面会写出安装流程. -------------------------------------报错----1------------------------------- ...

  3. PreTranslateMessage中有调用模态对话框的解决方法

    原文:https://blog.csdn.net/guoguojune/article/details/45332511 dlg.DoModal()截住了界面消息,所以返回时原来的pMsg的内容已经更 ...

  4. mac 安装 pycharm

    下载安装 链接:https://pan.baidu.com/s/19Hm6yZPL_mOTVAb5YQBZKA 密码:j73n 激活码 56ZS5PQ1RF-eyJsaWNlbnNlSWQiOiI1N ...

  5. 使用xshell-ssh连接服务器,报错:Xshell Socket error Event: 32 Error: 10053

    XShell连接CentOS系统时,报出Xshell Socket error Event: 32 Error: 10053..错误 有点烦人.. 操作:用SSH工具连接linux电脑出现的问题:Re ...

  6. 教你怎样用fluent长网格【转载】

    转载地址: http://blog.sina.cn/dpool/blog/s/blog_63a80e870100k1jo.html?type=-1 有的时候,当你十月怀胎,千辛万苦把网格生出来,导入f ...

  7. BurpSuite intuder里保存所有网页的特定内容:以bugku的cookies欺骗为例题

    这题里想读取index.php只能一行一行的读,通过控制line参数的值.如下: 正常的writeup都是写个爬虫,但我觉得burp肯定有自带这种功能,不用重造轮子.经过学习后发现以下步骤可以实现. ...

  8. ubuntu之路——day10.3 train/dev/test的划分、大小和指标更新

     train/dev/test的划分 我们在前面的博文中已经提到了train/dev/test的相关做法.比如不能将dev和test混为一谈.同时要保证数据集的同分布等. 现在在train/dev/t ...

  9. Nexus入门【转】

    介绍 DevOps平台采用的介质服务器类型为NEXUS,NEXUS是一个强大的maven仓库管理器,它极大的简化了本地内部仓库的维护和外部仓库的访问. 一.配置Maven [root@meteor ~ ...

  10. MySQL参数: innodb_flush_log_at_trx_commit和sync_binlog

    innodb_flush_log_at_trx_commit 当innodb_flush_log_at_trx_commit=0时, log buffer将每秒一次地写入log file, 并且log ...