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. WinForm相关注意点

    1. //this.dgvEmployees.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue; //dgvEmployees.RowHeade ...

  2. 【Excel 4.0 函数】REGISTER 的两种形式以及VBA等效语句

    形式1 REGISTER("SAMPLE.DLL", "MyFunction", "AIC") 形式1等效 VBA语句 Declare Fu ...

  3. yii php 图片上传与生成缩略图

    今天需要做图片上传与生成缩略图的功能,把代码进行记录如下: html 视图              ($pic_action_url = $this->createAbsoluteUrl('h ...

  4. [Android Pro] StarUML 版本破解

    参考:http://bbs.chinapyg.com/thread-79022-1-1.html 官网下载地址 : http://staruml.en.softonic.com 各平台版本均适用,本文 ...

  5. Innodb之拷贝InnoDB表从一服务器到另一台服务器

    将Innodb类型的表从一台服务器拷贝到另一台服务器,或从一个库拷贝到另一个库. 前提是:innodb_file_per_table =ON. 1 先在目标服务器(库)上创建一个相同的表结构. 如: ...

  6. 警告 - no rule to process file 'WRP_CollectionView/README.md' of type net.daringfireball.markdown for architecture i386

    warning: no rule to process file '/Users/mac/Downloads/Demo/Self/WRP_CollectionView/WRP_CollectionVi ...

  7. vsftp详细配置(转)

    详细配置转载来自以下链接: http://yuanbin.blog.51cto.com/363003/108262 vsftp源码下载(vsftpd-3.0.2.tar.gz): http://dow ...

  8. vpn,可以连接上,但是不能访问局域网内共享的文件怎么办

    不选用VPN的上网关,就可以识别域用户访问共享文件的权限了,在VPN连接的属性里双击TCP/IP协议-高级-去掉勾选"在远程网络上使用默认网关".

  9. 重温WCF之数据契约和序列化(四)

    一.数据契约 1.使用数据协定可以灵活控制哪些成员应该被客户端识别. [DataContract] public class Employee { [DataMember] public string ...

  10. 【PHP数组的使用】

    PHP数组使用关键字array标识,数组内的元素可以是任意类型,而且可以不是同一种类型,这和c.java不同. 遍历数组的方法可以使用foreach,也可以使用for循环 可以使用print_r或者v ...