zoukankan      html  css  js  c++  java
  • HDU1134_Game of Connections 卡特兰数

    解题思路:                 看着数据,有点儿像卡特兰数,直接写了看,就过了。。。 前几项为 (OEIS中的数列A000108): 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452, ... 总结下卡特兰数,不然还晕乎乎的,做了很多卡特兰数的题,公式倒是记住了,但是对这个卡特兰数的意义理解并不是很多,现在,总结下吧:                卡特兰数实质:h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2)  经常用的公式是:                 令h(1)=1,h(0)=1,catalan数满足递归式:                  h(n)=h(n-1)*(4*n-2)/(n+1);   卡特兰数模型: 1.出栈次序问题             一个栈(无穷大)的进栈序列为1,2,3,…,n,有多少个不同的出栈序列? 2.凸多边形的三角剖分问题         求将一个凸多边形区域分成三角形区域的方法数。 类似:一位大城市的律师在她住所以北n个街区和以东n个街区处工作。每天她走2n个街区去上班。如果她从不穿越(但可以碰到)从家到办公室的对角线,那么有多少条可能的道路?    类似:在圆上选择2n个点,将这些点成对连接起来使得所得到的n条线段不相交的方法数? 3.用给定节点组成二叉树的问题          (能构成h(N)个)
    import java.math.BigInteger;
    import java.math.BigInteger;
    import java.util.Scanner;
    public class Main
    {
        public static void main(String []args)
        {
            Scanner cin=new Scanner(System.in);
            BigInteger h[]=new BigInteger[1005];
            h[0]=new BigInteger("1");
            h[1]=new BigInteger("1");
            BigInteger one=new BigInteger("1");
            BigInteger four=new BigInteger("4");
            BigInteger two=new BigInteger("2");
            for(int i=2;i<1005;i++)
            {
                String str=String.valueOf(i);
                BigInteger n=new BigInteger(str);
                BigInteger temp=new BigInteger("0");
                h[i]=(n.multiply(four).subtract(two)).multiply(h[i-1]).divide(n.add(one));
                //h[i]=temp.divide(n.add(one));
                //h[i]=h[i-1].multiply(4*n-1).divide(n.add());    
            }
            while(cin.hasNext())
            {
                
                //令h(1)=1,h(0)=1,catalan数满足递归式: 
                //h(n)=(4*n-2)/(n+1)*h(n-1); 
                int n=cin.nextInt();
                if(n==-1)
                    break;
                System.out.println(h[n]);
            }
        }
    }
  • 相关阅读:
    js的基本数据类型有哪些?
    页面优化的方式有哪些?
    为什么要用rem
    sass开发过程中遇到的几个坑
    js事件监听
    js数组去重的方法
    什么是怪异盒模型
    Cookie SameSite属性介绍及其在ASP.NET项目中的应用
    HttpServlet在第一个Servlet程序中的知识点
    Myeclipse 2017 创建第一个servlet步骤
  • 原文地址:https://www.cnblogs.com/cchun/p/2520156.html
Copyright © 2011-2022 走看看