Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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.

Sample Input

Input
4 6
Output
2
Input
10 1
Output
9

Hint

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.

思路1:math 对于n < m时,从m出发,若m为偶数,m减半,否则,加1减半(对应结果加1),直到m <= n

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, m;
void solve()
{
cin >> n >> m;
if(n >= m) {cout << n - m << endl;return;}
int ans = ;
while(n < m)
{
if(m & ) {ans++;m++;}
m >>= ;
ans++;
}
ans += n - m;
cout << ans << endl;
}
int main()
{
solve();
return ;
}

思路2:bfs + 剪枝(记忆化)

/*times    memy
78ms 2104k
by orc
*/
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
int n ,m;
bool vis[];//此处的vis标记数组并不是像以往一样标记有没有做过而做到不走重复路径
struct node{ //这里是做备忘录,记忆化搜索
int x;
int cnt;
};
void bfs(node v)
{
memset(vis,false,sizeof vis);
queue<node> que;
que.push(v);
node now, nex;
while(!que.empty())
{
now = que.front();
que.pop();
if(vis[now.x]) continue;
if(now.x <= ) continue;//既然要做备忘录,那么下标就不能为0
if(now.x > m){ //重要剪枝,若now.x > m, 即now.x * 2就没必要入队,只能通过 - 1 来达到状态m
nex.x = now.x - ;
nex.cnt = now.cnt + ;
que.push(nex);
continue;
}
if(now.x == m) {cout << now.cnt << endl; return;}
nex.x = now.x - ;
nex.cnt = now.cnt + ;
que.push(nex);
nex.x = now.x * ;
nex.cnt = now.cnt + ;
que.push(nex);
vis[now.x] = true;//循环结尾处对当前出队元素now标记
}
}
int main()
{
cin >> n >> m;
if(n >= m) cout << n - m << endl;
else
{
node v;
v.x = n;
v.cnt = ;
bfs(v);
}
}

CodeForces 520B Two Buttons的更多相关文章

  1. CodeForces 520B Two Buttons(用BFS)

     Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. Codeforces.520B.Two Buttons(正难则反)

    题目链接 \(Description\) 给定两个数\(n,m\),每次可以使\(n\)减一或使\(n\)乘2.求最少需要多少次可以使\(n\)等于\(m\). \(Solution\) 暴力连边BF ...

  3. Codeforces 520B:Two Buttons(思维,好题)

    题目链接:http://codeforces.com/problemset/problem/520/B 题意 给出两个数n和m,n每次只能进行乘2或者减1的操作,问n至少经过多少次变换后能变成m 思路 ...

  4. 【codeforces 520B】Two Buttons

    [题目链接]:http://codeforces.com/contest/520/problem/B [题意] 给你一个数n; 对它进行乘2操作,或者是-1操作; 然后问你到达m需要的步骤数; [题解 ...

  5. codeforces 520 Two Buttons

    http://codeforces.com/problemset/problem/520/B B. Two Buttons time limit per test 2 seconds memory l ...

  6. Codeforces Round B. Buttons

    Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you ...

  7. 【打CF,学算法——二星级】CF 520B Two Buttons

    [CF简单介绍] 提交链接:Two Buttons 题面: B. Two Buttons time limit per test 2 seconds memory limit per test 256 ...

  8. CF520B——Two Buttons——————【广搜或找规律】

    J - Two Buttons Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  9. ACM训练赛:第20次

    这次的题思维都很强,等之后的考试结束会集中精力重新训练一些思维题. A - A simple question CodeForces - 520B 思路: 直接看的话,很容易发现如果 \(n > ...

随机推荐

  1. 转载jQuery图片放大插件[twiPicZoom]

    转载http://xuzhihong1987.blog.163.com/blog/static/26731587201312821725913/ 功能说明: 双击查看大图,鼠标滚动放大缩小,能够切换到 ...

  2. 240个jquery插件(转)

    http://www.kollermedia.at/archive/2007/11/21/the-ultimate-jquery-plugin-list/File upload Ajax File U ...

  3. a标签的妙用-拨打电话、发送短信、发送邮件

    前端时间在做手机WAP网站时,遇到需要点击页面上显示的电话号能直接拨号的需求,查找资料发现可以使用html的a标签完美实现该需求!记录下来以备后用...... 目前主流手机浏览器对H5的支持已经很不错 ...

  4. Express 4 中如何使用connect-mongo

    正在跟随上面的教程一步一步做,在会话支持那一节中安装 connect-mongo 后,添加: var MongoStore = require('connect-mongo')(express); v ...

  5. mysql 查看存储引擎的状态 show engine innodb status 详解

    首先,让我们来了解一下 SHOW INNODB STATUS 输出的基础,它打印了很多关于 InnoDB 内部性能相关的计数器.统计.事务处理信息等.在 MySQL 5 中,InnoDB 的性能统计结 ...

  6. Git 、 Cocoapods常用命令

    Git常用命令 1.添加文件   git  add  xxx 2.提交更新到本地  git commit   -m  'local-repo' 3.提交更新    git  push master  ...

  7. Android RadioButton selector背景

    RadioButton selector 背景 <?xml version="1.0" encoding="utf-8"?> <selecto ...

  8. iOS - 常用的宏定义

    1.处理NSLog事件(开发者模式打印,发布者模式不打印) 1 2 3 4 5   #ifdef DEBUG   #define NSLog(FORMAT, ...) fprintf(stderr,& ...

  9. oracle 10g\11g用imp导入数据的时候报错:ORA-01658: 无法为表空间 MAXDATA 中的段创建 INITIAL 区 错误解决

    备份文件是从11g中通过exp命令导出的,在10g下使用imp导入出现了上述错误,以为是低版本的不支持高版本的备份,于是使用11g测试,还是上面的问题. 其实是表空间大小不够的问题,下面是网上的解答: ...

  10. struts拦截器实现原理

    图1: 上1来源于Struts2官方站点,是Struts 2 的整体结构. 一个请求在Struts2框架中的处理大概分为以下几个步骤 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请 ...