Editorial for COCI '09 Contest 4 #6 Palacinke


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.

First, let us presume that Ana doesn't need to buy all ingredients and set up difference equations that will help us calculate the number of tours. Let X_i[t] be the number of tours ending in crossroad i at time t. To set up the equations, we examine all roads leading to i. For each road (edge j, i), on the right side of the equation we add the case of Ana not entering the shop, x_j[t-1], and the case where she did, x_j[t-2]. For each crossroad, this gives equations in the form X_i[t] = x_a[t-1] + x_a[t-2] + x_b[t-1] + x_b[t-2] + \dots.

Let S[t] be the number of tours ending in vertex 1 with time t. We have S[t] = S[t-1] + X_1[t]. The total number of tours ending in vertex 1 with time T is S[T]. Using binary exponentiation and matrix multiplication we can obtain S[T]. However, S[T] now also contains invalid paths. To get the number of valid paths, we use the inclusion/exclusion formula. First, we subtract all paths that do not contain one ingredient, then add all that do not contain two, then subtract all that do not contain three etc. The number of paths that do not contain one or more ingredients can be easily obtained by omitting x_j[t-2] in all equations where the shop sells the wanted ingredient.


Comments

There are no comments at the moment.