Mirror Trap
Time limit3sMemory limit512 MB
For each box [-x,x]x[-y,y]x[-z,z], find the maximum Manhattan distance a laser at the origin can travel before returning to the origin, avoiding edges and vertices.
- Level
Hard9 of 10
- Topics
- Math, Number theory, Geometry, Combinatorics
- Solved
- No attempts yet
Problem
A mirror trap is a rectangular box whose six inner faces are all mirrors, with the reflecting sides facing the interior. Its dimensions are , where are positive integers. Exactly at the centre sits a point laser whose size is negligible. Introduce a Cartesian coordinate system whose axes are parallel to the edges of the box, with the laser at the origin; the box then occupies .
The laser may be aimed at any integer lattice point inside the box (points on the mirror surfaces included, with the single exception of the origin ). The fired beam reflects off the mirrors, and its travelled distance is measured in the Manhattan (city) metric: the sum of the distances travelled parallel to each of the three axes.
Choose the aiming direction so that the beam satisfies all of the following, and report the maximum possible total travelled distance:
- it is reflected by the mirrors (not necessarily by all of them),
- it never crosses an edge (where two faces meet) or a vertex of the box,
- it returns to the laser (possibly from a different direction).
Edges and vertices do not reflect the beam.
Note: several aim points may attain the maximum, so instead of an aim point this problem asks for the maximum total travelled distance itself (a single integer).
Input
A single input describes several mirror traps. The first line contains the number of traps (). Each of the next lines describes one trap as three space-separated integers (); that trap has dimensions .
Output
Print exactly lines. The -th line contains a single integer: the maximum total distance the beam can travel in the -th trap while satisfying all of the conditions above.