一道趣题(catalan数)

Description


The director of a new movie needs to create a scaled set for the movie. In the set there will be N skyscrapers, with distinct integer heights from 1 to N meters. The skyline will be determined by the sequence of the heights of the skyscrapers from left to right. It will be a permutation of the integers from 1 to N.

The director is extremely meticulous, so she wants to avoid a certain sloping pattern. She doesn't want for there to be ANY three buildings in positions i, j and k, i < j < k, where the height of building i is smaller than that of building j, and building j's height is smaller than building k's height.

Your task is to tell the director, for a given number of buildings, how many distinct orderings for the skyline avoid the sloping pattern she doesn't like.


Input

There will be several test cases in the input. Each test case will consist of a single line containing a single integer N (3$ \le$N$ \le$1, 000), which represents the number of skyscrapers. The heights of the skyscrapers are assumed to be 1, 2, 3,..., N. The input will end with a line with a single 0.


Output

For each test case, output a single integer, representing the number of good skylines - those avoid the sloping pattern that the director dislikes - modulo 1,000,000. Print each integer on its own line with no spaces. Do not print any blank lines between answers.


catalan数

/*#include<cstdio>
#include<algorithm>
using namespace std;
#define mod 1000000
int main(){
    int n,i,j,k,sum,s[1010],f;
    //while(scanf("%d",&n))
    for(n=3;n<=20;n++){
        if(n==0)break;
        for(i=1;i<=n;i++)
            s[i]=i;
        sum=0;
        do{
            f=1;
            for(i=1;i<=n && f;i++)
                for(j=i+1;j<=n && f;j++)
                    for(k=j+1;k<=n && f;k++){
                        if(s[i]<s[j] && s[j]<s[k]){
                            f=0;
                            break;
                        }
                    }
            if(f)sum++;
        }while(next_permutation(s+1,s+n+1));
        printf(" %d =  %d\n",n,sum);
    }
}*/
#include<cstdio>
#define mod 1000000
long long h[1010];
int main(){
    int n,i,j;
    h[0]=1;h[1]=1;
    for(i=2;i<=1000;i++){
        for(j=0;j<=i-1;j++)
            h[i]=(h[i]+h[j]*h[i-1-j])%mod;
    }
    while(scanf("%d",&n)){
        if(n==0)break;
        printf("%lld\n",h[n]);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值