Poj 3934 queue

Source: Internet
Author: User

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 M, 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 C An 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 02 03 20 0

Sample output

104

If the answer is modeled as 9937, we will all think of recurrence. The key is how to push it.

Idea: I Personal J can be from 1: I-1 personal J-2 to insert a minimum person, so the original combination has no effect 2: I-2 personal J-1 on, and then insert a person on both sides, it does not affect the original combination.

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>using namespace std;const int M=9937;int dp[85][10005];void init(){    dp[1][0]=1;    dp[2][1]=2;    for(int i=3;i<=80;i++)    {        for(int j=0;j<=10000;j++)        {            if(j>=2)                dp[i][j]=(dp[i-1][j-2]*(i-2))%M;            if(j>=1)                dp[i][j]=(dp[i][j]+dp[i-1][j-1]*2)%M;        }    }}int main(){    init();    int n,m;    while(cin>>n>>m)    {        if(n==0&&m==0)            break;        cout<<dp[n][m]<<endl;    }    return 0;}


Poj 3934 queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.