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);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值