Return the number of possible k-even arrays of size n where all elements are in the range [1, m].
Since the answer may be very large, return it modulo109 + 7.
Example 1:
Input:n = 3, m = 4, k = 2
Output:8
Explanation:
The 8 possible 2-even arrays are:
[2, 2, 2]
[2, 2, 4]
[2, 4, 2]
[2, 4, 4]
[4, 2, 2]
[4, 2, 4]
[4, 4, 2]
[4, 4, 4]
Example 2:
Input:n = 5, m = 1, k = 0
Output:1
Explanation:
The only 0-even array is [1, 1, 1, 1, 1].
Example 3:
Input:n = 7, m = 7, k = 5
Output:5832
Constraints:
1 <= n <= 750
0 <= k <= n - 1
1 <= m <= 1000
Solutions
Solution 1: Memoization Search
Thinking
We count length-\(n\) arrays over \([1,m]\) with exactly \(k\) adjacent even pairs. Only parity matters: there are \(\lfloor m/2 \rfloor\) evens and \(m-\lfloor m/2 \rfloor\) odds.
State \((i,j,\textit{last})\) is the number of ways after \(i\) positions, with \(j\) even-pairs left and a given previous parity. An even costs one pair only when the previous value was even.
Negative \(j\) is zero; a finished array scores one iff \(j=0\). The dummy previous parity is odd so the first cell cannot form a pair.
Given the numbers \([1, m]\), there are \(\textit{cnt0} = \lfloor \frac{m}{2} \rfloor\) even numbers and \(\textit{cnt1} = m - \textit{cnt0}\) odd numbers.
We design a function \(\textit{dfs}(i, j, k)\), which represents the number of ways to fill up to the \(i\)-th position, with \(j\) remaining positions needing to satisfy the condition, and the parity of the last position being \(k\), where \(k = 0\) indicates the last position is even, and \(k = 1\) indicates the last position is odd. The answer is \(\textit{dfs}(0, k, 1)\).
The execution logic of the function \(\textit{dfs}(i, j, k)\) is as follows:
If \(j < 0\), it means the remaining positions are less than \(0\), so return \(0\);
If \(i \ge n\), it means all positions are filled. If \(j = 0\), it means the condition is satisfied, so return \(1\), otherwise return \(0\);
Otherwise, we can choose to fill with an odd or even number, calculate the number of ways for both, and return their sum.
The time complexity is \(O(n \times k)\), and the space complexity is \(O(n \times k)\). Here, \(n\) and \(k\) are the parameters given in the problem.
The memoized recursion is slower in some languages. The same transition is a layer DP: \(f[i][j][0/1]\) after \(i\) positions, \(j\) even-pairs, and a last parity.
An even comes from an odd last cell or from an even last cell with \(j-1\) pairs; an odd never increases the pair count. \(f[0][0][1]=1\) matches the search base.
The answer is \(f[n][k][0]+f[n][k][1]\).
We can convert the memoized search from Solution 1 into dynamic programming.
Define \(f[i][j][k]\) to represent the number of ways to fill the \(i\)-th position, with \(j\) positions satisfying the condition, and the parity of the previous position being \(k\). The answer will be \(\sum_{k = 0}^{1} f[n][k]\).
Initially, we set \(f[0][0][1] = 1\), indicating that after filling the \(0\)-th position, there are \(0\) positions satisfying the condition, and the parity of the previous position is odd. All other \(f[i][j][k]\) are initialized to \(0\).
The time complexity is \(O(n \times k)\), and the space complexity is \(O(n \times k)\), where \(n\) and \(k\) are the parameters given in the problem.