If the odd items of a sequence are all larger than the previous item, and the even items are all smaller than the previous item, it is called a wobble sequence. That is, a[2i]<a[2i-1], a[2i+1]>a[2i].
Xiao Ming wants to know how many swing sequences with length m and each number being a positive integer between 1 and n.
Input format
The input line contains two integers m and n.
Output format
Output an integer to indicate the answer. The answer may be very large, please output the remainder of the answer divided by 10000.
Sample input
3 4
Sample output
14
Sample description
The following is the swing sequence that meets the requirements:
2 1 2
2 1 3
2 1 4
3 1 2
3 1 3
3 1 4
3 2 3
3 2 4
4 1 2
4 1 3
4 1 4
4 2 3
4 2 4
4 3 4
Evaluation use case scale and conventions
For 20% of the evaluation cases, 1 <= n, m <= 5;
for 50% of the evaluation cases, 1 <= n, m <= 10;
for 80% of the evaluation cases, 1 <= n, m <= 100 ;
For all evaluation cases, 1 <= n, m <= 1000.
Meaning of the question : find the number of swing sequences of length m in the range from 1 to n
Idea : According to the data volume of the subject, brute force search will definitely not pass. This kind of swing is naturally associated with DP, but the problem is how to express this change. Here we can use dp[i][j] to express:
Odd number: when the i-th digit is the smallest number j, how many:
Even number: when the i-th digit is the maximum number of j:
When i is an even number: dp[i][j] = dp[i-1][j+1] + dp[i][j-1]; (here we can understand that when the i-th digit is represented, When the maximum number is j, it is divided into two parts—when the maximum number of the i-th digit is j-1, and when the minimum number of the i-1th digit is j+1 (that is, when the i-th digit is j))
When i is an odd number: dp[i][j] = (dp[i-1][j-1] + dp[i][j+1]); (here we can understand it as representing the i-th digit When the minimum number is j, it is divided into two parts—when the minimum number of the i-th digit is j+1, and when the maximum number of the i-1th digit is j-1 [that is, when the i-th digit is j]
Finally, if our total length is odd, then it is dp[m][1], if it is even, it is dp[m][n].
Code:
#include <iostream>
using namespace std;
int dp[1010][1010];
int main() {
// m为长度,n为数的范围
int m,n;
cin>>m>>n;
for(int i = 1; i <= n; i++)
dp[1][i] = n - i + 1;
for(int i = 2; i <= m; i++)
if(i & 1)
for(int j = n; j >= 1; j--)
dp[i][j] = (dp[i-1][j-1] + dp[i][j+1]) % 10000;
else
for(int j = 1; j <= n; j++)
dp[i][j] = (dp[i-1][j+1] + dp[i][j-1]) % 10000;
int ans = m & 1 ? dp[m][1] : dp[m][n];
cout<<ans;
return 0;
}