2013 Canadian Computing Competition, Stage 2

Day 2, Problem 2: Transforming Comets

While traveling 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 (p1, p2, ..., pn) where each pi is a 2-dimensional point (xiyi) with integer coordinates. The comet visits some point pi and then it visits point pi+1. The sequence is periodic: after visiting pn the comet visits p1, next (so the indices are interpreted modulo n), Gaussian hyper-comets also have the special property that pi ≠ pi+1 for each i and p1 ≠ pn.

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 any more.

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

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

Each test case begins with an integer n where 2 ≤ n ≤ 500000. The following n lines, for i from 1 to n, each contain a space-separated pair of integers xi  yi. These lines denote the points for a sequence viewed from Earth. Then, similarly, there are n more lines each containing a pair of integers xi′  yi′; 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 30000 (inclusive).

Output

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 x1  y1 could correspond xs′  ys′. 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

All Submissions
Best Solutions


Point Value: 25 (partial)
Time Limit: 10.00s
Memory Limit: 128M
Added: May 19, 2013

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

Comments (Search)

Hi, I'm a little confused about why I'm getting a wrong answer on this problem. I ran it manually with the test data they provided and it works fine - is there something I'm missing?

You're getting a runtime error, presumably because your arrays are of size 50,000 when the bounds are 500,000.

Actually I was talking about the previous submission -- I only tried decreasing the size because I thought I might be allocating too much memory (pretty silly thought in hindsight).

Hmm, well, your outputs are quite different for the cases you don't TLE on.

You're right, I must've been misreading my own program's output. Sorry to trouble you.