Piano
Time limit1sMemory limit64 MB
Given N equally likely piano tones, find the expected number of presses until a fixed M-tone sequence appears, for every prefix of it.
- Level
Hard8 of 10
- Topics
- String matching, Dynamic programming, Probability, Prefix sum
- Solved
- No attempts yet
Problem
Young Alisa likes to play the piano using only one finger. Unfortunately, Alisa never learned to play the piano, so her playing is entirely random. More precisely, every time she chooses a tone to play, she chooses it independently of all previous tones, and each of the tones has the same probability.
Her good friend Mirta wants to listen to a composition made of consecutive tones. Since Alisa plays randomly, Mirta does not know how long she will have to wait to hear exactly these tones in a row. Help Mirta find the expected number of key presses until she hears her wanted array of consecutive tones for the first time. Mirta is also very curious, so she wants to know the expected number of key presses for each prefix of her wanted array of tones as well.
Input
The first line contains a positive integer , the number of different piano tones ().
The second line contains a positive integer , the length of the wanted array ().
The third line contains the array of positive integers, each between and inclusive.
Output
Print lines. The -th line must contain the expected number of key presses until Mirta hears the prefix of length of her wanted array for the first time, modulo .
The expected number of key presses is always an integer.