Pills - HDU 4165 递推打表

Pills

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


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

题意:每天只动一个药片,如果是整的就变成半个,如果是半个就吃掉。

思路:我拙计的递推……用最笨的方式推出来的。那种比我好的递推方式待我学过后回来更新博客的(准备期末考试去了)

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<string>
using namespace std;
string num[40];
int main()
{ num[1]="1";
num[2]="2";
num[3]="5";
num[4]="14";
num[5]="42";
num[6]="132";
num[7]="429";
num[8]="1430";
num[9]="4862";
num[10]="16796";
num[11]="58786";
num[12]="208012";
num[13]="742900";
num[14]="2674440";
num[15]="9694845";
num[16]="35357670";
num[17]="129644790";
num[18]="477638700";
num[19]="1767263190";
num[20]="6564120420";
num[21]="24466267020";
num[22]="91482563640";
num[23]="343059613650";
num[24]="1289904147324";
num[25]="4861946401452";
num[26]="18367353072152";
num[27]="69533550916004";
num[28]="263747951750360";
num[29]="1002242216651368";
num[30]="3814986502092304";
  int t,i,j,k;
  while(~scanf("%d",&k)&& k>0)
  { cout<<num[k]<<endl;
  }
}

递推代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
long long num[40][40];
int main()
{ int t,i,j,k,p1,p2,p;
  p=0;
  for(k=1;k<=30;k++)
  { memset(num,0,sizeof(num));
    num[k][0]=1;
    t=k*2;
    while(t--)
    { for(i=0;i<=k;i++)
       for(j=0;j<=k;j++)
       if(i*2+j==t)
       { if(j>0)
         num[i][j]=num[i][j+1]+num[i+1][j-1];
         else
         num[i][j]=num[i][j+1];
       }
    }
    printf("num[%d]=\"%lld\";\n",++p,num[0][0]);
  }
}

最后附上最新版的做法:

#include<cstdio>
#include<cstring>
using namespace std;
long long num[35][35];
int main()
{ int i,j,k;
  for(i=0;i<=30;i++)
   num[1][i]=i+1;
  for(i=2;i<=30;i++)
  { num[i][0]=num[i-1][1];
    for(j=1;j+i<=30;j++)
     num[i][j]=num[i-1][j+1]+num[i][j-1];
  }
  while(~scanf("%d",&k)&& k>0)
   printf("%I64d\n",num[k][0]);
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值