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;
    }

     

  • 相关阅读:
    2018.5.17 memcached
    2018.5.11 B树总结
    2018.5.8 排序总结
    2018.5.8 python操纵sqlite数据库
    2018.5.4 Unix的五种IO模型
    2018.5.3 maven
    2018.5.3 docker
    Mybatis学习笔记,挺全的!
    这么强大的Mybatis插件机制原来就是这?
    Swagger API Spec + Swagger Codegen + YAPI管理接口文档
  • 原文地址:https://www.cnblogs.com/BobHuang/p/7463881.html
Copyright © 2011-2022 走看看