zoukankan      html  css  js  c++  java
  • hdu1032 Train Problem II (卡特兰数)

    题意:

    给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能。    (题于文末)

     

    知识点:

                                                                                            ps:百度百科的卡特兰数讲的不错,注意看其参考的博客。

    卡特兰数(Catalan):前几项为 : 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670…

        令h(0)=1,h(1)=1,catalan数满足递推式

         h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n>=2)

        另类递推式

        clip_image002

        递推关系的解为:

        clip_image002[8]

        递推关系的另类解为:

       clip_image002[13]

       对于在2n位的2进制中,有n个0,其余为1,且1的累计数>=0的累计数,二进制数有clip_image002[13]

       对于在n位的2进制中,有m个0,其余为1的catalan数为:clip_image002[15]

        理解:catalan数的理解

        应用:

        1.出栈次序: 一个栈(无穷大)的进栈序列为1,2,3,…,n,有多少个不同的出栈序列?          h(n)种。

        2.给定节点组成二叉树:给定n个节点,能构成多少种不同的二叉树?    h(n)种。

        3.括号化:矩阵连乘,依据乘法结合律,不改变其顺序,只用括号表示成对的乘积,有几种括号化的方案?  h(n-1)种。

        4.凸多边形三角划分:在一个凸n边形中,通过若干条互不相交的对角线,有多少种方法把这个多边形划分成若干个三角形?         h(n-2)种。

    /**/   

     

    题解:

    此题为应用1,运用公式   clip_image002 

    catalan数计算一般都涉及大数运算,java写起来方便。

     

    import java.util.Scanner;
    import java.math.BigInteger;
    import java.io.*;
    
    public class Main{
    	public static void main(String[] args){
    		int n;
    		Scanner sc=new Scanner(System.in);
    		while(sc.hasNext()){
    			n=sc.nextInt();
    			BigInteger ans=BigInteger.valueOf(1);
    			for(int i=1;i<=n;i++){
    				ans=ans.multiply(BigInteger.valueOf(4*i-2));
    				ans=ans.divide(BigInteger.valueOf(i+1));
    			}
    			System.out.println(ans);
    		}
    	}
    }

     

     

     

     

     

    Train Problem II

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Submit Status

    Description

    As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.

    Input

    The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.

    Output

    For each test case, you should output how many ways that all the trains can get out of the railway.

    Sample Input

    1 2 3 10

    Sample Output

    1 2 5 16796

    Hint

     The result will be very large, so you may not process it by 32-bit integers. 
  • 相关阅读:
    忍者X2简介+安装包+安装环境说明 [复制链接]
    拖拽的功能,可以看看这个
    openNI驱动控制kinect马达
    室内机器人漫游
    石头剪子布 C++多态实现
    PCL的KinectFusion开源实现
    Microsoft Kinect SDK中的Event Model
    小说下载阅读器_章节保存为XML并显示
    面试题:猫叫、老鼠跑、人醒的一点看法
    JQuery EasyUI之treegrid级联勾选的简单改进版
  • 原文地址:https://www.cnblogs.com/shentr/p/5349707.html
Copyright © 2011-2022 走看看