Editorial for COCI '17 Contest 2 #6 Garaža
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's observe any array of natural numbers and the GCD (greatest common divisor) of each prefix in the array, . For example:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
216 | 144 | 96 | 96 | 120 | 560 | 9 | 8 | |
216 | 72 | 24 | 24 | 24 | 8 | 1 | 1 |
Let's observe the two adjacent values and . The following properties hold:
- is divisible by .
- . In the case that , then it holds , which is why the value will change times at most.
We conclude that array can be written in a different way, so that we group all the prefixes with the same GCD, as an array of at most pairs of numbers where represents one of the values , and represent the number of appearances of that value in array . For example, the array from the above example can be written as: . Analogously, we can write the GCD of all suffixes of array .
We will solve this task using a segment tree where we will store the following data for each interval:
- GCD of all prefixes (in the above mentioned form, so as an array of pairs ),
- GCD of all suffixes (also in the above mentioned form), and
- The number of interesting contiguous subsequences in that interval, .
In order to apply the tournament tree data structure on this task, we need to define how we can determine new data for the union of intervals, based on the aforementioned data for two adjacent intervals. Let there be two adjacent intervals and . Based on the GCD of all prefixes and , let's try to determine the GCD of all prefixes (where represents the union of intervals and ). Array is calculated in the following way:
- for
- , for
We can notice that in some cases it will be possible to additionally group some elements in the array by values , and that the length of the total array is never going to be larger than . The complexity of determining this array is , and analogously, we can determine the array that represents the GCD of all suffixes of interval .
All that is left to determine is how we can calculate the number of interesting subsequences in the interval (). These values will be equal to the sum of values , and the number of interesting subsequences that are in both intervals and . Let correspond to the interval , and to the interval . Let's observe the smallest from the interval , for which a exists from the interval such that it holds that the subsequence is interesting, and the subsequence is not. The GCD of the subsequence can be calculated as the largest common divisor of the GCD-suffix until position in array and the GCD-prefix until position in array . However, we should notice one more thing: as we increase , so will the value either remain the same or increase by a positive number. Therefore, the number of interesting subsequences contained in both intervals and can be calculated using the two-pointer technique, that will iterate over array and that will iterate over array . The complexity of this approach is , i.e. .
The total complexity of the joining is , so the total complexity of the algorithm is .
Comments