zoukankan      html  css  js  c++  java
  • hdu 4165 Pills dp

    Pills

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


    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
    题意:n个药片,每次吃半片,2*n天吃完,求有多少种吃法;
    思路:dp,dp[i][j]表示剩余i个药片,j个半个药片的方案数;
       dp[i][j]=dp[i-1][j+1]+dp[i][j-1];
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    const int N=2e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    ll dp[110][110];
    void init()
    {
        for(int i=0;i<=35;i++)
            dp[0][i]=1;
        for(int i=1;i<=30;i++)
        {
            for(int j=0;j<=30;j++)
            if(j==0)
                dp[i][j]=dp[i-1][j+1];
            else
                dp[i][j]=dp[i-1][j+1]+dp[i][j-1];
        }
    }
    int main()
    {
        init();
        int n;
        while(~scanf("%d",&n))
        {
            if(n==0)break;
            printf("%lld
    ",dp[n][0]);
        }
        return 0;
    }
  • 相关阅读:
    程序员面试中有几大法则
    实现立方体旋转
    为什么技术与产品沟通起来总是那么痛苦
    移动互联网下半场的面试真经,让你进入 BAT 不再是梦
    Javascript+H5实现打小地鼠游戏
    Python多进程应用
    Python数据分析笔记:聚类算法之K均值
    给新手程序员的16个工作必备小妙招,省下时间去LOL吧!
    MySQL数据库开发常见问题及优化
    java中的四种引用类型
  • 原文地址:https://www.cnblogs.com/jhz033/p/6005112.html
Copyright © 2011-2022 走看看