http://codeforces.com/problemset/problem/520/B

B. Two Buttons
time limit per test

2 seconds

memory limit per test

256 megabytes

input:standard input
output:standard output

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 test(s)
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.

分析:

直接模拟,从后面往前面推。

AC代码:

 #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <vector>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#pragma warning(disable:4786) using namespace std; const int INF = 0x3f3f3f3f;
const int MAX = + ;
const double eps = 1e-;
const double PI = acos(-1.0); int main()
{
int n , m;
while(~scanf("%d %d",&n , &m))
{
int ans = ;
while(n < m)
{
if(m % )
{
m ++;
ans ++;
}
else
{
m /= ;
ans ++;
}
}
cout << ans + n - m << endl;
}
return ;
}

codeforces 520 Two Buttons的更多相关文章

  1. codeforces 520 Pangram

    http://codeforces.com/problemset/problem/520/A A. Pangram time limit per test 2 seconds memory limit ...

  2. CodeForces 520B Two Buttons(用BFS)

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

  3. 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 ...

  4. CodeForces 520B Two Buttons

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

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

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

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

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

  7. Codeforces Round #520 (Div. 2)

    Codeforces Round #520 (Div. 2) https://codeforces.com/contest/1062 A #include<bits/stdc++.h> u ...

  8. CF 520 B. Two Buttons(bfs)

    /*题意:一个数,就是输入的第一个数,让它变成第二个数最少用几步.可以点红色按钮,蓝色按钮来改变数字,红色:*2,蓝色:-1,如果变成负数,就变成原来的数.CF 520 B. Two Buttons思 ...

  9. 【codeforces 520B】Two Buttons

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

随机推荐

  1. Git相关的项目

    1.posh-git Git的PowerShell扩展 项目地址: https://github.com/dahlbyk/posh-git 可以用psget快速安装扩展模块,psget下载安装地址 h ...

  2. 三 mybatis typeAlias(别名)使用和resultMap使用

     1.MyBatis提供的typeAlias

  3. java TimeUnit synchronized demo

    import java.util.concurrent.TimeUnit; public class TestCalc { private static boolean stopRequested=f ...

  4. 用正则验证字符串格式,形如:A)XXX B)XXXX C)XXX

    今天遇到个小功能,要验证某个英文选项是否正确,例如:A)accumulate B)circling C)communities  D)competition  E)domestic F)financi ...

  5. Qt 之 自定义提示信息框—迅雷风格(模拟QDialog类的exec()方法) good

    http://blog.csdn.net/goforwardtostep/article/details/53614830

  6. Lazarus for Raspbian安装

    春节前看到树莓派 2代开始销售,第一时间在淘宝下单购买,无奈春节期间放假,要到3月份才可能收到,只能用QEMU模拟器先熟悉树莓系统.对从turbo Pascal开始的人来讲,如果能在树莓系统使用Pas ...

  7. Bundle文件的创建和使用(二)

    1.概念: An NSBundle object represents a location in the file system that groups code and resources tha ...

  8. 使用ASP.NET web API创建REST服务(二)

    Creating a REST service using ASP.NET Web API A service that is created based upon the architecture ...

  9. java字符串和unicode互转

    直接上代码 private static String decodeUnicode(String input) { if (null == input) return input; int len = ...

  10. ArcGIS API for Silverlight 之ElementLayer使用及TextSymbol的模板使用

    原文:ArcGIS API for Silverlight 之ElementLayer使用及TextSymbol的模板使用 在开发中动态在地图上添加文字信息,可以使用TextSymbol添加文字 // ...