Sequence and Transformation
Time limit2sMemory limit512 MB
Count length-n sequences with entries in [1,m] whose image after applying a min-based affine transformation k times has the given max-minus-min value.
- Level
Hard9 of 10
- Topics
- Combinatorics, Math, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
A transformation is applied to a sequence of length . The transformation has two steps. First, build a new sequence with the formula below.
Then replace the sequence with the sequence , so takes the value for every .
For a sequence of length , define .
The sequence is the result of applying the transformation times to some sequence, and you are given the value together with . Write a program that counts the sequences satisfying both conditions below.
- for every .
- , where is the sequence obtained by applying the transformation times to the sequence .
Input
The first line contains the number of test cases ().
Each test case is one line with four integers , , , separated by spaces ().
Output
For each test case, print the answer modulo on its own line.