#P9830. Minimum Walk Length on a Grid with Forbidden Straight-line Jumps

    ID: 22975 Type: Default 1000ms 256MiB

Minimum Walk Length on a Grid with Forbidden Straight-line Jumps

Minimum Walk Length on a Grid with Forbidden Straight-line Jumps

Consider a grid with n rows and m columns. There are \((n+1)\times(m+1)\) grid points which are the intersections of \((n+1)\) horizontal lines and \((m+1)\) vertical lines. The horizontal lines are numbered from 0 to \(n\) (top to bottom) and the vertical lines from 0 to \(m\) (left to right). The intersection of the \(i^{th}\) horizontal line and the \(j^{th}\) vertical line is named \((i,j)\), where \(0\le i\le n\) and \(0\le j\le m\).

You can travel by a series of walks. When you are at point \((x,y)\), you may choose a destination \((x',y')\) and walk along the line segment between \((x,y)\) and \((x',y')\). However, a walk is forbidden if there is any other grid point (other than the endpoints) lying on the segment. It can be shown that a walk from \((x,y)\) to \((x',y')\) is allowed if and only if \[\gcd(|x'-x|,\,|y'-y|)=1.\] The length of a walk is the Euclidean distance \(\sqrt{(x-x')^2+(y-y')^2}\).

One more restriction applies: the directions of two consecutive walks cannot be identical. Formally, if you walk from \(A=(x_0,y_0)\) to \(B=(x_1,y_1)\) and then from \(B=(x_1,y_1)\) to \(C=(x_2,y_2)\), you must ensure that
\[ (x_0-x_1)(y_1-y_2) \neq (x_1-x_2)(y_0-y_1). \]

Your task is to start at \((0,0)\) and reach \((n,m)\) by a sequence of allowed walks while obeying the turning rule. Find the minimum total length of your walks. The answer will be accepted if it has an absolute or relative error of at most \(10^{-6}\).

inputFormat

The input consists of a single line with two integers n and m representing the number of rows and columns, respectively.

\(1 \le n, m \le 50\)

outputFormat

Output the minimum total length required to travel from \((0,0)\) to \((n,m)\) under the given conditions. Print the answer as a floating-point number. Answers within an absolute or relative error of \(10^{-6}\) will be accepted.

sample

2 2
3.236068