Problem D
Dice Distributions

To play Maessiw, each player needs two dice. The two dice used by a single player are not required to have the same number of sides. However, the probability distribution of the sum of those two dice must be identical for both players.
We say that two probability distributions are identical if they assign the same probability to every possible outcome.
We model an $N$-sided die as a discrete uniform distribution over its faces: each face is labeled with an integer (labels need not be distinct or ordered), and each face occurs with probability $1/N$.
Currently, Mikael already has two dice: one with $A$ sides and one with $B$ sides. Harry has only one die with $C$ sides. To play the game, Harry needs a second die such that the distribution of the sum of his two dice is identical to the distribution of the sum of Mikael’s dice.
Additionally, Harry would like his new die to have as few sides as possible, to make the die and the game as simple as possible.
Can you help Harry design his second die?
Input
The first line of input contains the integers $A$, $B$, $C$, ($1 \leq A,B,C \leq 2\cdot 10^5$), where $A$ and $B$ are the number of sides of Mikael’s two dice, and $C$ is the number of Harry’s first die.
The second line contains $A$ integers $a_i$ ($1 \leq a_i \leq 2\cdot 10^5$), the number written on the $i$-th side of Mikael’s first die.
The third line contains $B$ integers $b_i$ ($1 \leq b_i \leq 2\cdot 10^5$), the number written on the $i$-th side of Mikael’s second die.
The fourth line contains $C$ integers $c_i$ ($1 \leq c_i \leq 2\cdot 10^5$), the number written on the $i$-th side of Harry’s first die.
Output
First print an integer $D$ on a single line, the minimum number of sides that Harry’s second die could have. On the second line, print $D$ integers, $d_1, d_2, \dots , d_D$ representing the numbers written on each of the sides in non-decreasing order.
Note that the numbers $d_i$ can exceed $2\cdot 10^5$.
It is guaranteed that a solution exists for the given input, and it can be proven that the answer is unique. It is also guaranteed that $D \leq 2\cdot 10^5$.
Scoring
Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.
Let $D$ be the minimum number of sides that Harry’s second die could have.
Group |
Points |
Constraints |
$1$ |
$20$ |
$A = B = C = D \leq 2000$, $a_i,b_i,c_i \leq 2000$ |
$2$ |
$30$ |
$A, B, C, D \leq 2000$, $a_i,b_i,c_i \leq 2000$ |
$3$ |
$30$ |
$A = B = C = D$ |
$4$ |
$20$ |
No additional constraints. |
Explanation of Samples
In sample 4, Mikael has dice with $3$ sides and $4$ sides, and Harry’s first die has $6$ sides. The smallest die possible that can be given to Harry such that Harry’s and Mikael’s probability distributions are identical, is a die with $1$ side (Do NOT question how physically to create a die with a single side).
We can verify that the probability distributions of their sums are identical.
Mikael has the following 12 outcomes, resulting in a probability of $\frac{8}{12} = \frac{2}{3}$ for obtaining 8, and $\frac{4}{12} = \frac{1}{3}$ for obtaining 9.
$b_1 = 4$ |
$b_2 = 4$ |
$b_3 = 4$ |
$b_4 = 4$ |
|
$a_1 = 4$ |
8 |
8 |
8 |
8 |
$a_2 = 4$ |
8 |
8 |
8 |
8 |
$a_3 = 5$ |
9 |
9 |
9 |
9 |
Harry has the following 6 outcomes, resulting in a probability of $\frac{4}{6} = \frac{2}{3}$ for obtaining 8, and $\frac{2}{6} = \frac{1}{3}$ for obtaining 9.
$d_1 = 5$ |
|
$c_1 = 3$ |
8 |
$c_2 = 3$ |
8 |
$c_3 = 3$ |
8 |
$c_4 = 3$ |
8 |
$c_5 = 4$ |
9 |
$c_6 = 4$ |
9 |
Thus, Mikael and Harry have identical probability distributions.
Sample Input 1 | Sample Output 1 |
---|---|
6 6 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 |
6 1 2 3 4 5 6 |
Sample Input 2 | Sample Output 2 |
---|---|
9 9 9 1 1 1 1 2 2 2 2 3 5 4 4 3 3 3 2 2 1 1 1 2 3 2 3 2 3 4 |
9 1 1 2 2 2 3 3 3 4 |
Sample Input 3 | Sample Output 3 |
---|---|
9 9 18 1 1 2 3 2 3 2 3 4 1 1 2 2 2 3 3 3 4 1 1 1 1 2 2 2 2 3 1 1 1 1 2 2 2 2 3 |
9 1 2 2 3 3 3 4 4 5 |
Sample Input 4 | Sample Output 4 |
---|---|
3 4 6 4 4 5 4 4 4 4 3 3 3 3 4 4 |
1 5 |