poj 2084 Game of Connections

来源:互联网 发布:图形图像处理的软件 编辑:程序博客网 时间:2024/06/05 23:31

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

23-1

Sample Output

25

分析出来是Catalan数容易,但是注意数据量,上限是100,不做处理的话肯定超范围,先用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 cur,i,j;BigInteger[] k=new BigInteger[102];k[0]=BigInteger.valueOf(1);for (i=1;i<=100;i++){k[i]=BigInteger.valueOf(0);for (j=0;j<i;j++)k[i]=k[i].add(k[j].multiply(k[i-j-1]));}while (cin.hasNext()){cur=cin.nextInt();if (cur!=-1)System.out.println(k[cur]);elsebreak;}cin.close();}}


很容易是不,现在用c语言来处理,就是大数乘法与大数除法交叉来实现

#include<iostream>#include<string.h>#include<stdio.h>#include<algorithm>using namespace std;#define BASE 10000int a[101][101];void multiply(int k,int num){               //乘法    int ans=0;    for (int i=100;i>=0;i--)    {        ans+=a[k-1][i]*num;        a[k][i]=ans%BASE;        ans/=BASE;    }}void devide(int k,int num){               //除法    int div=0;    for (int i=0;i<=100;i++)    {        div=div*BASE+a[k][i];        a[k][i]=div/num;        div%=num;    }}void init(){    memset(a,0,sizeof(a));    a[1][100]=1;    a[2][100]=2;    a[3][100]=5;    int num;    for (int i=4;i<=100;i++)    {                   //公式h(n)=h(n-1)*(4*n-2)/(n+1);        num=(4*i-2);        multiply(i,num);        num=i+1;        devide(i,num);    }}int main(){    int n;    init();    while (~scanf("%d",&n)&&n!=-1)    {        int pos;        for (int i=100;i>=0;i--)            if (!a[n][i])           //倒着遍历,因为数字长度不是太多,毕竟只是到n的上限是100            {                pos=i+1;                break;            }            printf("%d",a[n][pos]);            for(int i=pos+1;i<=100;i++)                printf("%04d",a[n][i]);        //保证与base的长度相同,因为可能会有0在里边                printf("\n");    }    return 0;}

还有一种是用普通的大数加法,然后把大数乘法拆分成大数加法,但是这样处理会超时,其实可以先这样处理,然后输出到文件,用数组存储,这样就非常快了

#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;string add(string a,string b){    int i,j,k,flag;    string c;    c="";    i=a.size()-1,j=b.size()-1;    k=0;flag=0;    while (i>=0&&j>=0)    {        c+=(a[i]-'0'+b[j]+flag);        flag=0;                     //重新赋值        if (c[k]>'9')        {            flag=1;            c[k]=c[k]-10;        }        i--,j--,k++;    }    while (i>=0)    {        c+=(a[i]+flag);        flag=0;        if (c[k]>'9')        {            flag=1;            c[k]=c[k]-10;        }        i--,k++;    }    while (j>=0)    {        c+=(b[j]+flag);        flag=0;        if (c[k]>'9')        {            flag=1;            c[k]=c[k]-10;        }        j--,k++;    }    char temp;    if (flag)       {           c+='1';           k++;       }        for (i=0,j=k-1;i<j;i++,j--)        {            temp=c[i];            c[i]=c[j];            c[j]=temp;        }        return c;}string mult(string a,string b){    int flag,i,j,p,q,t,k;    char temp;    string c,ans;    ans="0";    p=a.size();    q=b.size();    for (i=p-1;i>=0;i--)    {                  //将p拆开         c="";         flag=0;        for (j=i;j<p-1;j++)            c+='0';        for (j=q-1;j>=0;j--)        {            t=(b[j]-'0')*(a[i]-'0')+flag;            flag=t/10;            c+=(t%10+'0');        }        if (flag)            c+=(flag+'0');        for (j=0,k=c.size()-1;j<k;j++,k--)        {            temp=c[j];            c[j]=c[k];            c[k]=temp;        }        ans=add(ans,c);    }    return ans;}int main(){    int i,j,n;    string a,ans[101];    ans[0]="1";    ans[1]="1";    ans[2]="2";    ans[3]="5";    for (i=4;i<101;i++)        ans[i]="0";    for (i=4;i<101;i++)    {        for (j=0;j<i;j++)        {            a=mult(ans[j],ans[i-j-1]);            ans[i]=add(ans[i],a);        }    }    while (cin>>n&&n!=-1)        cout<<ans[n]<<endl;    return 0;}





0 0
原创粉丝点击