Forever Young
Time limit2sMemory limit1024 MB
Count the ways to turn non-increasing array A into B in exactly k unit steps, where every intermediate array must also stay non-increasing.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Combinatorics, Math
- Solved
- No attempts yet
Problem
Little Misha plays with infinite arrays which consist of nonnegative integers. An array is called good if it is non-increasing.
In one step, Misha can increase or decrease one number in a good array by , if the array remains good after this operation.
Initially, Misha had an array . Misha made steps and obtained an array . In how many ways could he have obtained it?
Input
The first line contains a single integer (): the number of nonzero elements in . The second line contains integers separated by spaces: , the elements themselves. All other elements of are zeroes.
The next two lines describe in the same format.
It is guaranteed that and .
The last line contains the single integer ().
Output
Print the number of ways modulo the prime .
Hint
In the first sample, the ways are: , , , , , , .
In the second sample, it is impossible to obtain the second array from the first in steps.