zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    window常见事件onload
    BOM顶级对象window
    模拟京东快递单号查询案例
    [Hibernate] one-to-one
    Katy Perry
    [Java] int 转换为BigDecimal
    [easyUI] datagrid 数据格 可以进行分页
    [easyUI] 树形菜单 tree
    [easyUI] lazyload 懒加载
    [easyUI] autocomplete 简单自动完成以及ajax从服务器端完成
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673058.html
Copyright © 2011-2022 走看看