Editorial for An Animal Contest 6 P5 - Rock Painting


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: rickyqin005

Subtask 1 Hint

What is the probability of rock i being the first or last rock with some colour c?

Subtask 1

Let's create an array a of length N to represent the initial state of the rocks. If ai=0, the ith rock has not been coloured. Otherwise, ai will denote the colour of the ith rock.

We also define f(i) to be the probability of rock i being the rightmost rock and g(i) to be the probability of rock i being the leftmost rock with some colour c. We have:

f(i)={(Cki)(C1)xiCxi+1if ai=00if ai0 and there exists some j>i with ai=aj(C1C)xiotherwise

where xi is the number of non-coloured rocks to the right of rock i and ki is the number of distinct non-zero ai to the right of rock i. Calculating g(i) is the same as f(i) but in reverse.

Therefore, the final answer to this problem is:

i=1Nif(i)i=1Nig(i)

Time Complexity: O(N+log(109+7))

Subtask 2

For this subtask, we can generalize the definition of f(i) to be f(i)=(C1C)Ni. Using r=C1C, we have:

Sf(i)=i=1NirNi=1rN1+2rN2++(N1)r1+Nr0

Using some algebra, we have:

rSf(i)=1rN+2rN1++(N1)r2+Nr1rSf(i)Sf(i)=rN+rN1++r2+r1Nr0Sf(i)=r1+r2++rN1+rNNr1

Substituting the sum of geometric series formula into the equation, we get:

Sf(i)=r(rN1)r1Nr1=r(rN1)(r1)2Nr1

Time Complexity: O(log(109+7))

Subtask 3

This subtask is an extension of subtasks 1 and 2 and is left as an exercise for the reader.

Time Complexity: O(Mlog(109+7))


Comments

There are no comments at the moment.