hdu1134 Game of Connections(Catalan+高精)

Problem Description

This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, … , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect.

It’s still a simple game, isn’t it? But after you’ve written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?

Input

Each line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 <= n <= 100.

Output

For each n, print in a single line the number of ways to connect the 2n numbers into pairs.

Sample Input
2
3
-1

Sample Output
2
5

分析:
圆上两点不相交连线
题目可以抽象为凸多边形定点两两连线且互不相交,求方案数
第1个点只能和偶数下标的点相连,
不管怎么连,多边形都能被分成两部分
f[n]=∑f[i]*f[n-1-i]
卡特兰数模型

这里写图片描述

这道题没有模数了
所以我们只能选择高精度

tip

代码中写的是高乘低和高除低
不是特别优美

//这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int n;
int a[103][103];
int len[103];

void catalan()      //递推求解 
{
    len[1]=1;
    a[1][1]=1;
    int l=1;
    for (int i=2;i<=100;i++)
    {
        for (int j=1;j<=l;j++)           //先暴力乘上 
            a[i][j]=a[i-1][j]*(4*i-2);

        int d=0;                         //进位的处理 
        for (int j=1;j<=l;j++)
        {
            a[i][j]+=d;
            d=a[i][j]/10;
            a[i][j]%=10;
        }
        while (d)
        {
            a[i][++l]=d;
            d=a[i][l]/10;
            a[i][l]%=10;
        }

        d=0;
        for (int j=l;j>=1;j--)           //   /(n+1)
        {
            int t=d*10+a[i][j];
            a[i][j]=t/(i+1);
            d=t%(i+1);
        }

        while (a[i][l]==0) l--;
        len[i]=l;
    }
}

int main()
{
    catalan();
    scanf("%d",&n);
    while (n!=-1)
    {
        for (int i=len[n];i>=1;i--)
            printf("%d",a[n][i]);
        puts("");
        scanf("%d",&n);
    }
}
原文地址:https://www.cnblogs.com/wutongtong3117/p/7673057.html