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 have to walk in line to
the dinning hall every day. When they are walking in line, if and only if two kids can see each other, they will talk to each other. Two kids can see each other if and only if all kids between them are shorter then both of them, or there are no kids between
them. Kids do not only look forward, they may look back 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 be too quiet(for it’s boring), so Linda decides that she must form a line in which
there are 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 you help her? 



Note: All kids are different in height.

Input

Input consists of multiple test cases. Each test case is one line containing two integers. The first integer is n, and the second one is m. (0 < n <= 80, 0 <= m <= 10000). 

Input ends by a line containing two zeros.

Output

For each test case, output one line containing the reminder of the number of ways divided by 9937. 

Sample Input

1 0
2 0
3 2
0 0

Sample Output

1
0
4

题意  linda在一个幼儿园当老师  他要把n个学生排成一列  使仅仅有m对学生可以讲话   当两个学生相邻或者他们之间的全部人都比他们矮时   他们就行讲话

每一个学生的身高都不同

令d[i][j]表示把i个学生排成一列使j对学生可以讲话的方法数

能够把i个学生分成i-1个学生和一个最矮的学生  把这个学生放在i-1个学生中随意两个学生之间都不会影响原来的结果  可是能讲话的学生对数添加了2  有i-2种放法

或者把这个最矮的学生放在两边  这样能讲话的对数仅仅添加了1   有两种放法

所以有转移方程d[i][j]=d[i-1][j-2]*i-2+d[i-1][j-1]*2(j>=2)

j<2时仅仅有d[1][0]=1(学生数大于1就有相邻的就能讲话了) 和d[2][1]=2(学生数大于2肯定不止一对能讲话了)两个有值  能够作为初始条件

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 81, 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;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

POJ 3934 Queue(DP)的更多相关文章

  1. 周赛 POJ 3934 Queue

    Description Linda is a teacher in ACM kindergarten. She is in charge of n kids. Because the dinning ...

  2. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  3. poj 3140(树形dp)

    题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...

  4. poj 3249(bfs+dp或者记忆化搜索)

    题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...

  5. poj 1088 滑雪 DP(dfs的记忆化搜索)

    题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 ...

  6. poj 3071 可能DP

    http://poj.org/problem? id=3071 推方程不难,可是难在怎么算 dp[i][j]表示第i场时第j仅仅队伍存活下来的概率 方程:dp[i][j]=sigma(dp[i-1][ ...

  7. poj 2342树形dp板子题1

    http://poj.org/problem?id=2342 #include<iostream> #include<cstdio> #include<cstring&g ...

  8. poj上的dp专题

    更新中... http://poj.org/problem?id=1037 dp[i][j][0]表示序列长度为i,以j开始并且前两位下降的合法序列数目; dp[i][j][1]表示序列长度为i, 以 ...

  9. POJ 2096 (概率DP)

    题目链接: http://poj.org/problem?id=2096 题目大意:n种bug,s个子系统.每天随机找一个bug,种类随机,来自系统随机.问找齐n种bug,且每个子系统至少有一个bug ...

随机推荐

  1. 使用VBA将批量的WORD文档转换为PDF

    Sub BatchConvertToPDF() Dim destFolderPath As String destFolderPath = GetFolderPath If destFolderPat ...

  2. 【前段开发】行内元素和块级元素总结(HTML CSS)

    块级元素 <address> information on author <blockquote> long quotation <button> push but ...

  3. sed中求公共前缀

    string1="test toast" string2="test test" printf "%s\n%s\n" "$stri ...

  4. POJ 1274 The Perfect Stall 水二分匹配

    主题链接:id=1274">点击打开链接 呵呵 #include<cstdio> #include<cstring> #include<cstdlib&g ...

  5. ExcelHelper Excel,Export,Import

    using System; using System.Collections.Generic; using System.Data; using System.Data.Odbc; using Sys ...

  6. EXE文件结构和读取方法

    一.EXE文件概念 EXE File英文全名executable file .译作可运行文件,可移植可运行 (PE) 文件格式的文件,它能够载入到内存中.并由操作系统载入程序运行,是可在操作系统存储空 ...

  7. 最简单的ADABOOST人脸检测程序。COPY执行,前提是你配置OpenCV周围环境

    #include "cv.h" #include "highgui.h" #include "stdio.h" void main() { ...

  8. log(n)在第一时间,以确定该阵列i小号码

    简介参考算法9.2 int partition(int *a,int p,int r){ int x=a[r]; int i=p-1; for(int j=p;j<=r-1;j++){ if(a ...

  9. hdu1978--How many ways(内存搜索)

    How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  10. 基于JAVA WEB技术旅游服务网站系统设计与实现网上程序代写

    基于JAVA WEB技术旅游服务网站系统设计与实现网上程序代写 专业程序代写服务(QQ:928900200) 随着社会的进步.服务行业的服务水平不断发展与提高,宾馆.酒店.旅游等服务行业的信息量和工作 ...