?
题面:
?
?
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputVasya 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?
InputThe first and the only line of the input contains two distinct integers n and m (1?≤?n,?m?≤?104), separated by a space .
OutputPrint 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) input4 6output
2input
10 1output
9Note
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.
?
?
解题:
法一:
一开始看到这题,感觉似曾相识。感觉可能可以写,最初的想法是这样的,n>=m,直接输出n-m。当n
?
代码:
?
#includeusing namespace std; int main() { int n,m,cnt=0,t,ans=0,last; cin>>n>>m; if(n>=m)cout< n) { if(m%2==0) { ans++; m/=2; } else { m=(m+1)/2; ans+=2; } } ans+=(n-m); cout< ?
法二:
看了一篇题解,说是spfa爆搜,顿时给跪了。感觉好奇怪,我也没看具体题解,想了一下,是不是建一张图,10000个数,减法由x到x-1建立一条权值为1的边,乘法由n到2*n建立一条权值为1的边,最后只要搜索n到m的最短路就好了。这是我自己的想法,感觉很巧妙,但是不会超复杂度吗?然而边不是矩阵存储的,因为10000个数,最多不会超过20000条边。