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. Python 006- python socket编程详细介绍

    转自https://blog.csdn.net/rebelqsp/article/details/22109925 Python 提供了两个基本的 socket 模块. 第一个是 Socket,它提供 ...

  2. InspectIT_EUM 实现原理概述

    在Git上查看 InspectIT 实现原理概述: 实现原理详解:  1.jsAgent如何注入到浏览器 通过ASM框架修改HttpService.service()方法,加入相关逻辑,对每一个Htt ...

  3. jQuery 工具函数

    jQuery工具函数 一.$.browser对象属性 属性列表 说明 webkit webkit相关浏览器则返回true,否则返回false,如google,傲游. mozilla mozilla相关 ...

  4. 20170225-第一件事:SAP模块清单

    第一件事:SAP模块清单 AM 资产会计 资产会计BC SAP Netweaver SAP NetweaverBW 业务信息仓库 业务信息仓库CA 常规跨应用程序 常规跨越应用程序CO 控制 控制 C ...

  5. Flume 和 kafka的区别和对比

    定义: Flume:是Cloudera提供的一个分布式的海量日志采集.聚合和传输的系统: Kafka:是一种高吞吐量的分布式发布订阅消息系统: 各特点: 场景: Flume主要是和HDFS\HBase ...

  6. 序列化FastReport,重要提示少走弯路 good

    原本在开发一个报表插件,因为需要远程传输,因此需要序列化报表,序列化FastReport有两种方式, 1.仅序列化数据,由客户端接受到数据,并呈现报表,这种方式需要在客户端存储报表格式文件xxx.Fr ...

  7. centreon问题总结

    1.SNMP TABLE ERROR : Requested table is empty or does not exist 这是SNMP的服务端查询客服端失败,失败的原理是权限不足 解决办法: v ...

  8. I2C测试【转】

    本文转载自: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

  9. hadoop datanode启动失败(All directories in dfs.data.dir are invalid)

    由于hadoop节点的磁盘满了,导致节点死掉,今天对其进行扩容.首先,将原节点的数据拷贝到目标节点下,从而避免数据的丢失,但是在执行hadoop_daemon.sh start datanode后没有 ...

  10. 【旧文章搬运】360安全卫士HookPort.sys完美逆向

    原文发表于百度空间,2009-11-08 这是第一次逆向一个企业级安全产品的核心代码,并完美替换原驱动正常工作============================================= ...