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

     

  • 相关阅读:
    c++ 动态判断基类指针指向的子类类型(typeid)
    qt在GUI显示时,将调试信息输出到控制台的设置
    Qt库版查询
    Qt动态库静态库的创建、使用、多级库依赖、动态库改成静态库等详细说明
    ICMP timestamp 请求响应漏洞
    linux 防火墙 ufw使用
    MySQL--binlog和relay log的生成和删除
    SSD 相关基础知识
    python36--将数据保存为excel
    MySQL 5.7并发复制和mysqldump相互阻塞引起的复制延迟
  • 原文地址:https://www.cnblogs.com/BobHuang/p/7463881.html
Copyright © 2011-2022 走看看