zoukankan      html  css  js  c++  java
  • HDU 4165 Pills(动态规划)

    Pills

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 42    Accepted Submission(s): 36

    Problem Description

    Aunt Lizzie takes half a pill of a certain medicine every day. She starts with a bottle that contains N pills.

    On the first day, she removes a random pill, breaks it in two halves, takes one half and puts the other half back into the bottle.

    On subsequent days, she removes a random piece (which can be either a whole pill or half a pill) from the bottle. If it is half a pill, she takes it. If it is a whole pill, she takes one half and puts the other half back into the bottle.

    In how many ways can she empty the bottle? We represent the sequence of pills removed from the bottle in the course of 2N days as a string, where the i-th character is W if a whole pill was chosen on the i-th day, and H if a half pill was chosen (0 <= i < 2N). How many different valid strings are there that empty the bottle?

    Input

    The input will contain data for at most 1000 problem instances. For each problem instance there will be one line of input: a positive integer N <= 30, the number of pills initially in the bottle. End of input will be indicated by 0.

    Output

    For each problem instance, the output will be a single number, displayed at the beginning of a new line. It will be the number of different ways the bottle can be emptied.

    Sample Input

    6

    1

    4

    2

    3

    30

    0

    Sample Output

    132

    1

    14

    2

    5

    3814986502092304

    Source

    The 2011 Rocky Mountain Regional Contest

    Recommend

    lcy

    解题报告:做这道题的时候以为是递归呢!结果推了一下推不出来,今天杭电比赛不知道,来的比较晚,看队友做出来了,就问了一下队友,其实这道题是一道动态规划的题目,每个状态都和前面的状态有关。为了好描述这道题我画了一张表,如下:就是把每一行加起来即为所求。

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int N = 32;
    __int64 dp[N][N];//这儿也得是64位
    void DP()
    {
    int i , j;
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;//初始化
    for (i = 1; i <= 30; ++i)//初始化
    {
    dp[i][1] = 1;
    }
    for (i = 1; i <= 30; ++i)
    {
    for (j = 2; j <= i; j ++)
    {
    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];//状态方程
    }
    }
    }
    int main()
    {
    int n, i;
    __int64 ans;
    DP();
    while (scanf("%d", &n) != EOF &&n)
    {
    ans = 0;
    for (i = 1; i <= n; ++i)
    {
    ans += dp[n][i];
    }
    printf("%I64d\n", ans);
    }
    return 0;
    }



  • 相关阅读:
    【iCore3 双核心板_ uC/OS-III】例程三:任务的挂起与恢复
    病例讨论-----鼻窦炎一例(联想的风)
    桂枝二越婢一治疗鼻窦炎(联想的风)
    半夏厚朴汤治疗双肺支气管炎(联想的风)
    茯苓饮治疗呕吐(联想的风)
    半夏泻心汤治疗腹泻一例(联想的风)
    己椒苈黄汤治水肿案(联想的风)
    苓甘五味姜辛汤(联想的风病案)
    黃芩汤(联想的风病案)
    九味羌活汤的理解---王幸福
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2378561.html
Copyright © 2011-2022 走看看