1763. Pass game Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
During physical education classes, Tom often plays games with his classmates. This time, the teacher played a pass game with his classmates.
The game rules are as follows: n students stand in a circle, and one of them holds a ball in his hand. When the teacher starts to pass the ball, each student can pass the ball to either of the two students on the left or right.) When the teacher blew the whistle again, the pass was stopped. At this time, the student with the ball not passed out was the loser, I want to show you a program.
Clever Tom raised an interesting question: how many different pass methods can be used to transfer the ball from the very young man's hand. After passing the ball m times, he returned to Tom. The two pass passing methods are regarded as different methods. if and only when the two methods are used, the students who receive the ball have different sequences in the order of the ball. For example, there are three students on the first, second, and third, and assume that Tom is the first, after passing the ball three times back to Tom, there are two methods: 1-> 2-> 3-> 1 and 1-> 3-> 2-> 1.
Input
There are multiple groups of input cases, one row per Case, two integers separated by spaces n, m3 <= n <= 30,1 <= m <= 30 ).
Output
Each Case group outputs a row with an integer indicating the number of methods that match the meaning of the question.
Sample Input
3 3
Sample Output
2
Dynamic Planning recursion)
If dp [I] [j] is set to the number of solutions that happen to the I user after the j pass, the transfer equation is easy to obtain:
dp[i][j]=d[i-1][j-1]+dp[i+1][j-1]
In addition, because people are in a circle, there are special cases:
dp[1][j]=dp[2][j-1]+dp[n][j-1]
dp[n][j]=dp[n-1][j-1]+dp[1][j-1]
The Code is as follows:
# Include <iostream> # include <cstring> using namespace std; int main () {int n, m; while (cin> n> m) {int dp [31] [31]; memset (dp, 0, sizeof (dp); dp [2] [1] = 1; dp [n] [1] = 1; // dp [I] [j]: j passes just to I for (int j = 2; j <= m; j ++) {for (int I = 1; I <= n; I ++) {if (I = 1) dp [I] [j] = dp [I + 1] [J-1] + dp [n] [J-1]; else if (I = n) dp [I] [j] = dp [I-1] [J-1] + dp [1] [J-1]; else dp [I] [j] = dp [I-1] [J-1] + dp [I + 1] [J-1];} cout <dp [1] [m] <endl ;}// system ("pause"); return 0 ;}