#P3012. [USACO11FEB] Cowlphabet G

[USACO11FEB] Cowlphabet G

Description

Like all bovines, Farmer John's cows speak the peculiar "Cow" language. As in many languages, each word in this language is a sequence of uppercase and lowercase letters (A-Z and a-z). A word is valid if and only if each ordered pair of adjacent letters in the word is a valid pair.

Farmer John, ever worried that his cows are plotting against him, recently tried to eavesdrop on their conversation. He overheard one word before the cows noticed his presence. The Cow language is spoken so quickly, and its sounds are so strange, that all Farmer John was able to perceive was the total number of uppercase letters, UU (1U2501 \leq U \leq 250), and the total number of lowercase letters, LL (1L2501 \leq L \leq 250), in the word.

Farmer John knows all PP (1P2001 \leq P \leq 200) valid ordered pairs of adjacent letters. He wishes to know how many different valid words are consistent with his limited data. However, since this number may be very large, he only needs the value modulo 9765432197654321.

Input Format

  • Line 1: Three space-separated integers: UU, LL, and PP.
  • Lines 2 to P+1P+1: Two letters AiA_i and BiB_i (each of which may be uppercase or lowercase), representing one valid ordered pair of adjacent letters in Cow, meaning AiA_i can be followed by BiB_i. No ordered pair appears more than once.

Output Format

  • Line 1: A single integer, the number of valid words consistent with Farmer John's data, modulo 9765432197654321.
2 2 7 
AB 
ab 
BA 
ba 
Aa 
Bb 
bB 

7 

Hint

The word Farmer John overheard had 2 uppercase and 2 lowercase letters. The valid pairs of adjacent letters are AB, ab, BA, ba, Aa, Bb and bB.

The possible words are: AabB ABba abBA BAab BbBb bBAa bBbB

Translated by ChatGPT 5