CCO '13 P5 - Transforming Comets

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 4.5s
Memory limit: 1G

Problem types
Canadian Computing Competition: 2013 Stage 2, Day 2, Problem 2

While travelling from Earth to Krypton, Superman was caught in a wormhole and transported instantly to some unknown location. Superman remembers seeing periodic comets from Earth, and he can see some periodic comets from his current location. He would like to use these comets to find his bearings, but first he has to match up which one is which.

The specific comets Superman sees are periodic Gaussian hyper-comets. A periodic Gaussian hyper-comet is a sequence (p_1, p_2, \dots, p_n) where each p_i is a 2-dimensional point (x_i, y_i) with integer coordinates. The comet visits some point p_i and then it visits point p_{i+1}. The sequence is periodic: after visiting p_n the comet visits p_1 next (so the indices are interpreted modulo n). Gaussian hyper-comets also have the special property that p_i \ne p_{i+1} for each i and p_1 \ne p_n.

Superman was disoriented in both space and time. In terms of space this means a comet that he saw before might now have its whole set of points rotated, both axes scaled by the same positive factor, and/or translated. Furthermore, since he was disoriented in time, the first point of a comet he used to know might not be the first point anymore.

For example, the right-triangular hyper-comet ((0, 0), (1, 0), (0, 1)) from Earth might look like ((40, 40), (20, 20), (60, 20)) or ((20, 20), (60, 20), (40, 40)). Note that reversing time or space is not an allowed transformation, e.g. it's not possible for this hyper-comet to appear as ((0, 1), (1, 0), (0, 0)).

Your goal is: given a periodic sequence of points corresponding to a Gaussian hyper-comet Superman saw from Earth, and a Gaussian hyper-comet Superman sees now, determine if they could be the same comet.

Input Specification

The first line contains 1 \le t \le 10, the number of test cases to follow.

Each test case begins with an integer n where 2 \le n \le 500\,000. The following n lines, for i from 1 to n, each contain a space-separated pair of integers x_i y_i. These lines denote the points for a sequence viewed from Earth. Then, similarly, there are n more lines each containing a pair of integers x_i^{\prime} y_i^{\prime}; these lines denote the points for a sequence viewed Superman's current location.

It is guaranteed that all of the coordinates are integers between 0 and 30\,000 (inclusive).

Output Specification

For each test case, if the two sequences could represent the same hyper-comet under this disorientation process, print out the smallest positive integer s so that x_1 y_1 could correspond to x_s^{\prime} y_s^{\prime}. If there is no such s, print 0.

Sample Input

3
3
0 0
1 0
0 1
20 20
60 20
40 40
4
0 0
1 1
0 0
1 1
30 30
19 23
30 30
19 23
4
0 0
1 0
1 1
0 1
0 0
2 0
2 1
0 1

Sample Output

3
1
0

Comments

There are no comments at the moment.