Editorial for CCC '21 S5 - Math Homework
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
The only numbers needed are and . For every operation with , fill with . After all the operations, check if the GCDs are correct.
Time Complexity:
Subtask 2
Consider a single requirement , we can reframe this requirement as the following two restrictions:
- Every single number from index to to be divisible to
- The GCD of the subarray is minimized
For any index , to make sure it satisfies the first restriction of all requirements that pass over it, it must be some multiple of the LCM of all of those requirements. Additionally, as we also want to minimize the GCD of the subarray specified in each requirement (while also satisfying the divisibility restriction), it's always optimal to take the LCM.
Thus, for each value in our final array, we collect all the requirements that pass over it, and just set it to the LCM of the values of those requirements. Then, we just check that our answer is valid by making sure the GCD restriction on each requirement is satisfied.
Time Complexity:
Subtask 3
There are many ways to optimize the subtask solution for full points, but this solution will not involve any data structures beyond difference array because throwing segment trees at the problem is boring.
To compute which requirements pass over any given index, notice that , so we can just use difference arrays to keep track of which requirements pass over any given index.
Checking that the GCD restriction of each requirement is satisfied is a bit more complicated. In this case, we'll treat them as range GCD queries on our final array and we first group the queries by their right endpoint. We then process them offline in increasing order of right endpoint. At each right endpoint from , we consider the GCD of all subarrays that end at that endpoint, and observe that as we move from the shortest to longest subarray, the GCD will change at most times. Thus, we can just maintain these GCD changes with a vector and answer all queries ending at that right endpoint with binary search.
Time Complexity:
Comments