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.

题意:

给两个数,n,m,让n通过给点两种走步方法等于m

对n可以有两种操作:

1. 减1

2.乘2

我只会BFS。。。

 #include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAX=2e4+;
const int MIN=;
struct stu
{
int num;
int step;
}start,one,two;
queue<struct stu>q;
bool vis[MAX+];
void bfs(int n,int m)
{
memset(vis,false,sizeof(vis));
start.num = n;
start.step = ;
vis[n]=;
q.push(start);
while(!q.empty())
{
one=q.front();
two=q.front();
q.pop();//删队首
one.num*=;
one.step++;
two.num-=;
two.step++;
if(one.num==m)
{
printf("%d\n",one.step);
break;
}
if(two.num==m)
{
printf("%d\n",two.step);
}
if(one.num> && one.num<MAX && !vis[one.num])
{
q.push(one);
vis[one.num]=;
}
if(two.num> && two.num<MAX && !vis[two.num])
{
q.push(two);
vis[two.num]=;
}
}
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
if(n>m)
{
printf("%d\n",n-m);
}
else if(n==m)
printf("0\n");
else
{
bfs(n,m);
}
}
return ;
}

Codeforces Round #295 (Div. 2) B. Two Buttons的更多相关文章

  1. Codeforces Round #295 (Div. 2)B - Two Buttons BFS

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

  2. Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )

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

  3. Codeforces Round #295 (Div. 2) B. Two Buttons 520B

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

  4. 【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons

    题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m: 随后发篇随笔普及下memset函数的初始化问题.自己也是涨了好多姿势. 代码 #include<iostream> ...

  5. Codeforces Round #295 (Div. 2) B. Two Buttons (DP)

    题意:有两个正整数\(n\)和\(m\),每次操作可以使\(n*=2\)或者\(n-=1\),问最少操作多少次使得\(n=m\). 题解:首先,若\(n\ge m\),直接输出\(n-m\),若\(2 ...

  6. Codeforces Round #295 (Div. 2)

    水 A. Pangram /* 水题 */ #include <cstdio> #include <iostream> #include <algorithm> # ...

  7. codeforces 521a//DNA Alignment// Codeforces Round #295(Div. 1)

    题意:如题定义的函数,取最大值的数量有多少? 结论只猜对了一半. 首先,如果只有一个元素结果肯定是1.否则.s串中元素数量分别记为a,t,c,g.设另一个串t中数量为a',t',c',g'.那么,固定 ...

  8. Codeforces Round #295 (Div. 2)C - DNA Alignment 数学题

    C. DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. Codeforces Round #295 (Div. 2)A - Pangram 水题

    A. Pangram time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

随机推荐

  1. bash exec

    1 当exec执行命令时,会为该命令创建shell进程,并且终止老的shell进程的执行,并且保留老的shell进程的进程号 [root@localhost ~]# cat test_exec.sh ...

  2. Mac 下如何安装pip 和xlwt

    sudo easy_install pip pip install xlwt sudo pip install xlwt sudo pip install requests

  3. 简说 call() 、apply() 、bind()

    对于这三个方法,我想一部分人还是比较陌生的. 所以今天来个简单的介绍~ 我们可以将call()和apply()看作是某个对象的方法,通过调用方法的形式来间接调用函数.call()和apply()的第一 ...

  4. Java读取UTF-8格式文件第一行出现乱码——问号“?”及解决 And Java读带有BOM的UTF-8文件乱码原因及解决方法

    測试样例: Java读取UTF-8的txt文件第一行出现乱码"?"及解决 test.txt文件内容: 1 00:00:06,000 --> 00:00:06,010 < ...

  5. linux kfifo移植

    先挖个坑,自己慢慢来填. 参考:http://blog.csdn.net/linyt/article/details/5764312 参考:http://www.cnblogs.com/Anker/p ...

  6. C.Candy

    There are NN children standing in a line. Each child is assigned a rating value. You are giving cand ...

  7. String常量池

    http://developer.51cto.com/art/201106/266454.htm

  8. 树状数组(二叉索引树 BIT Fenwick树) *【一维基础模板】(查询区间和+修改更新)

    刘汝佳:<训练指南>Page(194) #include <stdio.h> #include <string.h> #include <stdlib.h&g ...

  9. box-sizing: border-box;的作用

    box-sizing 属性可以被用来调整这些表现: content-box  是默认值.如果你设置一个元素的宽为100px,那么这个元素的内容区会有100px宽,并且任何边框和内边距的宽度都会被增加到 ...

  10. MySQL数据库设计常犯的错以及对性能的影响

    1.过分的反范式化为表建立太多的列 我们在设计数据库的结构时,比较容易犯的第一个错误就是对表进行了过分的反范式化的设计,这就容易造成了表中的列过多,虽然说Mysql允许为一个表建立很多的列,但是由于M ...