Editorial for COCI '09 Contest 4 #6 Palacinke
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 be the number of tours ending in crossroad at time . To set up the equations, we examine all roads leading to . For each road (edge ), on the right side of the equation we add the case of Ana not entering the shop, , and the case where she did, . For each crossroad, this gives equations in the form .
Let be the number of tours ending in vertex with time . We have . The total number of tours ending in vertex with time is . Using binary exponentiation and matrix multiplication we can obtain . However, 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 in all equations where the shop sells the wanted ingredient.
Comments