Cowlphabet
Time limit1sMemory limit128 MB
Count valid words over 52 letters with exactly U uppercase and L lowercase letters, given the allowed adjacent letter pairs, modulo 97654321.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics, Graph, Matrix
- Solved
- No attempts yet
Problem
Like all bovines, Farmer John's cows speak the peculiar 'Cow' language. As in many languages, each word in Cow is a sequence of uppercase and lowercase letters (A–Z and a–z). A word is valid if and only if every ordered pair of adjacent letters (the earlier letter followed by the later one) is a valid pair.
Ever worried that his cows are plotting against him, Farmer John recently tried to eavesdrop on their conversation and overheard a single word before the cows noticed him. Cow is spoken so quickly, and its sounds are so strange, that all he could perceive was the total number of uppercase letters () and the total number of lowercase letters () in that word.
Farmer John knows all () valid ordered pairs of adjacent letters in Cow. He wants to know how many different valid words are consistent with this limited data. Because the count can be very large, report it modulo .
Input
- Line 1: Three space-separated integers , , and .
- Lines 2 through : Two letters (each may be uppercase or lowercase) describing one valid ordered pair of adjacent letters — the first letter may be immediately followed by the second.
Output
- Line 1: A single integer — the number of valid words consistent with Farmer John's data, taken modulo .
Notes
- A word is an ordered sequence of letters, and the same letter may appear multiple times.
- and are the totals over the whole word; the uppercase and lowercase letters may sit in any positions.
- Two words are different whenever their letter sequences differ.
- Since and , every word has length at least 2, so each letter it contains belongs to at least one adjacent pair.