#P2436. Contestant Classification
Contestant Classification
Contestant Classification
In this problem, a cyclic pattern is determined by two positive integers \(A\) and \(B\). In each cycle of length \(A+B\), the first \(A\) contestants are classified as Shenben (神犇) and the next \(B\) contestants are classified as Konjac (蒟蒻).
You are given two sets of contestant positions. The first set contains the positions that must be classified as Shenben and the second set contains the positions that must be classified as Konjac. The classification for a contestant at position \(x\) is determined as follows: let \(r = x \bmod (A+B)\) where we treat \(r = A+B\) if \(x \bmod (A+B) = 0\). Then, the contestant is Shenben if \(1 \le r \le A\) and Konjac if \(A+1 \le r \le A+B\>.
Your task is to determine if there exists a pair of positive integers \(A\) and \(B\) leading to a cyclic pattern that matches all the given assignments. If such a pair exists, output any valid pair \((A,B)\); otherwise, output -1.
Note: If the input does not allow any valid solution, simply output -1.
inputFormat
The first line contains two integers \(n\) and \(m\) — the number of positions that must be Shenben and the number of positions that must be Konjac, respectively.
The second line contains \(n\) distinct positive integers representing the positions that must be classified as Shenben.
The third line contains \(m\) distinct positive integers representing the positions that must be classified as Konjac.
outputFormat
If there exists a pair \((A,B)\) that meets the condition, output two positive integers \(A\) and \(B\) separated by a space. Otherwise, output -1.
sample
2 3
1 6
3 4 10
1 4