Problem D: USACO 2017 US Open Contest, Bronze Problem 1. The Lost Cow

Problem D: USACO 2017 US Open Contest, Bronze Problem 1. The Lost Cow

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 131  Solved: 13
[Submit] [Status] [Web Board] [Creator:]

Description

10points
English:
Farmer John has lost his prize cow Bessie, and he needs to find her!

Fortunately, there is only one long path running across the farm, and Farmer John knows that Bessie has to be at some location on this path. If we think of the path as a number line, then Farmer John is currently at position xx and Bessie is currently at position yy (unknown to Farmer John). If Farmer John only knew where Bessie was located, he could walk directly to her, traveling a distance of |x−y||x−y|. Unfortunately, it is dark outside and Farmer John can't see anything. The only way he can find Bessie is to walk back and forth until he eventually reaches her position.

Trying to figure out the best strategy for walking back and forth in his search, Farmer John consults the computer science research literature and is somewhat amused to find that this exact problem has not only been studied by computer scientists in the past, but that it is actually called the "Lost Cow Problem" (this is actually true!).

The recommended solution for Farmer John to find Bessie is to move to position x+1x+1, then reverse direction and move to position x−2x−2, then to position x+4x+4, and so on, in a "zig zag" pattern, each step moving twice as far from his initial starting position as before. As he has read during his study of algorithms for solving the lost cow problem, this approach guarantees that he will at worst travel 9 times the direct distance |x−y||x−y| between himself and Bessie before he finds her (this is also true, and the factor of 9 is actually the smallest such worst case guarantee any strategy can achieve).

Farmer John is curious to verify this result. Given xx and yy, please compute the total distance he will travel according to the zig-zag search strategy above until he finds Bessie.



chinese:

农夫约翰失去了他的牛贝西(Bessie),他需要找到她!

幸运的是,整个农场只有一条漫长的道路,而农夫约翰知道贝肯定在这条道路上的某个位置。如果我们将路径视为数字线,那么农夫约翰当前位于x位置,而Bessie当前处于y位置(农夫约翰不知道y的值)。如果农夫约翰只知道贝西的位置,那么他可以直接向她走去,前进| x-y |步。不幸的是,外面一片漆黑,农夫约翰什么也看不到。他找到贝茜的唯一方法就是来回走动,直到他最终到达她的位置。

为了找出在搜索中来回走动的最佳策略,农夫约翰查阅了计算机科学研究文献,觉得很有趣,发现这个确切的问题不仅是过去计算机科学家研究的,而且是实际上被称为“迷失的牛”(这确实是事实!)。

建议的解决方案是:农民约翰移动到位置x + 1,然后反向并移动到位置x−2,然后移动到位置x + 4,依此类推,以“ 之字形”模式进行每一步,每一步距离他的初始起始位置两倍的距离。正如他在研究的那样,这种方法保证了他在最坏的情况下将走的距离是| x-y |的9倍。在他和贝西找到他之前找到了他(这也是事实,并且9实际上是任何策略都能保证的最小的最坏情况)。

农夫约翰很好奇,想要验证这个结果。给定x和y,请根据上面的之字形搜索,计算出他找到贝西将要走的总距离。


Input

The single line of input contains two distinct space-separated integers xx and yy. Both are in the range 0…1,0000…1,000.



整数x和y以空格分,范围都在0…1,000之间


Output



Print one line of output, containing the distance Farmer John will travel to reach Bessie.



给一行输出,其中包含农夫约翰到达贝西的距离。







Sample Input Copy

3 6

Sample Output Copy

9