Redaksiya
Problem Author: | Illia Fursov, Maksym Shvedchenko |
Prepared by: | Illia Shevchenko, Illia Fursov |
Editorial by: | Illia Shevchenko |
Group 1:
In this group, you can check all subsegments. Calculate the GCD and the arithmetic mean and check if they are equal.
Solution complexity:
Group 2:
In this group, you can iterate through all subsegments, but do it a bit smarter.
for (int l = 0 ; l < n ; l++) for (int r = l ; r < n ; r++)
By iterating through the segments in this way, we can maintain the GCD and the sum.
Solution complexity: where is the maximal possible element from the array .
Group 3: (no restrictions)
You can notice the following fact: if a subarray is good, then all numbers in it are equal.
Proof: Let's take the minimum element, denote it as m, its GCD as g, and the arithmetic mean as s. Since m is divisible by g, then , and at the same time , then , and at the same time , then , which means that all numbers are equal.
If we use this fact, then we need to find the maximum length subsegment of equal numbers and the number of such subsegments.
This problem can be solved as follows: divide the array into blocks of equal numbers, the maximum subsegment will be the maximum length of a block, and the count can be calculated for each block separately using the formula , where k is the size of the block.
Solution complexity: