Word Chain
Time limit1sMemory limit1024 MB
Given a dictionary, for each starting word find the win probabilities for each bear, the chance Tokki wins by an endless game, and the expected word count, all mod 998244353.
- Level
Medium7 of 10
- Topics
- Graph, Probability, Matrix, Math
- Solved
- No attempts yet
Problem
The White Bear and the Black Bear received a dictionary with words from Tokki. The two bears like to play word chain. In word chain, the two bears take turns saying words from the dictionary. A bear may only say a word that starts with the last letter of the word the other bear said just before. If at least one valid word exists, the bear picks one of them at random, and every valid word has the same probability of being picked. If no valid word exists, the game ends. The bear who said the last word wins. A word in the dictionary can be said many times, so the game may never end. Each time a bear says a word, Tokki decides whether the game can end. If the game cannot end no matter which words the two bears say afterward, Tokki ends the game and wins.
For each integer from to , let be the probability that the White Bear wins when the game starts with the White Bear saying the -th word of the dictionary, the probability that the Black Bear wins, the probability that Tokki wins, and the expected number of words said by the two bears until the game ends. The White Bear, the Black Bear, and Tokki compare the values computed mathematically with the values obtained by actually playing the game. Find for every integer from to .
Input
The first line contains the number of words in the dictionary. From the second line onward, the -th of the next lines contains the -th word of the dictionary. Every word consists of uppercase Roman letters.
Output
Print lines. On line , print , each taken modulo and separated by spaces. Strictly speaking, write each rational number as , where , , and are integers. Print the unique integer with that satisfies .