Editorial for COCI '12 Contest 4 #5 Dlakavac
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us present infected people in one day as an object which has a defined operator . We want the operator to determine the infected people for the next day, based on two of the past days. Let us denote the first day with . We denote the second and every day as . Then we have:
We can think of a day as an array of s and s such that there is in the position if the person was infected that day. The operator can then be implemented with a complexity of .
Let us take a look at the solution for day 3:
Here we add an operator of exponentiation as a shortened writing of consecutive multiplication. Let us take a look at the properties of this operator.
This is obviously right because we have defined exponentiation as shorthand for consecutive multiplication.
This relation is also true for our operator . It is now possible, using logarithmic exponentiation, to implement the operation with a complexity of where is a day we are interested in.
The algorithm:
power(B, k) =
if k is 0,
return {0, 1, 0, 0, ...}
if k is even,
half = power(B, k / 2)
return half * half
if k is odd,
return power(B, k - 1) * B
If becomes , we must return the object which will be the neutral element for the operator . In our case it is .
Comments