Pasta
InterviewTime limit1sMemory limit128 MB
Count sequences of length N over three kinds where no kind appears three or more times in a row, with some positions fixed, modulo 10000.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
Sanggeun makes pasta for dinner every day. There are three kinds of pasta he can make: tomato sauce, cream sauce, and basil sauce.
He wants to plan the pasta he will eat over the next days. Each day he picks one of the three kinds, but because eating the same pasta too many days in a row gets tiring, he never eats the same kind on three or more consecutive days. In other words, any single kind may be eaten on at most two days in a row.
In addition, the pasta for of the days is fixed in advance.
Given and the fixed-day information, write a program that counts the number of possible plans.
Input
The first line contains two integers and . (, )
Each of the next lines describes one fixed day in the form , meaning the pasta eaten on day is . Here means tomato sauce, means cream sauce, and means basil sauce. All are distinct.
Output
Print the number of possible plans modulo .
Hint
When and the pasta is fixed to tomato on day 1, tomato on day 3, and cream on day 4, the following 6 plans are possible. (Each number is the pasta kind eaten on that day.)
- 1, 2, 1, 2, 1
- 1, 2, 1, 2, 2
- 1, 2, 1, 2, 3
- 1, 3, 1, 2, 1
- 1, 3, 1, 2, 2
- 1, 3, 1, 2, 3