Official

G - Doubles Editorial by en_translator


Let \(S=\sum_i K_i\).

First, prepare the following associative array for each die \(i\):

\(\mathrm{count}_i[X]=\)the number of faces on die \(i\) with \(X\) written on them.

This can be done in a total of \(O(S\log S)\) time over all \(i\).

How can we find the probability that two dice \(i\) and \(j\) show the same value? It is sufficient to find for each face value \(X\) of die \(i\), how many faces with \(X\) die \(j\) has. Thus, the associative array \(\mathrm{count}_j\) enables us to find it in \(O(K_i \log K_j)\) time.

Thus, by enumerating all pairs of dice, the answer can be found in a total of \(\sum_{i,j} O(K_i \log K_j)=O(NS\log S)\) time.

Evaluation $K_i \log K_j \leq K_i \log S$, so $\sum_{i,j} O(K_i \log K_j)\\ \subset \sum_{i,j}O(K_i \log S)\\ \subset \sum_{j}O(S \log S)\\ \subset O(NS \log S)$

posted:
last update: