HDU 4165 Pills(Catalan)

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

题目链接

这道题目就是一个卡特兰数列,百度百科解释很详细卡特兰数列
数列的两种递推方式
h ( 0 ) = 1 , h ( 1 ) = 1 h(0)=1,h(1)=1 h(0)=1,h(1)=1

  1. h ( n ) = h ( 0 ) × h ( n 1 ) + h ( 1 ) × h ( n 2 ) + . . . + h ( n 1 ) × h ( 0 ) ( n 2 ) h(n)= h(0)\times h(n-1)+h(1)\times h(n-2) + ... + h(n-1)\times h(0) (n\ge2) h(n)=h(0)×h(n1)+h(1)×h(n2)+...+h(n1)×h(0)(n2)
  2. h ( n ) = h ( n 1 ) × ( 4 × n 2 ) n + 1 h(n)=\frac{h(n-1)\times(4\times n-2)}{n+1} h(n)=n+1h(n1)×(4×n2)

AC代码:

//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <set>
#include <map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
const double eps = 1e-5;
const double e = 2.718281828459;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll Catalan[31];
    mem(Catalan, 0);
    Catalan[0] = 1;
    Catalan[1] = 1;
    for (int i = 2; i <= 30; ++i) {
        for (int j = 0; j <= i - 1; ++j) {
            Catalan[i] += Catalan[j] * Catalan[i - j - 1];
        }
    }
    int n;
    while (cin >> n, n) {
        cout << Catalan[n] << endl;
    }
    return 0;
}

代码:

//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <set>
#include <map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
const double eps = 1e-5;
const double e = 2.718281828459;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll Catalan[31];
    mem(Catalan, 0);
    Catalan[0] = 1;
    Catalan[1] = 1;
    for (int i = 2; i < 31; ++i) {
    	Catalan[i] = Catalan[i - 1] * (4 * i - 2) / (i + 1);
	}
	int n;
	while (cin >> n, n) {
		cout << Catalan[n] << endl;
	}
    return 0;
}
全部评论

相关推荐

阿里巴巴各部门年终奖开奖了,有人拿到了220w
真烦好烦真烦:拿命换钱呢,公司给你220万,肯定是因为你对公司的贡献大于220万,想想要多厉害多累才能达到
投递阿里巴巴集团等公司10个岗位 >
点赞 评论 收藏
分享
03-12 13:51
南昌大学 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务