Möbius Strip
Time limit1sMemory limit128 MB
Given m and n, compute the average graph distance over all ordered square pairs on the m by 2n Mobius grid.
- Level
Hard9 of 10
- Topics
- Math, Combinatorics, Graph, Geometry
- Solved
- No attempts yet
Problem
A Möbius strip is a surface with a single side and a single boundary edge. You can build one from a strip of paper: give one of the short ends a half-turn twist, then glue the two short ends together.
Start with a rectangular strip of paper whose two faces are each covered by a square grid of size with ( squares across the width, squares along the length). Twisting one short end by a half turn and gluing the short ends together joins the two faces into one continuous surface, so the resulting Möbius strip has size : squares across the width and squares all the way around the loop. For example, a Möbius strip of size has and (that is, ).
Two squares are adjacent when they share a full side, except along the single boundary edge of the strip. An ant stands on one square and moves only from a square to an adjacent one; it can never step across the boundary edge. The distance between two squares is the least number of moves the ant needs to get from one to the other (equivalently, the number of squares it enters, not counting the square it started on), so two adjacent squares are at distance .
The average distance of a Möbius strip of size is the average of the distances taken over every ordered pair of squares, including each square paired with itself (which contributes distance ). For example, a Möbius strip of size has average distance .
Given the size of a Möbius strip, output its average distance.
Input
The first line contains the number of test cases . Each of the following lines contains two integers and (), describing a Möbius strip of size .
Output
For each test case, print one line with the average distance of that Möbius strip, rounded to the nearest tenth so that it shows exactly one digit after the decimal point.