Find the shortest integer direction so two movable perpendicular lines split the 4N points into four groups of N each.
Medium6GeometrySortingBrute forceNo attempts yetTime limit5sMemory limit512 MBKing Tyrone conquered the nation of Carrania, and his four sons immediately started to argue about how to split the land. The argument is about the gold mines. There are 4N of them, and each son must end up with exactly N.
The king draws an X on the map. The X is a pair of perpendicular straight lines that cuts the nation into four parts, one part per son. No gold mine may lie on a border, and each of the four parts must contain exactly N gold mines.
The direction of the X is written as an integer vector (dx,dy). One border is a line with direction vector (dx,dy), and the other border is a line with direction vector (−dy,dx). The minister may put the intersection point of the two borders anywhere.
A vector (dx,dy) is good if the two perpendicular lines with these directions can be placed so that no gold mine lies on a border and each of the four parts holds exactly N gold mines.
Turning a direction vector by 90 degrees or flipping its sign gives the same X, so only vectors with dx≥1, −dx<dy≤dx and gcd(dx,∣dy∣)=1 are considered. Every X that has an integer direction vector has exactly one representative in this range.
Find the good vector with the smallest dx2+dy2. If several good vectors share that value, take the one with the smallest dy.
The first line contains the number of test cases T. Each test case starts with a line containing N, the number of gold mines each son must get. The next 4N lines each contain two integers xi and yi, the coordinates of one gold mine.
Limits
For each test case, print one line in the form Case #x: dx dy. Here x is the test case number starting from 1, and dx and dy are the good vector with the smallest dx2+dy2, with ties broken by the smallest dy.