- 分析:栈是一种常见的数据结构,有许多关于栈的问题,其中之一就是统计元素可能的出栈序列。具体说,就是给定n个元素,依次通过一个栈,求可能的出栈序列的个数。
对于每一个数来说,必须进栈一次、出栈一次。我们把进栈设为状态‘1’,出栈设为状态‘0’。n个数的所有状态对应n个1和n个0组成的2n位二进制数。由于等待入栈的操作数按照1‥n的顺序排列、入栈的操作数b大于等于出栈的操作数a(a≤b),因此输出序列的总数目=由左而右扫描由n个1和n个0组成的2n位二进制数,1的累计数不小于0的累计数的方案种数。
在2n位二进制数中填入n个1的方案数为c(2n,n),不填1的其余n位自动填0。从中减去不符合要求(由左而右扫描,0的累计数大于1的累计数)的方案数即为所求。
任何一个由n+1个0和n-1个1组成的2n位二进制数,由于0的个数多2个,2n为偶数,故必在某一个奇数位上出现0的累计数超过1的累计数。同样在后面部分0和1互换,使之成为由n个0和n个1组成的2n位数,即n+1个0和n-1个1组成的2n位数必对应一个不符合要求的数. - h(n)= h(0) * h(n-1) + h(1) * h(n-2) + …..+ h(n-1)h(0) (其中n>=2) 这个公式更容易理解catalan的本质,表示以某个节点划开,分别计算各自的catalan数,递归求解,很像动规,在一个序列中设置断点。
- h(n)=(4*n-2) * h(n-1)/(n+1);
- 亦可以理解为从1,1开始的特殊的动规。
const int maxLen = 1005;
struct BigInt{
long long num[maxLen];
int digit = 0;
BigInt() = default;
BigInt(char a[]){
int la = strlen(a);
int i = la-8, j = 0;
for(; i >= 0; i -= 8)
sscanf(a+i,"%8d",&num[j++]);
int t; num[j] = 0;
if(i+8 > 0){
for(int k = 0; i+8 > 0; ++k){
sscanf(a+k,"%1d",&t);
num[j] = num[j]*10 + t;
--i;
}
++j;
}
num[j] = -1; digit = j;
}
BigInt(int a){
int j = 0;
num[j++] = a%100000000;
if(a >= 100000000)
num[j++] = a/100000000;
num[j] = -1; digit = j;
}
BigInt(long long a){
int j = 0;
num[j++] = a%100000000;
a /= 100000000;
if(a > 0){
num[j++] = a%100000000;
a /= 100000000;
if(a > 0){
num[j++] = a%100000000;
}
}
num[j] = -1; digit = j;
}
int bigCmp(const BigInt rhs){
if(digit > rhs.digit)
return 1;
else if(digit < rhs.digit)
return -1;
else{
for(int i = digit-1; i >= 0; --i){
if(num[i] > rhs.num[i])
return 1;
else if(num [i] < rhs.num[i])
return -1;
}
return 0;
}
}
BigInt operator + (const BigInt rhs){
BigInt ans; int idx = 0, carry = 0;
while(num[idx]!=-1 && rhs.num[idx]!=-1){
ans.num[idx] = num[idx]+rhs.num[idx]+carry;
carry = ans.num[idx] / 100000000;
ans.num[idx++] %= 100000000;
}
if(num[idx] != -1){
while(num[idx] != -1){
ans.num[idx] = num[idx] + carry;
carry = ans.num[idx] / 100000000;
ans.num[idx++] %= 100000000;
}
}
else{
while(rhs.num[idx] != -1){
ans.num[idx] = rhs.num[idx] + carry;
carry = ans.num[idx] / 100000000;
ans.num[idx++] %= 100000000;
}
}
if(carry > 0)
ans.num[idx++] = carry;
ans.num[idx] = -1;
ans.digit = idx;
return ans;
}
BigInt operator * (const int rhs){
BigInt ans; int carry = 0,idx = 0;
if(rhs != 0){
for(idx = 0; num[idx] != -1; ++idx){
ans.num[idx] = num[idx]*rhs + carry;
carry = ans.num[idx] / 100000000;
ans.num[idx] %= 100000000;
}
if(carry > 0)
ans.num[idx++] = carry;
}
else{
ans.num[idx++] = 0;
}
ans.num[idx] = -1;
ans.digit = idx;
return ans;
}
BigInt operator - (const BigInt rhs){
BigInt ans;
int idx = 0, carry = 0;
while(rhs.num[idx] != -1){
ans.num[idx] = num[idx] - rhs.num[idx] - carry;
carry = 0;
if(ans.num[idx] < 0){
ans.num[idx] += 100000000;
carry = 1;
}
++idx;
}
while(num[idx] != -1){
ans.num[idx] = num[idx] - carry;
carry = 0;
if(ans.num[idx] < 0){
ans.num[idx] += 100000000;
carry = 1;
}
++idx;
}
ans.num[idx] = -1;
ans.digit = idx;
return ans;
}
void frac(int n){
num[0] = 1; num[1] = -1; digit = 1;
int carry, idx;
for(int i = 2; i <= n; ++i){
carry = 0;
for(idx = 0; num[idx] != -1; ++idx){
num[idx] = num[idx]*i + carry;
carry = num[idx] / 100000000;
num[idx] %= 100000000;
}
if(carry > 0)
num[idx++] = carry;
num[idx] = -1;
digit = idx;
}
}
long long operator % (const int rhs){
int idx = digit-1;
long long r = num[idx--]%rhs;
while(idx >= 0){
r = (r*100000000LL+num[idx])%rhs;
--idx;
}
return r;
}
BigInt operator / (const int rhs){
BigInt ans;
int i = digit-1, r = 0;
while(i >= 0){
ans.num[i] = (num[i]+r*100000000LL)/(long long)rhs;
r = (num[i]+r*100000000LL)%(long long)rhs;
--i;
}
ans.digit = digit;
while(ans.digit > 1 && ans.num[ans.digit-1] == 0)
ans.digit--;
ans.num[ans.digit] = -1;
return ans;
}
void Print(){
if(digit > 0){
int i = digit-1;
printf("%I64d",num[i]);
while(i--)
printf("%08I64d",num[i]);
printf("\n");
}
}
};
BigInt catalan(int n){
BigInt ans;
if(n==1 || n==0)
return BigInt(1);
else{
ans = catalan(n-1)*(4*n-2)/(n+1);
}
return ans;
}