poj 2084 Game of Connections

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

分析出来是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]);
			else
				break;
		}
		cin.close();
	}

}


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

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define BASE 10000
int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值