题干:

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.

解题报告:

这题做法很多。。首先一眼就是老套的bfs,,就不说了。。。注意别忘判断出现小于0的情况就行了,,不然会RE的。。也就是你给他限制了上界别忘了限制下界就行了。(复杂度on的)

还有一种方法就是倒着考虑这个问题,这个问题可以反过来说:我们应该用“对这个数加1”和“如果这个数是偶数,就除以2”的运算,得到从m开始的n。(复杂度logn的)

因为你要知道啊如果m是个奇数,也就意味着我最终一定是先得到一个偶数然后再-1得到的,所以我们就加回去成个偶数,然后偶数要想得到n,毫无疑问只能/2,然后如果发现<n了,那就直接加回来就是答案了。这倒是不难想,因为你会发现 ÷2+1+1和+1÷2,得到的效果相同,但是后者仅用了两步,,所以说明这样贪心一定是时间最短的(相当于推出了bfs的策略,就可以不用bfs直接递推就可以了)。

另外这个解法可以化简成dp的形式

AC代码1:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
char s[MAX];
int vis[50005];
int n,m;
ll mod = 1e9+7;
struct Node {int x;int t;Node(int x,int t):x(x),t(t){}
};
int bfs() {queue<Node> q;q.push(Node(n,0));while(!q.empty()) {Node cur = q.front();q.pop();if(cur.x == m) return cur.t;if(vis[cur.x]) continue;if(cur.x < 0) continue;if(cur.x > 50005) {printf("hahahhaha\n");}vis[cur.x]=1;q.push(Node(cur.x-1,cur.t+1));if(cur.x <= m) q.push(Node(cur.x*2,cur.t+1));}return -1;
}
int main()
{cin>>n>>m;if(n>=m) printf("%d\n",n-m);else {int ans = bfs();printf("%d\n",ans);}return 0 ;}

AC代码2:(思维)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int main()
{int n,m;cin>>n>>m;if(n>=m) printf("%d\n",n-m);else {int ans = 0;
//这样正着推是错的、、、。。。。
//      while(n*2<=m) {
//          n*=2;ans++;
//      }
//      if(m%2 == 0) {
//          printf("%d\n",ans+((n-m/2)+1));
//      }
//      else printf("%d\n",ans+1+n-m);while(n!=m) {if(m%2==1) ans++,m++;m/=2;ans++;if(n>=m) {ans+=(n-m);break;}}printf("%d",ans);}return 0 ;}

AC代码3:(会慢一点,100ms左右)

#include <bits/stdc++.h>
using namespace std;int n, m, dp[2][20000], curr, last, cnt;int main()
{scanf("%d %d", &n, &m);if(m < n){printf("%d\n", n-m);return 0;}dp[0][m] = 1;last = 0;curr = 1;while(cnt<10000){for(int i = 0; i<2*m; i++) {if(dp[last][i]){if(i == n){printf("%d\n",cnt);return 0;}if( i+1 < 20000 ) dp[curr][i+1] = true;if( i%2 == 0 ) dp[curr][i/2] = true;}}curr=!curr;last=!last;cnt++;}return 0;
}

AC代码4:

【CodeForces - 520B】Two Buttons (bfs或dp或时光倒流,trick)相关推荐

  1. CodeForces 520B Two Buttons

    题意:给你两个数: 要你计算让第一个数变到第二个数的最小步骤. 第一个数能翻倍或减一. 思路:队列+ bfs; #include<stdio.h> #include<iostream ...

  2. *【CodeForces - 859C 】Pie Rules (博弈dp,时光倒流)

    题干: You may have heard of the pie rule before. It states that if two people wish to fairly share a s ...

  3. Codeforces 148D. Bag of mice(概率dp)

    Codeforces 148D. Bag of mice(概率dp) Description The dragon and the princess are arguing about what to ...

  4. 第 254 场力扣周赛(KMP、贪心、快速幂、二分+多源bfs、并查集 + 时光倒流)

    第 254 场力扣周赛 稀里糊涂双眼双眼惺忪的做了三道,错了4次...还是600来名 5843. 作为子字符串出现在单词中的字符串数目 题目描述 给你一个字符串数组 patterns 和一个字符串 w ...

  5. 2021 ICPC 四川省赛 L - Spicy Restaurant(多源BFS,DP)

    Spicy Restaurant https://codeforces.com/gym/103117/problem/L 题目大意:给一个 nnn 个点 mmm 条边的点权图.再给出 qqq 个询问, ...

  6. Codeforces 1276D/1259G Tree Elimination (树形DP)

    题目链接 http://codeforces.com/contest/1276/problem/D 题解 我什么DP都不会做,吃枣药丸-- 设\(f_{u,j}\)表示\(u\)子树内,\(j=0\) ...

  7. Codeforces 264B Good Sequences ★ (分解素因子+DP)

    题目链接:http://codeforces.com/problemset/problem/264/B 题目大意:给定一个数列a1,a2,a3,a4,--,an(数据保证ai严格递增,n<=10 ...

  8. Codeforces 118 D. Caesar's Legions (dp)

    题目链接:http://codeforces.com/contest/118/problem/D 有n个步兵和m个骑兵要排成一排,其中连续的步兵不能超过k1个,连续的骑兵不能超过k2个. dp[i][ ...

  9. CodeForces - 468C Hack it!(构造+数位dp)

    题目链接:点击查看 题目大意:求出一段区间 [l,r][l,r][l,r] 的数位和对 aaa 取模后为 000.更具体的,设 f(x)f(x)f(x) 为 xxx 的数位和,本题需要求出一对 [l, ...

最新文章

  1. 逃离 AI 赛道的投资人:做局失利、破局无力
  2. 甲子光年微信公众号往期精华文章[2019-08-14]
  3. 如何复制静态文件以使用Webpack构建目录?
  4. RabbitMQ指南(上)
  5. BugKuCTF 杂项 签到题
  6. swf批量转png_CAD批量打印(探索者易打软件)优势介绍
  7. windowsXP/7下消除快捷键箭头的方法
  8. 教师资格证综合素质思维导图
  9. 【安卓开发】Webview简单使用
  10. arm+linux+entry.s,linux kernel 之底层中断机制entry_armv.S
  11. 算法 matlab_MATLAB遗传算法及其实现
  12. Hiho----无间道之并查集
  13. Bin Code Editor格式化JSON编辑器
  14. 这几个棘手的面试常见问题,如何高情商的回答?
  15. 修改计算机管理员administrator的密码
  16. 《Android项目实战-博学谷》应用图标欢迎界面
  17. 武忠祥老师每日一题||不定积分基础训练(六)
  18. ubuntu16.04笔记本查看电脑配置(CPU,显卡,内存,硬盘)
  19. HDU5142 NPY and arithmetic progression BestCoder Round #23 1002
  20. 教你九种 JVM GC 问题的排查方法

热门文章

  1. [Leetcode][第40题][JAVA][数组总和2][回溯][剪枝]
  2. [剑指offer][JAVA]面试题第[33]题[二叉搜索树的后序遍历][单调栈][递归分治]
  3. [众包]Eclipse 运行简单亚马逊AMT模板
  4. zemax模拟ld_Zemax光学设计实例(83)在非序列模式下LD阵列的光束整形
  5. java se程序设计课后答案,JAVA SE程序设计及实践
  6. python查看各列数据类型_pandas中查看数据类型的几种方式
  7. ubuntu13.10无法登陆
  8. 【转】c#数字图像处理(二)彩色图像灰度化,灰度图像二值化
  9. 【转】__declspec用法详解
  10. 【转】1.6异步编程:IAsyncResult异步编程模型 (APM)