Editorial for COCI '13 Contest 1 #5 Organizator


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.

Notice that the solution equals the size of a team multiplied by the number of participating clubs, while a club participates if it has a member count divisible by the team size.

The easiest method to count the participating clubs for some team size S is iterating over all club sizes and counting clubs that have a size divisible by S. The complexity of this algorithm is \mathcal O(N \times maxS), where maxS is the maximum club size, because the team size can be any value between 1 and maxS, and counting for each team size takes \mathcal O(N).

We need a faster method of counting the competing clubs. Since club sizes are limited to 2 million, we can use an array a of size maxS to store, for each possible size, the number of clubs with that size.

In order to count the participating clubs for some team size d, we need to compute the sum: a[d] + a[2d] + a[3d] + \dots, which requires \frac{maxS}{d} steps.

Trying out all possibilities for d from 1 to maxS requires \frac{maxS}{1} + \frac{maxS}{2} + \dots + \frac{maxS}{maxS-1} + 1 steps, which approximately equals maxS \ln maxS. Thus, the complexity of this algorithm is \mathcal O(maxS \log maxS), which is sufficient to score all points.


Comments

There are no comments at the moment.