HDU 1131 Count the Trees 大数计算

题目是说给出一个数字,然后以1到这个数为序号当做二叉树的结点,问总共有几种组成二叉树的方式。这个题就是用卡特兰数算出个数,然后因为有编号,不同的编号对应不同的方式,所以结果是卡特兰数乘这个数的阶乘种方案。因为数字比较大,所以要用高精度的方法也就是用字符数组来做,我分别写了三个函数,一个算加法,一个算乘法,最后一个打表,等打出表来最后只要判断一下输入的数是第几个,直接输出就行了,下面是我的代码,第一次写高精度的这种大数处理,可能看上去比较繁琐= =

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char ans[105][1005];
char ads[1005],mus[1005];
char jc[105][1005];
char res[105][1005];

int multiply(char* a1,char* b1)
{
    memset(mus,0,sizeof(mus));
    int i,j,k;
    int len,len1,len2;
    len1=strlen(a1);
    len2=strlen(b1);
    int mut=0,t;
    bool va[1005];
    int s[1005],adt[1005];
    char a[1005],b[1005];
    memset(s,0,sizeof(s));
    memset(adt,0,sizeof(adt));
    memset(va,0,sizeof(va));
    for(i=len1-1,j=0;i>=0;i--)
    {
        a[i]=a1[j];
        j++;
    }
    for(i=len2-1,j=0;i>=0;i--)
    {
        b[i]=b1[j];
        j++;
    }
    for(i=0;i<len1;i++)
    {
        mut=0;
        for(j=0;j<len2;j++)
        {
            t=(a[i]-‘0‘)*(b[j]-‘0‘)+mut;
            mut=0;
            if(t>=10)
            {
                mut=t/10;
                t=t%10;
            }
            s[i+j]=t+s[i+j]+adt[i+j];
            va[i+j]=1;
            adt[i+j]=0;
            if(s[i+j]>=10)
            {
                if(!va[i+j+1])
                    adt[i+j+1]=s[i+j]/10+adt[i+j+1];
                else
                    adt[i+j+1]=s[i+j]/10;
                s[i+j]=s[i+j]%10;
            }
        }
        s[i+j]=mut;
    }
    s[i+j-1]=mut+adt[i+j-1];
    if(s[i+j-1]!=0)
        k=i+j-1;
    else
        k=i+j-2;
    for(i=k,j=0;i>=0;i--)
    {
        mus[i]=(s[j]+‘0‘);
        j++;
    }
    return 0;
}

int additive(char* a,char* b)
{
    memset(ads,0,sizeof(ads));
    int len,len1,len2;
    int i;
    int ad[1005];
    len1=strlen(a);
    len2=strlen(b);
    if(len1==len2)
    {
        len=len1;
    }
    else if(len1>len2)
    {
        len=len1;
        for(i=len;i>=len-len2;i--)
        {
            b[i]=b[i-len+len2];
        }
        for(i=len-len2-1;i>=0;i--)
        {
            b[i]=‘0‘;
        }
    }
    else if(len1<len2)
    {
        len=len2;
        for(i=len;i>=len-len1;i--)
        {
            a[i]=a[i-len+len1];
        }
        for(i=len-len1-1;i>=0;i--)
        {
            a[i]=‘0‘;
        }
    }
    int t=0;
    for(i=len-1;i>=0;i--)
    {
        ad[i]=(a[i]-‘0‘)+(b[i]-‘0‘)+t;
        t=0;
        if(ad[i]>=10)
        {
            t++;
            ad[i]=ad[i]-10;
            ads[i]=ad[i]+‘0‘;
        }
        else
        {
            ads[i]=ad[i]+‘0‘;
        }
    }
    if(t==1)
    {
        for(i=len;i>=0;i--)
        {
            ads[i]=ads[i-1];
        }
        ads[0]=‘1‘;
    }
    return 0;
}

