Editorial for COCI '13 Contest 4 #4 Guma
Submitting an official solution before solving the problem yourself is a bannable offence.
To begin with, we will explain the solution of an easier version of this task, which appeared in the original Croatian edition of COCI, called HONI. The difference between this version and COCI's version is that in the easier version, the cuts had to be continuous (not interrupted).
First, we have to notice that, if the vertical part should be split into parts, there must be cuts on it, some of which were prolonged from previous vertical parts and some which have just started. The required solution is the sum of cuts which have just started for all vertical parts: this way, we count each cut exactly once – when we start it.
For each vertical part, we want to know the number of cuts prolonged from previous parts. Let us assume that the previous vertical part was cut into parts and that the current vertical part should be cut into parts. Both numbers are divisible by . This means that the first parts can be seen as large parts by , and the new parts as large parts by . The big parts match and the cuts between them can be prolonged. The number of those cuts is . The remaining cuts should have to be started and added to the final solution.
We can deduce from this example that, in general, the number of cuts that can be prolonged from the part to the part is equal to the greatest common divisor of numbers and minus one. (Let us notice that in the case when the numbers are relatively prime, this is also true because we can prolong cuts.) We leave the proof of this procedure for the reader to implement for practice. The greatest common divisor can be calculated using, for example, Euclid's algorithm because the naive method is too slow for large test data.
The COCI version of this task was significantly more difficult: the cuts could "jump over" vertical parts. In other words, the cuts didn't have to be continuous. What would the minimal possible number of cuts be in this situation?
We are looking for the number of different heights which we can make a cut on. Again, given the current vertical part which should be cut into pieces, we are looking for the number of cuts which can be prolonged from one of the previous vertical parts. The heights we're interested in are .
When these fractions are irreducible, the denominators are divisors of . For each divisor , we come up with fractions , where are relatively prime because the fraction is irreducible. Has the cut appeared before? Yes, if it's equal to for a previous part numbered , which means is a multiple of . If we've marked the appearance of all previous divisors of (using, for example, an array of and ), it is simple to check if has appeared before.
The complete procedure goes like this: for each divisor of the number (we find the divisors with complexity we check whether we've used it before. In other words, if there are cuts . If not, we mark it as used and add all cuts to the final solution, meaning that we are looking for the number of s smaller than and relatively prime with , which is the Euler function of number . Its values can be calculated in advance using formulas for Euler's totient function.
Comments