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 > ...
随机推荐
- POJ 3597 Polygon Division (DP)
题目链接 题意:把一个正多边形分成数个三角形或者四边形,问有多少种方案. 题解: 如果分出的全为三角形的话,那就是正多边形三角剖分问题.它的结果就是Catalan数.现在也可以划分出四边形的话,可以采 ...
- java获得本机IP,名称等
import java.net.InetAddress; import java.net.UnknownHostException; public class GetLocalIP { public ...
- 6. ZigZag Conversion
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- C#复制、粘贴文本信息到剪贴板
复制:private void button1_Click(object sender, System.EventArgs e) { // Takes the selected text from a ...
- 二叉树计数(codevs 3112)
题目描述 Description 一个有n个结点的二叉树总共有多少种形态 输入描述 Input Description 读入一个正整数n 输出描述 Output Description 输出一个正整数 ...
- 关于s:iterator 和s:if 的结合使用
<s:iterator value="list" status="st"> <div class="sidebar-nav" ...
- 20145206邹京儒《Java程序设计》第5周学习总结
20145206 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 8.1 语法与继承架构 package CH5; /** * Created by Administrato ...
- 两个viewport的故事(第一部分)
原文:http://www.quirksmode.org/mobile/viewports.html 在这个迷你系列的文章里边我将会解释viewport,以及许多重要元素的宽度是如何工作的,比如< ...
- Python下安装MySQLdb
前提是你已经安装过mysql 1.从https://pypi.python.org/pypi/MySQL-python/下载MySQL-python,然后用rz命令上传到相关目录 2.用tar -zx ...
- linux 下查mac
sh-4.1# cat /sys/class/net/eth0/address 4c:cc:6a::9a: sh-4.1# ifconfig -a |grep 'HWaddr'|awk '{print ...