Eevee
Time limit5sMemory limit512 MB
Given k random stacks of n fragments, find the expected sum over all ranges of 2 or more stacks of interleavings with no k equal-stone fragments in a row.
- Level
Hard9 of 10
- Topics
- Combinatorics, Probability, Dynamic programming, Math
- Solved
- No attempts yet
Problem
Eevee can evolve in various ways. For instance, it evolves when it comes close to one of the evolution stones.
There are different evolution stones, numbered from 1 to . Each stone is split into fragments. Eevee grouped all the fragments into stacks, numbered from 1 to . Each stack contains exactly fragments, each from a different stone, so each stack can be viewed as a permutation of the stone fragments.
Eevee performs the following operation times. It picks a non-empty stack, removes its topmost fragment, and appends it to the end of a sequence that starts empty. Two ways of combining the stacks are different if at some step a different stack index is chosen. After the process, if the sequence contains neighboring fragments of the same stone, Eevee can evolve by accident. A way to combine the stacks is called good if no interval of length contains only fragments of the same stone.
Let be the number of good ways to combine the stacks with indices in the range . For an interval of length , we assume Eevee performs the operation only times, and no interval of length may contain only fragments of the same stone.
Compute
However, it turned out that Eevee shuffled each stack before the process. So each stack contains a random permutation of its fragments. Each input permutation is chosen uniformly from all permutations, independently of the others.
Input
The first line contains two integers and (), the number of stacks and the number of fragments in each stack. Each of the next lines contains integers. The -th number in the -th line is (, and for ), the -th number from the top of the -th stack.
Output
Output the value of the sum described in the problem statement.
Hint
Consider calculating in the first sample. Eevee has three stacks of the stone fragments:

One of the good ways to combine them is as follows:

The following one is not good, as it contains three 3s in a row:

In the first sample , , and . In the second sample , , , , , and .