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

     

  • 相关阅读:
    ECharts 上传图片Example
    SpringBoot|mybatis-maven依赖
    SpringBoot|web应用开发-CORS跨域资源共享
    IDEA|自动生成序列化ID
    SpringBoot|自定义业务异常使用
    SpringBoot|常用配置介绍
    SpringBoot|多环境配置
    SpringBoot|其他常用注解
    SpringBoot|以jar包形式运行springboot服务
    SpringBoot|restfull风格的接口实现方式
  • 原文地址:https://www.cnblogs.com/BobHuang/p/7463881.html
Copyright © 2011-2022 走看看