加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数卡特兰数

发布时间:2020-12-14 04:00:09 所属栏目:大数据 来源:网络整理
导读:关于卡特兰数的介绍:http://baike.baidu.com/link?url=ClVyY47KI51GLOK4wJjghAPe0iNHgrgb4zUxrwO1SKGHZXsCR9Ftnhb7driZ8dQmT6mRSQW4BgYbUUvLGIWlR_ 在这儿主要讲解怎样求比较大的卡特兰数,不能直接求,只能用到高精度的思想来求? HDU1023 1130 1134 可做模

关于卡特兰数的介绍:http://baike.baidu.com/link?url=ClVyY47KI51GLOK4wJjghAPe0iNHgrgb4zUxrwO1SKGHZXsCR9Ftnhb7driZ8dQmT6mRSQW4BgYbUUvLGIWlR_

在这儿主要讲解怎样求比较大的卡特兰数,不能直接求,只能用到高精度的思想来求?

HDU1023 1130 1134

可做模板

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;

int a[101][101];
int b[101];

void Catanlan()
{
    int len;
    int cover;
    int temp;
    a[1][0]=b[1]=1;//先处理1
    len=1;
    for(int i=2;i<101;i++)  //卡特兰数的公式
    {
        for(int j=0;j<len;j++)
            a[i][j]=a[i-1][j]*(4*i-2);
        cover=0;
        for(int j=0;j<len;j++)//高精度的思想
        {
            temp=a[i][j]+cover;
            a[i][j]=temp%10;
            cover=temp/10;
        }
        while(cover)//最高位不为0
        {
            a[i][len++]=cover%10;
            cover/=10;
        }
        cover=0;
        for(int j=len-1;j>=0;j--)//除法
        {
            temp=cover*10+a[i][j];
            a[i][j]=temp/(i+1);
            cover=temp%(i+1);
        }
        while(!a[i][len-1])
            len--;
        b[i]=len;
    }
}

int main()
{
   // freopen("C:UsersAdministratorDesktopin.txt","r",stdin);
    int n;
    Catanlan();
    while(cin>>n)
    {
        for(int i=b[n]-1;i>=0;i--)
        {
            cout<<a[n][i];
        }
        cout<<endl;
    }
    return 0;
}

POJ2084

http://poj.org/problem?id=2084

import java.math.*;
import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args)
	{
		Scanner cin =new Scanner(System.in);
		BigInteger num[] =new BigInteger[101];
		num[0]=BigInteger.ONE;
		num[1]=BigInteger.ONE;
		for(int i=2;i<101;i++)
		{
			num[i]=num[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
		}
		while(cin.hasNextInt())
		{
			int n=cin.nextInt();
			if(n==-1)break;
			System.out.println(num[n]);
		}
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读