Problem A
Expression Bracketing
Input: standard input
Output: standard output
Time Limit: 1 second
Memory Limit: 32 MB
In this problem you will have to find in how many ways n letters can be bracketed so that the bracketing is non-binary bracketing. For example 4 letters have 11 possible bracketing:
xxxx, (xx)xx, x(xx)x, xx(xx), (xxx)x, x(xxx), ((xx)x)x, (x(xx))x, (xx)(xx), x((xx)x), x(x(xx)). Of these the first six bracketing are not binary. Given the number of letters you will have to find the total number of non-binary bracketing.
Input
The input file contains several lines of input. Each line contains a single integer n (0<n<=26). Input is terminated by end of file.
Output
For each line of input produce one line of output which denotes the number of non binary bracketing with nletters.
Sample Input
3
4
5
10
Sample Output
1
6
31
98187
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
LL da[30];
LL db[30];
LL dfs(int n);
LL dfs2(int n)
{
if(n<=1) return 1;
LL &ans = db[n];
if(~ans) return ans;
ans=0;
for(int i=1;i<=n;i++)
ans+=dfs(i)*dfs2(n-i);
return ans;
}
LL dfs(int n)
{
if(n==1) return 1;
LL &ans = da[n];
if(~ans) return ans;
ans=0;
for(int i=1;i<n;i++)
ans+=dfs(i)*dfs2(n-i);
return ans;
}
LL C(int n, int m)
{
double tmp=0;
for(int i=0;i<m;i++)
tmp+=log10(n-i)-log10(i+1);
return (LL)(pow(10, tmp)+0.5);
}
int main()
{
memset(da, -1, sizeof(da));
memset(db, -1, sizeof(db));
LL n;
while(~scanf("%lld", &n))
{
if(n==1) puts("0");
else printf("%lld\n", dfs(n)-C(2*n-2, n-1)/n);
}
}
C(2*n-2, n-1)/n是用来求卡特兰数的。数据量不会超过long long。是道不错的题。