zoukankan      html  css  js  c++  java
  • 组合数学的卡特兰数 TOJ 3551: Game of Connections

    这个就是卡特兰数的经典问题

    直接用这个公式就好了,但是这个题涉及大数的处理h(n)=h(n-1)*(4*n-2)/(n+1)

    其实见过好几次大数的处理了,有一次他存的恰好不多于30位,直接分成两部分long long 存了

    这个只涉及到大数乘小数,大数除以小数,所以比较简单些

    3551: Game of Connections 分享至QQ空间

    Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
    Total Submit: 5            Accepted:4

    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

    Hint

    The result may exceed 2^64.

    #include <stdio.h>
    int a[101][66];
    int main()
    {
        a[1][0]=1;
        for(int i=2; i<101; i++)
        {
            int c=0;
            for(int j=0; j<66; j++)
            {
                a[i][j]=a[i-1][j]*(4*i-2)+c;
                c=a[i][j]/10;
                a[i][j]%=10;
            }
            c=0;
            for(int j=65; j>=0; j--)
            {
                c=c*10+a[i][j];
                a[i][j]=c/(i+1);
                c%=(i+1);
            }
        }
        int n;
        while(~scanf("%d",&n),n>0)
        {
            int t=65;
            while(a[n][t]==0)t--;
            while(t>=0)printf("%d",a[n][t--]);
            putchar(10);
        }
        return 0;
    }

     

  • 相关阅读:
    SQLServer2012数据库降级至SQLServer2008R2的方法
    男程序猿和女程序猿的网恋—相见(二)
    收藏关于AI的相关的文章
    JAVA提高九:集合体系
    JAVA提高八:动态代理技术
    JAVA提高七:类加载器
    JAVA提高六:泛型
    JAVA提高五:注解Annotation
    JAVA提高四:反射基本应用
    JAVA提高三:反射总结
  • 原文地址:https://www.cnblogs.com/BobHuang/p/7463881.html
Copyright © 2011-2022 走看看