Queue
Description
linda is a teacher in ACM kindergarten. She is in charge of N kids. Because The dinning hall is a little bit far away from the classroom, those n kids has to walk in line to the dinning HAL L every day. When they is walking in line, if and only if the kids can see the other, they'll talk to each of the other. The kids can see the other if and only if all kids between them is shorter then both of them, or there is no kids betwe En them. Kids do does not have a look forward, they could look up and talk to Kids behind them. Linda don ' t want them to talk too much (for it's not safe), but she also don't want them to being too quiet (for it ' s boring), So Linda decides this she must form a line in which there is exactly m pairs of kids who can see each other. Linda wants to know, in how many different ways can she form such a line. Can help her?
Note:all Kids is different in height.
Input
Input consists of multiple test cases. Each test case was one line containing and integers. The first integer is N, and the second one is m. (0 < n <=, 0 <= m <= 10000).
Input ends by a line containing the zeros.
Output
for each test case, output one line containing the reminder of the number of ways divided by 9937.
Sample Input
1 02 03) 20 0
Sample Output
104
Test instructions Linda is a teacher in a kindergarten he's going to put n students in a row so that only m can talk to students when two students are next to each other or all of them are shorter than they speak.
Every student has a different height.
Make D[i][j] The number of methods by which I have a student to be able to speak to students in a row
able to divide I students into I-1 pupils and a The shortest student put the student in I-1 students random Two students will not affect the original results but can speak of the students added 2 have i-2 seed release method
Or put the shortest student on either side so that the logarithm of the speech can only be added 1 There are two methods of putting
So there is the transfer equation d[i][j]=d[i-1][j-2]*i-2+d[i-1][j-1]*2 (j>=2)
j<2 only have d[1][0]=1 (the number of students is greater than 1, there are neighbors can speak ) and d[2][1]=2 (the number of students greater than 2 must be more than one pair can speak) two values can be used as initial conditions
#include <cstdio> #include <cstring>using namespace std;const int N = Bayi, M = 10001;int d[n][m], N, M;int main () { D[1][0] = 1, d[2][1] = 2; for (int i = 1, i < N; ++i) for (int j = 2; j < M; ++j) d[i][j] = (d[i-1][j-2] * (i-2) + 2 * d[i-1][ J-1])% 9937; while (scanf ("%d%d", &n, &m), N) printf ("%d\n", D[n][m]); return 0;}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
POJ 3934 Queue (DP)