Pills - HDU 4165 递推打表
来源:互联网 发布:白酒网络推广方案 编辑:程序博客网 时间:2024/04/29 11:16
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?
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
61423300
Sample Output
132114253814986502092304
题意:每天只动一个药片,如果是整的就变成半个,如果是半个就吃掉。
思路:我拙计的递推……用最笨的方式推出来的。那种比我好的递推方式待我学过后回来更新博客的(准备期末考试去了)
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]);}
0 0
- Pills - HDU 4165 递推打表
- HDU 4165(Pills )
- HDU 4165 Pills
- HDU 4165Pills
- HDU 4165 Pills
- HDU 4165 Pills
- hdu 4165 Pills
- hdu 4165 Pills
- HDU 4165 Pills (dp解法)
- HDU 4165 Pills (DP卡特兰数列)
- HDU 4165 Pills (卡特兰数)
- HDU 4165 Pills【卡特兰数】
- HDU 4165 Pills(DP/记忆化搜索/卡塔兰数)
- 杭电 4165 Pills
- pills
- hdoj.4165 Pills【卡特兰数列】 2015/08/27
- ZZULI 1595 Pills
- HDU4165 (卡特兰数列) Pills
- google-glog功能介绍
- 数据库 T-SQL 里的 GO 是什么意思
- 线程同步-生产者与消费者问题
- 支持向量机笔记(二) Lagrange duality拉格朗日对偶
- class-dump逆向分析app
- Pills - HDU 4165 递推打表
- 关于hibernate锁,缓存
- 黑马程序员-Generic
- tar命令【自己留存】
- 用户空间和内核空间通讯之【proc文件系统】
- Windows Live Writer测试帖
- 那么为什么作为函数形参的数组和指针申明可以互换呢?
- VIPS:基于视觉的Web页面分页算法
- K-means聚类算法