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 ;
}

最新文章

  1. js动态的把左边列表添加到右边,可删除。
  2. List 集合线程安全测试
  3. 内容营销三大实用法则(内含干货)-同样可运用在EDM数据营销中
  4. EF code First数据迁移学习笔记(转)
  5. iOS中3种正则表达式的使用与比较
  6. 开机就提示“请安装TCP/IP协议,error=10106”的解决的方法
  7. Android进阶笔记18:选用合适的IPC方式
  8. android studio 中获取sha1
  9. ArcGis :正尝试在 OS 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。
  10. unity3d Find()使用
  11. ruby和Python简单对比
  12. 利用指针突破C++编译器的防线
  13. 国籍控件(js源码)
  14. mysql转ElasticSearch的案例分析
  15. EsRejectedExecutionException排错与线程池类型
  16. Centos下Sphinx的下载与编译安装
  17. 阿里巴巴excel工具easyexcel 助你快速简单避免OOM
  18. SSH下authorized_keys, id_rsa, id_rsa.pub, known_hosts作用
  19. 把post请求的地址粘贴到浏览器地址栏敲回车报错405[Method Not Allowed]
  20. git pull时解决分支分叉(branch diverged)问题

热门文章

  1. vector 初始化所有方法
  2. Python之 for循环\while循环
  3. PHP 依赖注入 (转)
  4. NRF51822之app_button使用
  5. Shell 字符串常见操作
  6. ECSHOP给分类添加图
  7. 分布式服务框架dubbo原理解析 转
  8. &lt;dependency&gt;spring-webmvc&lt;/dependency&gt;
  9. Kafka使用入门教程 简单介绍
  10. Java 找到数组中两个元素相加等于指定数的所有组合