Generator
Time limit3sMemory limit256 MB
Find the expected number of random draws until all n given length-L sequences have each appeared as a contiguous block of the stream.
- Level
Hard9 of 10
- Topics
- Probability, String matching, Bit manipulation, Dynamic programming
- Solved
- No attempts yet
Problem
You are given different integer sequences. All sequences have the same length , and all integers in these sequences are from to .
You also have a machine that generates a stream of random numbers. Initially, the stream is empty. Every second, the machine generates a random integer from to , inclusive, and appends it to the stream. Each random integer is generated independently with the same probability distribution, which is given to you.
With probability , eventually, each of the given sequences appears as consecutive elements of the stream. The occurrences of different sequences may overlap. At the first moment when each of the given sequences has appeared at least once, the machine stops immediately. Your task is to calculate the expected number of seconds after which the machine stops.
Input
There are one or more test cases. The first line of input contains an integer , the number of test cases.
Each test case starts with a line containing three positive integers , and : the number of sequences, the upper bound for the integers that may appear in the sequences and in the stream, and the length of each sequence, respectively (, , ).
The next line contains positive integers describing the probability distribution the machine uses. The machine generates with probability , with probability , and so on, where . It is guaranteed that .
Each of the next lines contains an integer sequence of length . All integers in these sequences are positive and do not exceed . It is guaranteed that all given sequences are pairwise distinct.
The total sum of over all test cases does not exceed .
Output
For each test case, calculate the answer as an irreducible fraction and output the integer on a separate line. Here, is the multiplicative inverse of modulo .
It is guaranteed that for all given inputs, the integers and are relatively prime.