Transforming Comets
Time limit5sMemory limit512 MB
Given two cyclic sequences of integer points, decide whether one is a rotation, uniform positive scaling, and translation of the other, and report the matching cyclic offset.
- Level
Hard8 of 10
- Topics
- String matching, Geometry, String, Math
- Solved
- No attempts yet
Problem
While traveling from Earth to Krypton, Superman was caught in a wormhole and instantly transported to an unknown location. He remembers the periodic comets he used to watch from Earth, and from his new position he can also see some periodic comets. He would like to use them to get his bearings, but first he must work out which comet is which.
These comets are periodic Gaussian hyper-comets. A periodic Gaussian hyper-comet is a sequence where each is a point with integer coordinates. The comet visits and then ; the sequence is periodic, so after it visits again (indices are taken modulo ). A Gaussian hyper-comet also satisfies for every , and .
Superman was disoriented in both space and time. In space, a comet he once knew may now appear rotated, scaled uniformly by the same positive factor on both axes, and/or translated. In time, the point he remembers as the first point may no longer be listed first.
For example, the right-triangular hyper-comet seen from Earth might now appear as or as . Reversing space or time is not allowed: this comet can never appear as .
Given one Gaussian hyper-comet as seen from Earth and one as seen from Superman's current location, decide whether they could be the same comet.
Input
The first line contains an integer (), the number of test cases.
Each test case begins with an integer (). The next lines each contain two space-separated integers (), the points of the comet seen from Earth. Then more lines follow, each containing two space-separated integers , the points of the comet seen from Superman's current location.
All coordinates are integers between and inclusive. For each comet, for every and .
Output
For each test case, print the smallest positive integer such that the Earth point can correspond to the current point under the disorientation described above (rotation, uniform scaling, translation, and a cyclic time shift, with no reflection and no time reversal). If the two comets cannot be the same, print .