Editorial for COCI '10 Contest 4 #4 Prosjek


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

First, let us determine the minimum number of integers needed to obtain the average of P.

Let S = \{n_1, n_2, n_3, n_4, n_5\} be the solution to the problem and let n = n_1 + n_2 + n_3 + n_4 + n_5. The following holds:

\displaystyle \begin{align*}
(n_1 + 2 \times n_2 + 3 \times n_3 + 4 \times n_4 + 5 \times n_5) / n &= P \\
(n_1 + 2 \times n_2 + 3 \times n_3 + 4 \times n_4 + 5 \times n_5) &= n \times P
\end{align*}

It can be inferred that the right side of the equation is an integer. P can be expressed in the form of a reduced fraction a/b, where a and b are relatively prime.

\displaystyle (n_1 + 2 \times n_2 + 3 \times n_3 + 4 \times n_4 + 5 \times n_5) = n \times (a/b)

The equation holds if and only if n is a multiple of b. Therefore, the minimum n is not less than b. We now show how to obtain a solution where n = b.

Observe that with n integers we can obtain any sum in the interval [n, 5n]. Moreover, a \le 5b. Considering these facts, the number of each integer can be found with a greedy algorithm. Starting from the greatest one, we pick each integer maximum number of times, such that it is possible that the remaining sum can be obtained using the remaining number of integers.


Comments

There are no comments at the moment.