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. gpio口、内核定时器使用

    /*申请gpio*/ int gpio_request(unsigned gpio, const char *label); /*设置gpio为输入状态,即设置如(GPH0CON)*/ int gpi ...

  2. j-linkV8固件更新(任意版本)

    在使用j-link v8调试程序时,容易出现固件丢失或出错的情况,导致电脑不能识别,j-link上面的灯不亮.我今天刚刚遇到了这个情况,于是就拆开外壳,在网上搜索资料,发现刷固件相关的还真多,但是有一 ...

  3. shell服务管理->

    nginx.php等服务管理练习脚本 ->nginx的启动状态 root@jumpserver- day02]# cat nginx_web.sh #!/bin/bash source /etc ...

  4. 决策树和adaboost

    前面:好老的东西啊,啊啊啊啊啊啊啊啊啊 来源于统计学习方法: 信息增益: 其中 信息增益率: 基尼指数: 取gini最小的 先剪枝——在构造过程中,当某个节点满足剪枝条件,则直接停止此分支的构造. 后 ...

  5. P1183 多边形的面积

    一道睡论数论题 其实是AC300祭才做的水题 题意: 很直白的的题意啊,就是求任意一个多边形的面积 所以我们来安利一下一个求多边形面积的数学通式: 给定多边形的顶点坐标(有序),让你来求这个多边形的面 ...

  6. C++静态成员的应用

    当在类外部定义静态成员时,不能重复使用static关键字 静态成员函数不包含this指针(无论是显示还是隐式使用) 静态成员可以通过类对象进行访问,也可以通过类进行访问 静态成员不是由构造函数初始化的 ...

  7. jenkins Error performing command: git ls-remote -h

    Jenkins新建项目中源码管理使用Git时遇到如下问题: Failed to connect to repository : Error performing command: git ls-rem ...

  8. 13 在 O(1) 时间内删除链表节点

    删除链表的一个结点,用下一个结点覆盖掉要删除的结点,再释放掉要删结点的下一个结点的内存 Java: public ListNode deleteNode(ListNode head, ListNode ...

  9. USACO 6.2 Shaping Regions

    Shaping Regions N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white s ...

  10. USACO 5.3 Milk Measuring

    Milk MeasuringHal Burch Farmer John must measure Q (1 <= Q <= 20,000) quarts of his finest mil ...