CodeForces 520B Two Buttons
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
4 6
2
10 1
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的更多相关文章
- CodeForces 520B Two Buttons(用BFS)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces.520B.Two Buttons(正难则反)
题目链接 \(Description\) 给定两个数\(n,m\),每次可以使\(n\)减一或使\(n\)乘2.求最少需要多少次可以使\(n\)等于\(m\). \(Solution\) 暴力连边BF ...
- Codeforces 520B:Two Buttons(思维,好题)
题目链接:http://codeforces.com/problemset/problem/520/B 题意 给出两个数n和m,n每次只能进行乘2或者减1的操作,问n至少经过多少次变换后能变成m 思路 ...
- 【codeforces 520B】Two Buttons
[题目链接]:http://codeforces.com/contest/520/problem/B [题意] 给你一个数n; 对它进行乘2操作,或者是-1操作; 然后问你到达m需要的步骤数; [题解 ...
- codeforces 520 Two Buttons
http://codeforces.com/problemset/problem/520/B B. Two Buttons time limit per test 2 seconds memory l ...
- 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 ...
- 【打CF,学算法——二星级】CF 520B Two Buttons
[CF简单介绍] 提交链接:Two Buttons 题面: B. Two Buttons time limit per test 2 seconds memory limit per test 256 ...
- CF520B——Two Buttons——————【广搜或找规律】
J - Two Buttons Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
- ACM训练赛:第20次
这次的题思维都很强,等之后的考试结束会集中精力重新训练一些思维题. A - A simple question CodeForces - 520B 思路: 直接看的话,很容易发现如果 \(n > ...
随机推荐
- SQL Server 2008 R2 安装出错:Could not open key
安装Sql Server 2008 R2 企业版出现错误提示无法继续安装,错误提示为: Could not open key: UNKNOWN\Components\7ABFE44842C12B390 ...
- IOS-Uikit框架介绍
•UIKit可识别三种类型的输入事件: –触摸事件 –运动(加速计)事件 –远程控制事件 IKit框架将触击信息封装为一个UIEvent对象,并派发给恰当的视图(有关UIKit如何将事件递送给您的视图 ...
- ios tableview 适配横竖屏
tableview.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
- 常用js函数整理--common.js
var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data: data, dataType: 'json ...
- !带有指针的类和struct赋值的本质 - host to device
//这个变量必须在while循环外面 //原因是当将loadModels[modelNum].g_3DModel[0]赋值给新建类后 //里面的数值拷贝过去了,而里头的指针只给了地址 //所以如果这个 ...
- 类模板的static成员
下列代码可以通过编译吗?如何修改使其通过编译? template <class T> struct sum { static void foo(T op1 , T op2){ c ...
- PHP之MVC项目实战
本文主要包括以下内容 类文件自动加载 路径管理 页面跳转 注册自动加载方法 配置文件系统 cookie session 类文件自动加载 在PHP中使用别的类时,需要载入类文件,如果类很多的话,需要重复 ...
- Android手绘效果实现
效果图 原理 大概介绍一下实现原理.首先你得有一张图(废话~),接下来就是把这张图的轮廓提取出来,轮廓提取算法有很多,本人不是搞图像处理的,对图像处理感兴趣的童鞋可以查看相关资料.如果你有好的轮廓提取 ...
- sdut 2498【aoe 网上的关键路径】
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2498 代码超时怎么破: #include< ...
- 算法系列:XXX
转载自http://www.cnblogs.com/skynet/p/3372855.html 这次分享的宗旨是——让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择 ...