Count how many arrays of length over values in have exactly adjacent equal pairs.
You are given three integers , , and .
Count the number of arrays of length such that:
- Each element is an integer from $1m$ inclusive.
- Exactly indices satisfy and .
Return the number of such arrays modulo .
This is a counting problem: you do not need to construct the arrays, only count how many satisfy the condition.
Input Format
A single test instance is given as three integers:
- — length of the array
- — number of possible values
- — required number of adjacent equal pairs
Output Format
Return one integer: the number of arrays of length with exactly matching adjacent pairs, modulo .
Constraints
- Answers are taken modulo
Note: the exact official limits may vary by platform, but the problem is typically solved under modular arithmetic with combinatorial DP.
Example 1
Input
n = 3, m = 2, k = 1
Output
4
Explanation
Arrays of length 3 over {1, 2} with exactly one equal adjacent pair are:
- [1, 1, 2]
- [1, 2, 2]
- [2, 2, 1]
- [2, 1, 1]
So the answer is 4.
Example 2
Input
n = 2, m = 3, k = 0
Output
6
Explanation
We need arrays of length 2 with no equal adjacent pair. The first element can be any of 3 values, and the second must be different, giving arrays.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.