Twirling Towards Freedom (Large)
Time limit5sMemory limit512 MB
Each minute you stay put or rotate 90 degrees clockwise around a star, and must report the largest squared distance from the origin reachable in M minutes.
- Level
Hard9 of 10
- Topics
- Geometry, Number theory, BFS, Greedy
- Solved
- No attempts yet
Problem
Your spaceship starts at the origin of a two-dimensional plane. The galaxy holds stars, and star is at .
Each minute you do exactly one of two things: pick a star and rotate the ship 90 degrees clockwise around that star, or stay where you are. You may pick the same star again later. Rotating a point by 90 degrees clockwise around moves it to .
You have minutes, and you want to finish as far from the origin as possible. A rotation sends integer coordinates to integer coordinates, so the squared distance between the origin and the final position is always an integer. Report that integer.

The picture shows the first three rotations of one possible path. The yellow dots are stars and the purple dots are positions of the ship. This path is not necessarily part of an optimal solution.
Input
The first line has the number of test cases . The test cases follow. The first line of a test case has , the second line has , and each of the next lines has two integers and , the position of one star.
Limits
- No two stars are at the same location.
- A star may be at the origin.
Output
For each test case, print one line in the form Case #x: S, where is the test case number starting from 1 and is the square of the largest distance from the origin that the ship can reach within minutes. fits in a signed 64-bit integer.