Editorial for DMOPC '20 Contest 3 P3 - A Ring of Buckets


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.

Author: Riolku

Subtask 1

For the first subtask, some very naive brute force will work. Each bucket will be poured into at most M times, and there are N of them, so there are NM possible combinations, each taking N operations to check.

Time Complexity: O(QNM+1)

Subtask 2

For the second subtask, note that if we have three consecutive buckets, and the first is poured into a times and the second b times, then we must pour into the third one c=MbKa times. We can thus brute force all possibilities for the first two values of the answer array, and use this formula to get the remaining ones. If any values are negative or larger than M, we don't have a solution.

Time Complexity: O(QNM2)

Subtask 3

Note first that if NM is not divisible by K+2, we do not have a solution.

Furthermore, note that if M is divisible by K+2, a possible solution is MK+2, repeated N times.

For the remainder of this editorial, let Ai be our answer array, where A1 is the amount we pour into the first bucket, and Ai is the amount we pour into the ith bucket.

In fact, our buckets are in a circle, so let's make A infinite in both directions, where we have Ai=Ai+N.

Now, let a,b,c,d be arbitrary consecutive elements of a.

Note that we must have a+bK+c=M, as mentioned in subtask 2.

For this subtask, if K=1, we have:

a+b+c=M b+c+d=M

Or:

ad=0 a=d

So every third element is equal.

It should be clear that if every third element is equal, unless N is divisible by 3, Ai=Ai+1 (that is, all elements are equal).

If N is not divisible by 3, then we must have a solution with all identical elements, or no solution at all.

If N is divisible by 3, the following pattern yields the lexicographically smallest solution:

0,0,M,0,0,M,0,

And this is all that is required for the K=1 subtask.

Time Complexity: O(QN)

Subtask 4

For this subtask, we consider K=2. We now have:

a+2b+c=M b+2c+d=M

So we have:

a+bcd=0 a+b=c+d

If N is odd, we have:

Ai+Ai+1=Ai+2+Ai+3=Ai+N1+Ai+N=Ai1+Ai

Or:

Ai+1=Ai1

So every other element is equal. In fact, if N is odd:

Ai=Ai+2=Ai+N+1=Ai+1

So every element is equal. This case proceeds similarly to the K=1 case mentioned above.

However, if N is even, the case proceeds differently. Let X=a+b. Then:

i=1NAi=A1+A2++AN=N2×X

Also, let Y=b+c. Then:

i=1NAi=A1+A2++AN=A2+A3++AN+1=N2×Y

So X=Y, or a+b=b+c, or a=c.

So every other element is equal. Then:

a+2b+c=M 2a+2b=M

So M must be even in order for a solution to exist.

In fact, our lexicographically smallest pattern is:

0,M2,0,M2,0,

This concludes the K=2 case.

Time Complexity: O(QN)

Subtask 5

For this subtask, we have K3. We have:

a+bK+c=M b+cK+d=M

Or:

ab+(bc)K+(cd)=0 (bc)(K1)=da

Without loss of generality, assume bc, since if b<c, we can consider the array backward.

Then, since K>2:

1<K1 (bc)(K1)(bc)=(da)

We have equality only when b=c. Assume that b>c. Then:

(bc)<(da) a+b<c+d

This means that:

Ai+Ai+1<Ai+2+Ai+3<Ai+4+Ai+5<

However, this means:

Ai+Ai+1<Ai+2N+Ai+2N+1=Ai+Ai+1

Which is a contradiction.

Thus, we must have b=c, which tells us that every element of the array must be equal.

In fact, when K>2, we have a solution if and only if M is divisible by K+2.

Time Complexity: O(QN)

Subtask 6

To finish off the solution, we need help from a little math.

Suppose our answer contains a repeating cycle C of length L. Note that for this problem, L3. Let Y=NL, and note that it must be an integer. Then we need to compute:

i=1NAi×Bi1=i=1Yj=1LCj×BL(i1)+(j1) i=1Y(BL)i1j=1Cj×Bj1

Note that this inner sum can be precomputed. Assign it the value Z:

i=1YZ×(BL)i1

This should look very similar to the geometric sequence formula, and we can apply it here:

Z×(BL)Y1BL1

Note that since we are computing our answer modulo 109+7, instead of dividing, we need to multiply by the multiplicative modular inverse.

Time Complexity: O(QlogN)

Final Thoughts

Although this problem has an (admittedly exhausting) proof, this problem should teach you to try solutions. Many who solved this problem attempted submitting MK+2 N times and got AC on the K3 case, and proceeded from there. The other way to approach this is bottom up; look at the subtasks and solve them individually, and then combine them together.

This problem isn't meant for you to sit and stare at for hours. Don't be afraid to guess.


Comments

There are no comments at the moment.