Teleportation
Time limit2sMemory limit512 MB
Count length-L button sequences that move from ship S to ship T, where each ship has four outgoing transitions, modulo 10^4.
- Level
Medium6 of 10
- Topics
- Matrix, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
The Galactic Confederation installed a new teleport system on its spaceships. Every ship has one teleport booth, and inside the booth there is a panel with four buttons. Each button carries a different letter, A, B, C or D, together with the number of the destination ship the user is transported to, instantly, when that button is pressed. Confederation ships are identified by the integers 1 to .
To use the system the user buys a ticket for each trip, and one trip is one button press. The panel has few buttons compared with the number of ships in the Confederation, so travelling from a ship to another ship may require a ticket for trips.
Look at the ships in the figure below. In the teleport booth of ship 3, pressing button B transports the user to ship 2. With trips left on the ticket, pressing button B again transports the user to ship 1.

Given the departure ship , the arrival ship and the number of trips written on the ticket, count the distinct button sequences that take the user from ship to ship . A sequence has exactly presses. For the ships in the figure, four distinct sequences with take a user from ship to ship : CD, DA, AB and BB.
Input
The first line contains two integers and , the number of ships and the number of trips on the ticket. (, )
The second line contains two integers and , the departure ship and the arrival ship. ()
Each of the next lines describes the panel of one teleport booth. The -th of those lines contains four integers , , and , the numbers written on the four buttons of ship . ()
Output
Print one line with a single integer. Let be the number of distinct button sequences of length that take the user from ship to ship . Print modulo .