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.

两种操作:

1.乘以2

2.减一

然后直接跑一发bfs就好啦

struct node
{
int x;
int step;
};
int vis[maxn];
int n,m;
int main()
{
cin>>n>>m;
queue<node> q;
q.push({n,});
vis[n]=;
while(!q.empty())
{
node now=q.front();
q.pop();
if(now.x==m)
{
cout<<now.step<<endl;
return ;
}
REP(i,)
{
node next=now;
next.step++;
if(i)
{
next.x*=;
if(vis[next.x]||next.x>maxn-||next.x<)
continue;
vis[next.x]=;
q.push(next);
}
else
{
next.x--;
if(vis[next.x]||next.x>maxn-||next.x<)
continue;
vis[next.x]=;
q.push(next);
}
}
}
}

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

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

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

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

  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 #599 (Div. 2) D. 0-1 MST(bfs+set)

    Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers ...

  7. Codeforces Round #295 (Div. 2)

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

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

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

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

随机推荐

  1. souce insight出错 There was an error opening project

    souce insight出错 There was an error opening project: "...": Options->Preferences->Fol ...

  2. fcntl函数的用法总结

    fcntl系统调用可以用来对已打开的文件描述符进行各种控制操作以改变已打开文件的的各种属性 函数原型:   #include<unistd.h> #include<fcntl.h&g ...

  3. awk的常用内置函数的使用【转】

    手把手教你在linux下熟悉使用awk的指令结构 (15) 大家好,今天和大家说一下awk吧.反正正则 早晚也要和大家说,不如一点一点和大家先交代清楚了,省得以后和大家说的时候,大家有懵的感觉... ...

  4. SPOJ 16549 - QTREE6 - Query on a tree VI 「一种维护树上颜色连通块的操作」

    题意 有操作 $0$ $u$:询问有多少个节点 $v$ 满足路径 $u$ 到 $v$ 上所有节点(包括)都拥有相同的颜色$1$ $u$:翻转 $u$ 的颜色 题解 直接用一个 $LCT$ 去暴力删边连 ...

  5. ThinkPHP文件目录说明

    1.ThinkPHP文件包下目录结构说明 2.ThinkPHP文件目录下文件说明 3.Conf目录下 4.Library目录

  6. python图片处理(一)

    在matlab中有相应的图像进行二值化处理,并且标记连通区域 L = bwlabel(BW,n) 返回一个和BW大小相同的L矩阵,包含了标记连BW中每个连通区域的类别标签,标签的值是1.2.num(连 ...

  7. linux c获取本地时间

    在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef _TM_DEFINED struct tm { int tm_sec; /* 秒–取值区间 ...

  8. google浏览器打开新的标签页显示http://www.google.com.hk/url?sa=p&hl=zh-CN&……

    chrome的版本:51.0.2704.106 m使用该版本的chrome后,每次打开新标签页,都会提示“无法访问此网站”.并自动跳转到一个地址“http://www.google.com.hk/ur ...

  9. 【PAT】1018 锤子剪刀布 (20)(20 分)

    1018 锤子剪刀布 (20)(20 分) 大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算 ...

  10. C语言:奇偶归一猜想

    1.奇偶归一猜想——求多少步归一.(10分) 题目内容: 奇偶归一猜想——对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1. 如n = 11,得 ...