AcWing train out of the stack problem

AcWing train out of the stack problem

Description

  • A n train carriages are numbered 1,2,3, ..., n.

    Each car has two motion, push and pop, the arrangement may ask carriages out of the stack of n number of species.

Input

  • Enter an integer n, represents the number of train carriages.

Output

  • S represents an integer output a possible arrangement of n number of carriages in the stack.

Data Size

  • 1≤n≤60000

Sample Input

3

Sample Output

5

answer:

  • Cattleya by the number of cut, proved as follows:

Set DP [i] is the number of i-number scheme. Let x a final element of the stack, the stack has a large number of elements than the elements nx x a, x is smaller than the number of elements of a x-1. Then the number of each part of the program were dp [n - x], dp [x - 1]. Because they influence each other in two parts, it is a multiplicative principle. Then when i = n, dp [n] =? Since x can take the values ​​from 1 to n, the total number of programs so dp [n] = dp [0] * dp [n - 1] + dp [1] * dp [n - 2] + ... dp [ n - 1] * dp [0]. Then this is exactly the number of recursive Cartland.

//因为反正是卡特兰的裸题,高精度又懒(不会)写
//所以嫖了一遍题解上来
//原作者:秦淮岸灯火阑珊大大
#include <iostream>
#include <cstdio>
#define fir(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;

const ll M=1e9;//M为压位的最大值
ll a[60004],l,sum[120004];
int n;

void Prime(int b,int f)
{
    for(int j=2;j*j<=b && b!=1;j++)//质因数分解.
        while(b%j==0)
            sum[j]+=f, b/=j;
    if(b) sum[b]+=f;
}
void High(ll c)
{
    for(int i=1;i<=l;i++) a[i]*=c;
    for(int i=1;i<=l;i++)
        a[i+1]+=a[i]/M,a[i]%=M;//我们需要压缩位置快速处理
    while(a[l+1]) ++l;
}
int main()
{
    a[1]=1,l=1;
    scanf("%d",&n);
    //对于两个组合数相除,我们这道题目必须使用快速的质因数分解法,去处理.
    for(int i=1;i<=n;i++) Prime(n+i,1);
    for(int i=2;i<=n+1;i++) Prime(i,-1);
    for(int i=2;i<=2*n;i++)
        for(ll j=0;j<sum[i];++j) 
            High(i);//高精度
    printf("%lld",a[l]);
    for(ll i=l-1;i;--i) 
        printf("%09lld",a[i]);//输出
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11300737.html