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. Android开发ListView使用OnScrollListener实现分页加载数据

    上篇博文和大家分享了下拉刷新,这是一个用户体验很好的操作方式.新浪微薄就是使用这样的方式的典型. 还有个问题,当用户从网络上读取微薄的时候.假设一下子所有载入用户未读的微薄这将耗费比較长的时间,造成不 ...

  2. C++转让Lua

    转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/39756423 我使用的cocos2d-x版本号是2.3.3.先在一个C++projec ...

  3. Unity3D在一建筑GL材料可以改变颜色和显示样本

    void CreateLineMaterial()     {         if (!mat)         {             mat = new Material("Sha ...

  4. 关于读style元素定义样式表兼容性

    <span style="font-size:18px;"></span><pre name="code" class=" ...

  5. Oracle SQL Lesson (11) - 创建其他数据库对象(试图/序列/索引/同义词)

    schema(模式)一个用户下一组对象的集合,一般与用户名一致. 视图 CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias].. ...

  6. STL 源代码分析 算法 stl_algo.h -- merge

    本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie merge (应用于有序区间) ------------------------------ ...

  7. jquey :eq(1)

    $("#div_Goods .datagrid-row .numberbox:eq(1)") $("#div_Goods .datagrid-row .numberbox ...

  8. 创Wcf案例数据服务

    首先,创建实体类: using System; using System.Linq; using System.Collections.Generic; using System.Data.Servi ...

  9. windows Azure平台开发

    rest api 和SDK power shell azure pack

  10. Codeforces Round #256 (Div. 2) D. Multiplication Table 二分法

     D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes input st ...