#P9827. Sum of Shortest Path Distances in the Sky Garden Maze

    ID: 22973 Type: Default 1000ms 256MiB

Sum of Shortest Path Distances in the Sky Garden Maze

Sum of Shortest Path Distances in the Sky Garden Maze

Prof. Du and Prof. Pang plan to build a sky garden near Allin city. The garden will include a plant maze formed by a set of circular and straight roads. In the blueprint, there are n circles (each circle i has radius \(i\)) centered at the origin \((0,0)\), and m straight lines (each passing through \((0,0)\)). Each circle is divided into \(2m\) equal arcs by these lines, and every intersection between a circle and a line gives two distinct points. In addition, any two different straight roads meet at the origin.

Let \(Q\) be the set of all \(n+m\) roads and let \(P\) be the set of all distinct intersections of any two different roads in \(Q\). Thus, \(|P|=2nm+1\) (the extra one corresponds to the origin).

For any two different points \(a, b\) in \(P\), define \(dis(\{a,b\})\) as the shortest distance to walk from \(a\) to \(b\) along the roads. Note that on a circle of radius \(i\), the distance along a circular arc between consecutive intersection points is \(\frac{\pi i}{m}\). On any straight road (which contains the intersections with the circles and the origin), the distance between the intersection points corresponding to circles with radii \(r\) and \(r+1\) is exactly 1.

Any point (other than the origin) can be represented in polar coordinates as \((r, \theta)\) where \(r\in\{1,2,\dots,n\}\) and \(\theta\) is one of the \(2m\) equally spaced angles: \(0,\,\frac{\pi}{m},\,\frac{2\pi}{m},\,\dots,\,\frac{(2m-1)\pi}{m}\). For two such points \((r_1,\theta_1)\) and \((r_2,\theta_2)\) (with \(r_1,r_2>0\)), one may travel radially and along a circular arc. In fact, if we choose an intermediate circle of radius \(t\) (with \(0\le t\le \min(r_1,r_2)\)), the travel distance is \[ r_1+r_2-2t+t\cdot\alpha, \quad\text{where } \alpha=\frac{\pi}{m}\cdot L, \] with \(L=\min(|k_1-k_2|,2m-|k_1-k_2|)\) when \(\theta_1=k_1\frac{\pi}{m}\) and \(\theta_2=k_2\frac{\pi}{m}\). Optimizing over \(t\) shows that when \(\frac{\pi L}{m}2\) it is optimal to choose \(t=0\) so that the distance becomes \(r_1+r_2\). (For points on the same ray, \(L=0\), and the distance is simply \(|r_1-r_2|\).)

Your task is to calculate the sum of \(dis(\{a,b\})\) for every unordered pair \(\{a,b\}\subseteq P\).

Input Format: Two integers \(n\) and \(m\).

Output Format: Output one real number representing the sum of shortest distances. Your 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\) separated by a space.

outputFormat

Output a single real number: the sum of \(dis(\{a,b\})\) for all unordered pairs \(\{a,b\}\subseteq P\). The answer should be printed with at least 6 digits after the decimal point.

sample

1 1
4.000000