int excel()
{
    ans[0][0]=‘1‘;
    ans[1][0]=‘1‘;
    char sum[105];
    int n;
    int i,j;
    char t[5];
    memset(sum,0,sizeof(sum));
    for(i=2;i<=100;i++)
    {
        for(j=i;j>0;j--)
        {
            multiply(ans[i-j],ans[j-1]);
            additive(mus,sum);
            strcpy(sum,ads);
        }
        strcpy(ans[i],sum);
        memset(sum,0,sizeof(sum));
    }
    jc[1][0]=‘1‘;
    for(i=2;i<100;i++)
    {
        memset(t,0,sizeof(t));
        if(i>=10)
        {
            t[0]=i/10+‘0‘;
            t[1]=i%10+‘0‘;
        }
        else
        {
            t[0]=i+‘0‘;
        }
        multiply(jc[i-1],t);
        strcpy(jc[i],mus);
    }
    multiply(jc[99],"100");
    strcpy(jc[100],mus);
    for(i=1;i<=100;i++)
    {
        multiply(ans[i],jc[i]);
        strcpy(res[i],mus);
        //cout<<"res["<<i<<"]="<<res[i]<<endl;
    }
    return 0;
}

int main()
{
    int n;
    excel();
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        cout<<res[n]<<endl;
    }
    return 0;
}
时间: 2024-10-03 13:10:06

HDU 1131 Count the Trees 大数计算的相关文章

UVa 10007 &amp; hdu 1131 Count the Trees (卡特兰数)

Count the Trees Time Limit:3000MS    Memory Limit:0KB     64bit IO Format:%lld & %llu SubmitStatus Description  Count the Trees  Another common social inability is known as ACM (Abnormally Compulsive Meditation). This psychological disorder is somewh

hdu 1131 Count the Trees

卡特兰数*阶乘就是答案     第一次用java..高精度写起来好快....不过代码比较丑.. import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String []args) { Scanner cin = new Scanner(System.in); int i,ii; BigInteger []c= new BigInteger[105]; B

HDU 2424-Gary&#39;s Calculator(表达式计算+大数)

Gary's Calculator Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 950    Accepted Submission(s): 209 Problem Description Gary has finally decided to find a calculator to avoid making simple cal

hdu 1392 Surround the Trees (凸包)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7043    Accepted Submission(s): 2688 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

hdu 1086(计算几何入门题——计算线段交点个数)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7167    Accepted Submission(s): 3480 Problem Description Ma

hdu 1812 Count the Tetris polya计数

哈哈哈,简单polya,公式自己推导. 不过这题需要用到大数,很久没写Java,调了好久. import java.math.*; import java.util.*; import java.io.*; public class Main{ public static void main(String args[]){ Scanner cin=new Scanner(System.in); int n; BigInteger c; while(cin.hasNextInt()) { BigI

重温当年入门战之大数计算

大数计算: 由于编程语言提供的基本数值数据类型表示的数值范围有限,不能满足较大规模的高精度数值计算,因此需要利用其他方法实现高精度数值的计算,于是产生了大数运算. 大数计算简析:        大数计算实现的理论是,首先提取输入值赋予指定String字符串. 通过String.charAt(index)提取每一位的值,赋予int数组. 然后求相乘每一位的值和进位. 直到最后每一位都求出来. 代码实现:         1 import java.awt.*; 2 import java.awt.

hdu 1705 Count the grid(皮克定理)

题目链接:hdu 1705 Count the grid 题意: 给定一个三角形三点坐标,问三角形内有多少个坐标均为整数的点. 题解: 给定顶点坐标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积 S 和内部格点数目 n.边上格点数目 s 的关系:S = n +s/2+1 三角形两向量叉积/2=面积. 向量上整数点数为gcd(v.x,v.y)(此公式对于一条边上的结果不准确,但是三条边加在一起的和是准确的) 1 #include<bits/stdc++.h> 2 #define F(

zjuoj 3602 Count the Trees

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602 Count the Trees Time Limit: 2 Seconds      Memory Limit: 65536 KB A binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left&