POJ 2084 Game of Connections 卡特兰数

看了下大牛们的,原来这题是卡特兰数,顺便练练java。递归式子:h(0)=1,h(1)=1   h(n)= h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2)   打表172MS

import java.math.BigInteger;
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        BigInteger[] a=new BigInteger [205];
        a[0]=a[1]=BigInteger.ONE;
        for(int i=2;i<=200;i++){
            a[i]=BigInteger.ZERO;
            for(int j=0;j<i;j++){
                a[i]=a[j].multiply(a[i-j-1]).add(a[i]);
            }
            //System.out.println(a[i]);
        }
        while(true){
            int n=in.nextInt();
            if(n==-1)
                break;
            System.out.println(a[n]);
        }
    }

}
时间: 2024-08-27 07:55:00

POJ 2084 Game of Connections 卡特兰数的相关文章

POJ 2084 Game of Connections(卡特兰数)

卡特兰数源于组合数学,ACM中比较具体的使用例子有,1括号匹配的种数.2在栈中的自然数出栈的种数.3求多边形内三角形的个数.4,n个数围城圆圈,找不相交线段的个数.5给定n个数,求组成二叉树的种数…… 此题就是第4个样例,是裸卡特兰数,但是这里牵扯的大数,可以使用java的大数类解决,但是我这里使用高精度乘法和除法模拟的(主要是java不会). 此处的递推式为H[1] = 1:H[n] = H[n-1]*(4*n-2)/(n+1){n>=2}:代码如下: 需要注意输出的形式,我这里的进制是100

POJ2084 Game of Connections 卡特兰数 关于卡特兰数经典的几个问题

Game of Connections Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9128   Accepted: 4471 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 ord

POJ 2084 Game of Connections

卡特兰数. #include<stdio.h> #include<string.h> const __int64 base=1000000000; const int lenth=100; void mul(__int64 a[],int len,int b) { int i; __int64 jw=0; for(i=len-1;i>=0;i--) { jw=jw+a[i]*b; a[i]=jw%base; jw=jw/base; } } void div(__int64 a

B - Game of Connections(卡特兰数)

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. E

POJ 2084 Catalan数

[题意]: 一个环上有2*N个连续的数,求将这些数两两连接且连接的边不相交的方法数. [知识点]: 数学+Catalan数 令h(1)=1,h(0)=1 递归式1:h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2) 递归式2:h(n)=((4*n-2)/(n+1))*h(n-1); 该递推关系的解为:h(n)=C(2n,n)/(n+1) (n=1,2,3,...) [题解]: Catalan数典型的应用,2*N的答案为h(N). [

HDU 1134 Game of Connections(卡特兰数)

题目代号:HDU 1134 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1134 Game of Connections Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4668    Accepted Submission(s): 2729 Problem Description Thi

POJ 2084 Catalan数+高精度

POJ 2084 /**************************************** * author : Grant Yuan * time : 2014/10/19 15:42 * source : POJ 2084 * algorithm: Catalan数+高精度 * ***************************************/ import java.io.*; import java.math.*; import java.util.*; publ

poj 1095 题解(卡特兰数+递归

题目 题意:给出一个二叉树的编号,问形态. 编号依据 1:如果二叉树为空,则编号为0: 2:如果二叉树只有一个节点,则编号为1: 3:所有含有m个节点的二叉树的编号小于所有含有m+1个节点的二叉树的编号: 4:如果一棵含有m个节点的二叉树(左子树为L,右子树为R)的编号为n,要想其它含有m个节点的二叉树的编号如果大于n,则需要满足两个条件中的任意一个:1.左子树的编号大于L的左子树的编号等于L的编号,但是右子树的编号大于R的编号.(大概就是先将右子树的个数填满将变幻完后再将右子树的点向左子树转移

组合数学--卡特兰数-持续更新

参考资料: 基本介绍和各种分类 http://www.cnblogs.com/topW2W/p/5410875.html 另类递归式: h(n)=h(n-1)*(4*n-2)/(n+1);  (从n开始,更常用) 前几个卡特兰数:规定C0=1,而 分类 :  括号,栈,矩阵乘法,     凸多边形划分,二叉搜索树构造      步数上下,找零, C1=1,C2=2,C3=5,C4=14,C5=42, C6=132,C7=429,C8=1430,C9=4862,C10=16796, C11=587