HDU 1131 Count the Trees large number calculation, hdu1131

Source: Internet
Author: User

HDU 1131 Count the Trees large number calculation, hdu1131

The question is to give a number, and then use 1 to this number as the node of the binary tree. There are several methods to form the binary tree. This question is to use the catlan number to calculate the number, and then because there is a number, different numbers correspond to different methods, so the result is the catlan number multiplication method of this number. Because the numbers are relatively large, we need to use a high-precision method, that is, using character arrays. I have written three functions, one for addition, one for multiplication, And the last for table creation, when the table is typed, you only need to judge the number of input items and directly output the data. below is my code. The first time I write this type of big data processing with high precision, it may seem complicated =

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.