We need to parse problem: We have a constellation of m stars. In the photo there are n stars, which includes exactly one occurrence of that constellation (same shape, orientation, size). Means there is a translation (dx, dy) such that translating each of the m constellation points by (dx, dy) yields points that are all present in the photo. However photo may have extra stars (n >= m). The constellation is given, the photo points are given. Need to find (dx, dy) that maps all constellation points to points in the photo.
Constraints: m up to 200, n up to 1000. Coordinates up to 1e6. Number of datasets up to 5.
Goal: output translation amounts.
Assumptions: exactly one such translation exists. So we can compute.
Simplest approach: For each pair of stars in constellation, compute vector differences. Also for each pair in photo, compute differences. Then find a translation that aligns constellation to photo. Since only translation, not rotation or scaling, we need to match all points.
We can pick a reference point from constellation (e.g., first star). Then for each candidate point in photo that could be the image of that reference, we test if translating the constellation by dx = candidate.x - ref.x, dy = candidate.y - ref.y results in all points present in the photo (i.e., each translated constellation point is in the set of photo points). Since there may be extra stars, it's okay as long as all are present.
The naive: iterate over each photo point (n) and test O(m log n) if all translated points are in a hash set. n up to 1000, m up to 200 => at most 1000*200=200k checks, which is fine. However, there may be false positives? But exactly one solution exists, so we can find it. However, need to ensure that we don't accidentally find a translation that matches but also includes extra? The problem says exactly one figure with same shape, orientation, and size as the constellation; that means there is exactly one translation such that all m constellation points map to points present. It does not require that the mapping is unique? But it says exactly one figure, implying uniqueness. So we can just find the translation that works.
Edge cases: multiple photo points might happen to align for some but not all points. So we must verify fully. That is O(n*m). With n=1000, m=200, 200k operations plus set lookup O(1), fine.
But we should also consider that n may be 1000, m 200, so 200k is trivial. Also dataset up to 5, so overall fine.
But wait: there is a nuance: In the input examples, they have two datasets. The second dataset seems to give a translation that is large negative/positive. So our approach works.
But could there be a scenario where m > n? Not possible because the constellation is included; it says photo contains exactly one figure; so at least m <= n? Could be m>n? But they said photo contains exactly one figure with same shape, orientation, and size; if m>n, then number of stars in constellation > number of stars in photo, cannot be included. So m <= n. So okay.
Thus algorithm:
For each dataset:
- read m, if m ==0 break.
- read m lines for constellation stars, store as list of (x,y).
- read n, then n lines for photo stars, store in list. Also create a set of (x,y) for quick lookup.
- For each photo point (px, py):
- compute dx = px - ref_x, dy = py - ref_y, where ref is constellation[0].
- For each star (cx, cy) in constellation:
- check if (cx + dx, cy + dy) is in set. If any missing, break; else continue.
- If all present, then (dx, dy) is the answer. Print and break.
Since there is exactly one, we break after first match.
This is straightforward.
But need to consider precision? All integers, fine.
Potential issue: Could there be multiple matches? But problem says exactly one figure, so only one translation. However, if constellation points are not distinct? But they are distinct.
Edge: If constellation points are all same? No, positions are all different.
Potential edge: If constellation includes duplicate? Not allowed.
Now complexity O(n*m) per dataset, max 200k, fine.
But wait: Could the naive approach be too slow if we use Python and not optimized? 200k * maybe constant for set lookup is fine.
But there is an alternative method: using hashing of vectors to reduce. Not needed.
Thus we can implement.
However, careful: Input format: There is m, then m lines, then n, then n lines, and next dataset. The last dataset ends with m=0. There may be blank lines? Usually not.
But the problem statement mentions multiple datasets and each dataset ends with m=0? Actually input description: The first line of the input contains m. Then m lines. Then m+2 line contains n. Then more lines. Then when m is 0, it indicates end of input. So we read until m==0.
Thus while True: read m, if m==0 break; then read m pairs, then read n, then read n pairs. Good.
Now test on given example:
Example 1:
5
8 5
6 4
4 3
7 10
0 10
10
10 5
2 7
9 7
8 10
10 2
1 2
8 1
6 7
6 0
0 9
We choose ref (8,5). Try each photo point.
Let's manually compute: The answer is 2 -3. So dx=2, dy=-3. Then ref maps to (10,2). Check if (10,2) in photo? Yes. All others map: (6,4) -> (8,1)? (6+2=8,4-3=1) yes; (4,3)->(6,0) yes; (7,10)->(9,7) yes; (0,10)->(2,7) yes. So works.
Our algorithm would find that.
Second example: answer -384281 179674. So fine.
Thus implementation is easy.
But careful: The set of photo points must contain exactly the translated constellation; extra stars are allowed. So just all must be present.
Implementation details: Since coordinates up to 1e6, we can store tuples in a set.
Potential optimization: Pre-sort photo points? Not needed.
Now let's consider potential pitfalls: The constellation could be large; but we just test each candidate. However, there might be a situation where the reference point's image could be any of n points. So worst-case we test n * m = 200k. That's fine.
But could there be case where m=200, n=1000, and many candidates pass partial matches but fail at last, making runtime maybe a bit more but still okay.
Now we might think about early break: we can precompute constellation points relative to reference, maybe store as list of (dx_i, dy_i) where dx_i = cx_i - ref_x, dy_i = cy_i - ref_y. Then for each candidate (px, py), we compute required target positions: (px+dx_i, py+dy_i). Then check if each is in set. So we can compute deltas once.
Implementation:
deltas = [(x - ref_x, y - ref_y) for x,y in constellation]
Then for each photo point (px,py):
dx = px - ref_x // Actually we don't need dx separately if we directly compute target positions as (px + delta_x, py + delta_y). But that's same.
good = True
for dx_i, dy_i in deltas:
if (px + dx_i, py + dy_i) not in photo_set: good=False; break
if good: answer = (px - ref_x, py - ref_y); break
But careful: For delta_i = (cx_i - ref_x, cy_i - ref_y). Then for a candidate point that corresponds to ref mapping to px,py, the mapping of other constellation points is (cx_i + (px - ref_x), cy_i + (py - ref_y)) = (ref_x + delta_i + (px - ref_x), ref_y + delta_i_y + (py - ref_y)) = (px + delta_i_x, py + delta_i_y). So correct.
Thus we can compute deltas once.
Now we need to output the translation amounts: dx = px - ref_x, dy = py - ref_y.
Thus simple.
Now edge: The reference point itself (cx0) maps to px,py; but note that we used ref_x = cx0, ref_y = cy0. So deltas for ref are (0,0). So the first check always passes if we use the same point? Actually we only check for all deltas including (0,0), which is (px,py) itself; obviously photo_set contains (px,py) because we are iterating over photo points. So it's fine.
But one nuance: What if there are duplicate points between constellation and photo? Actually all points distinct, but if translation is zero, then (px,py) = (ref_x, ref_y) if ref appears in photo. That's possible. So we need to consider.
Now answer is unique, so break after first.
Now input reading: Since number of datasets unknown, we need to read until m=0. The input may contain multiple datasets. Use sys.stdin.read() or read line by line.
Given typical Japanese problems, the input ends with 0 as m. So we implement.
Now we need to handle large input? Not huge; max 5 datasets, each up to 1000 lines, so total lines < 5000, fine.
Now let's produce solution.
But wait: There's a nuance: The constellation may have m=1. Then any photo point would match if the translation maps the single star to any photo point? But problem says "the photo always contains exactly one figure with the same shape, orientation, and size as the constellation". For m=1, any single point is a shape; but then there could be multiple translations? Because if there is exactly one figure, that means there is exactly one translation such that the constellation maps to points in photo. But with m=1, any photo point yields a translation; but the condition "exactly one figure" might enforce that there is exactly one occurrence of the constellation. However, with m=1, the shape is just a point. The photo contains many points, many translations could map the single point to a photo point? But problem statement: "The photo always contains exactly one figure with the same shape, orientation, and size as the constellation you are looking for." So if the constellation is a single star, then the figure in the photo would be a single star that matches? But there could be many stars; how can exactly one figure exist? Because the figure is the set of stars; if we look for a single star, any star in the photo would be a match, but then there would be many figures, each consisting of a single star, contradicting exactly one. So probably m >= 2? Not explicitly said but typical problems m might be >= 1? But m is given as 1 ≤ m ≤ 200. So m could be 1. But then how to have exactly one figure? Possibly they assume that the constellation shape is non-degenerate? Or maybe it's allowed and then we need to find translation such that the point maps to a star in the photo, but if there are many stars, many translations exist; but the problem says exactly one figure, so in such a case there must be only one star? But n could be >1, then there would be multiple matches. So probably test data avoid m=1 when n>1. Or maybe they guarantee unique translation. For m=1, any photo point yields a translation, but exactly one translation? That would require that only one photo point is present? But n can be up to 1000. Maybe the problem implies that the constellation shape includes at least 2 stars? Or maybe we need to find the translation that maps the constellation to the photo; if multiple translations possible, perhaps the correct one is the one that maps all points? But for m=1, all translations mapping to any photo point are valid; but the problem says exactly one figure exists, so the constellation is unique; thus for m=1, there must be exactly one star in the photo that matches? But that would mean n=1? Possibly they avoid this case. We'll just implement as is; if m=1, then we iterate over photo points and as soon as we find a point, we output dx,dy; but there could be multiple, but problem says exactly one, so we can break after first; but if there are multiple, we would output the first found which might not be correct. However, given constraints, maybe they avoid m=1. But just in case, we could ensure uniqueness: For m=1, the translation is determined by the difference between the constellation point and the unique photo point that matches? But there would be many if n>1. However, problem statement says "The photo always contains exactly one figure with the same shape, orientation, and size as the constellation." So if constellation has one point, the "figure" is a single star. The photo contains many stars, but if there is exactly one figure, that would mean there is exactly one set of stars that matches the shape? But a single star shape can be considered as a set of one star; any single star is that shape. So they'd all be figures. So unless the photo also includes the exact same star (the same coordinates) which is impossible because they are in different coordinate systems (constellation coordinates and photo coordinates), you can't have multiple figures? Actually consider: The constellation is a set of points; a figure in the photo is a set of points that, after translation, matches exactly the constellation set. So for a single-point constellation, a figure would be any single point in the photo because you can translate the constellation point to that photo point. So if the photo has n stars, there are n different figures. So to have exactly one figure, n must be 1. So maybe they avoid m=1 unless n=1. Since constraints allow n≥1, but it's possible m=1, n=1. That is trivial. So we can handle.
Thus our algorithm works: for m=1, we set deltas empty? Actually deltas would be just (0,0) from ref to itself. So for each photo point, we check if (px+0, py+0) is in set (which it is, since we are iterating photo points that are in the set). So all photo points would pass. So we would break at the first photo point and output translation. That would be ambiguous if n>1, but if problem guarantee exactly one figure, it must be that only one translation works, implying that there is exactly one photo point that can be the image? Actually if there are multiple points, then multiple translations would map the single point to a point present. But the condition that exactly one figure exists would be violated. So we can assume n=m? Not necessarily, but for m>=2, extra stars can exist. For m=1, n must be 1 to have unique figure. So it's safe.
Thus proceed.
Now, but there is a nuance: The constellation's points may be given in arbitrary order, and the photo may have extra points. Our algorithm checks that all translated constellation points are present, regardless of order. That's correct.
Now, we should consider time complexity: set lookup O(1) average.
Now, is there any need for more clever approach to avoid false positives? Our algorithm will check all m points, so false positives are eliminated.
Thus implement.
Now output format: for each dataset, print two integers separated by space: dx dy.
Now the given examples: The first output "2 -3", second "-384281 179674". So negative numbers possible.
Now test with example.
Potential pitfalls: Input reading may include extra spaces or newline. Use sys.stdin.readline.
Now coding: We'll define solve().
Pseudo:
import sys
def solve():
import sys
lines = sys.stdin.read().strip().splitlines()
# But we need to parse sequentially. Alternatively, use iterator.
# Simpler: use sys.stdin to read line by line.
# Let's implement using sys.stdin.
data = sys.stdin
out_lines = []
while True:
line = data.readline()
if not line:
break
m = int(line.strip())
if m == 0:
break
constellation = []
for _ in range(m):
x,y = map(int, data.readline().split())
constellation.append((x,y))
n = int(data.readline())
photo = []
for _ in range(n):
x,y = map(int, data.readline().split())
photo.append((x,y))
# Build set
photo_set = set(photo)
# Choose reference
ref_x, ref_y = constellation[0]
deltas = [(cx - ref_x, cy - ref_y) for cx, cy in constellation]
found = False
for px, py in photo:
# Check all deltas
ok = True
for dx, dy in deltas:
tx = px + dx
ty = py + dy
if (tx, ty) not in photo_set:
ok = False
break
if ok:
dx_ans = px - ref_x
dy_ans = py - ref_y
out_lines.append(f"{dx_ans} {dy_ans}")
found = True
break
# According to problem, we must find one; no need to handle not found.
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()
But we need to ensure reading lines correctly: There might be empty lines? Possibly not. But using data.readline() may return empty string at EOF, but we break on m==0 earlier. Usually input ends with line containing "0". So after reading that line, we break, and then no more datasets.
Edge: After reading the last dataset, there might be no newline after "0"? But typical.
Now let's test with given input.
We'll simulate.
Input:
5
8 5
6 4
4 3
7 10
0 10
10
10 5
2 7
9 7
8 10
10 2
1 2
8 1
6 7
6 0
0 9
5
904207 809784
845370 244806
499091 59863
638406 182509
435076 362268
10
757559 866424
114810 239537
519926 989458
461089 424480
674361 448440
81851 150384
459107 795405
299682 6700
254125 362183
50795 541942
0
Our code: first m=5, read 5 constellation points, then read n=10, read 10 photo points. Then compute. Should output "2 -3". Then next dataset: m=5 again? Wait reading: After first dataset, there is a line "5" which is m for second dataset. Then read constellation points, then n=10, etc. Then m=0 line. So output second line "-384281 179674". That matches.
But is the answer for second dataset given? The example output is 2 -3 and -384281 179674. So we get that.
Now we should test manually with second dataset? Possibly we can try compute approximate: constellation points:
(904207,809784)
(845370,244806)
(499091,59863)
(638406,182509)
(435076,362268)
photo points: list as above.
We can compute deltas relative to first point (904207,809784). Deltas:
(0,0)
(-58837, -565978)? Actually compute differences: (845370-904207 = -58837, 244806-809784 = -564978? Wait 244806-809784 = -564978. So (-58837, -564978). Next (499091-904207 = -405116, 59863-809784 = -749921) etc. Then we test each photo point. The answer is dx = px - 904207, dy = py - 809784. The given answer: dx = -384281, dy = 179674. That means px = 904207 - 384281 = 519926, py = 809784 + 179674 = 989458. Indeed 519926 989458 is in photo list (third photo point). So that's the ref mapping. So our algorithm will catch that.
Thus solution works.
But we need to consider possibility of multiple candidates: The problem says exactly one figure, so only one translation works. So break after first.
Now one more nuance: Could there be duplicate points between photo points and constellation? Not important.
Now let's think about edge case: What if the reference point's image is not in photo? But it must be, because for the translation to exist, the reference point must map to some photo point. Since we iterate over all photo points, we will find it.
Now is it guaranteed that the first photo point we test is not the correct one? But eventually we will test all.
Now complexity: O(n*m) per dataset. With n=1000, m=200, 200k, fine.
Now one could also use a more efficient approach: compute hashing of point patterns, but not needed.
Thus final answer.
But wait: Could there be a scenario where the constellation has many points and the photo has many, but multiple candidates pass partial checks, leading to many checks? 200k worst-case, that's fine.
Now potential improvement: pre-sort photo points and binary search? Not needed; set is hash.
Now implement in Python.
Now confirm that we need to output each answer on its own line.
Now done.
But the problem might be from an old contest, perhaps we need to handle up to 5 datasets and coordinates up to 1e6, but time limit is generous. So we are fine.
Now also note: The input might contain extra spaces; using map(int, line.split()) handles.
Now final code.
However, some caution: The set of photo points might be large (1000). But we also need to consider that we are iterating over photo points and for each we check m points; total 200k operations. That's fine.
But there is also possibility of using the vector approach: compute all differences between constellation points and pick two to define translation more robust? Not needed.
Now produce final answer.
But there is a nuance: The constellation shape and orientation must be same. Since only translation allowed, it's just mapping.
Thus solution.
Now check potential pitfalls: The input may contain leading/trailing whitespace; we use strip.
Now final code.
We'll include function solve() and main guard.
Now I'll write solution accordingly.
Now we might also need to handle reading when there are blank lines. Could be some datasets separated by blank lines? Not mentioned. But we can skip empty lines if encountered.
We can modify reading loop: while True: line = data.readline(); if not line: break; line=line.strip(); if line=='': continue; m=int(line); if m==0: break; then read m lines similarly, but ensure we skip empty lines if any.
But given typical problems, no blank lines. But to be safe, we can handle.
Simpler: Use sys.stdin.read().split() to get tokens and parse sequentially. That's robust.
Let's do token parsing.
Implementation using token list:
import sys
def solve():
tokens = sys.stdin.read().strip().split()
it = iter(tokens)
out = []
while True:
try:
m = int(next(it))
except StopIteration:
break
if m == 0:
break
constellation = []
for _ in range(m):
x = int(next(it)); y = int(next(it))
constellation.append((x,y))
n = int(next(it))
photo = []
for _ in range(n):
x = int(next(it)); y = int(next(it))
photo.append((x,y))
photo_set = set(photo)
ref_x, ref_y = constellation[0]
deltas = [(cx - ref_x, cy - ref_y) for cx, cy in constellation]
found = False
for px, py in photo:
ok = True
for dx, dy in deltas:
if (px + dx, py + dy) not in photo_set:
ok = False
break
if ok:
out.append(f"{px - ref_x} {py - ref_y}")
found = True
break
# should be found
sys.stdout.write("\n".join(out))
if name == "main":
solve()
This is simpler and robust against blank lines.
Now test with example.
Works.
Now check for corner: When m=0, break and stop reading more tokens? The token list after reading 0 might still have nothing else, but that's fine.
Now final answer.
I think that's it.
But wait: There is one more nuance: The problem says "The photo always contains exactly one figure with the same shape, orientation, and size as the constellation you are looking for." So translation should map all constellation points to some stars in the photo. However, there might be also extra stars that happen to coincide with some translated constellation points beyond the m points? That doesn't matter. So our method is correct.
Now let's consider if the constellation could be rotated or scaled? No, only translation.
Thus final.
Now we produce final answer.
But before final, we should double-check with more test.
Suppose constellation points: (0,0), (1,0), (0,1). Photo points: (2,2), (3,2), (2,3), (5,5). Then translation (2,2) works. Our algorithm: ref (0,0) deltas: (0,0),(1,0),(0,1). For each photo point: (2,2) -> targets: (2,2),(3,2),(2,3) all present -> output (2,2). Good.
Now what if there is another photo point that accidentally matches some of them but not all? For instance, if photo point (5,5) alone might match if we consider ref (0,0) mapping to (5,5) would require (6,5) and (5,6) which are not present, so fails.
Thus unique.
Now also note: There might be multiple translations if the constellation is symmetric? For example, constellation points (0,0),(1,0),(0,1) is not symmetric? But if it's symmetric under some translation? Actually symmetry: Two different translations could produce the same set? That would require that there exist two distinct translation vectors such that the translated sets are both subsets of the photo set. That would imply that the photo contains at least two copies of the constellation shape. But problem says exactly one figure, so that won't happen.
But if the constellation points are all the same? Not allowed because all distinct.
So fine.
Thus solution stands.
Now let's discuss potential pitfalls with large coordinates: using set of tuples of ints, fine.
Now final output.
Now we should provide explanation of approach.
Thus answer: Use a reference star and check all possible translations.
Now produce final answer with code.
But wait: The problem statement mentions "The number of datasets does not exceed 5." So multiple.
Now final.
Now to be thorough, we could think about performance: In worst case, we may have 5 datasets, each 200*1000=200k checks, 1M checks, trivial.
Thus final.
Now I'll write final solution.
```python
import sys
def solve() -> None:
tokens = sys.stdin.read().strip().split()
it = iter(tokens)
out_lines = []
while True:
try:
m = int(next(it))
except StopIteration:
break
if m == 0:
break
# read constellation
constellation = []
for _ in range(m):
x = int(next(it))
y = int(next(it))
constellation.append((x, y))
n = int(next(it))
photo = []
for _ in range(n):
x = int(next(it))
y = int(next(it))
photo.append((x, y))
photo_set = set(photo)
# use the first star as reference
ref_x, ref_y = constellation[0]
# pre‑compute relative vectors from the reference star
deltas = [(x - ref_x, y - ref_y) for (x, y) in constellation]
found = False
for px, py in photo:
ok = True
for dx, dy in deltas:
if (px + dx, py + dy) not in photo_set:
ok = False
break
if ok:
dx_ans = px - ref_x
dy_ans = py - ref_y
out_lines.append(f"{dx_ans} {dy_ans}")
found = True
break
# According to the problem statement a solution always exists
# (and is unique), so we don't need to handle 'not found'.
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()