Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

递推的方法做的,本来觉得可能超时,结果79ms,速度还可以

Posted by TonyChang at 2012-03-04 13:40:10 on Problem 2084
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N=100+10; 
struct Big
{
	int s[6*N], len;
	void init()
	{
		memset(s,0,sizeof(s));
		len=0;
	}
};
Big arr[210];
int nnum;

Big operator + (Big a, Big b)
{
	Big c;
	c.init();
	c.len=max(a.len,b.len);
	for(int i=1;i<=c.len;i++)
	{
		c.s[i]=c.s[i]+a.s[i]+b.s[i];
		if(c.s[i]>=10)
		{
			c.s[i]-=10;
			c.s[i+1]++;
		}
	}
	if(c.s[c.len+1])
		c.len++;
	return c;
}

Big operator * (Big a, Big b)
{
	Big c;
	c.init();
	for(int i=1;i<=a.len;i++)
	{
		int j;
		for(j=1,c.len=i-1;j<=b.len;j++)
		{
			//c.len=i-1;
			c.s[++c.len]=c.s[c.len]+a.s[i]*b.s[j];
		}
	}
	for(int i=1;i<=c.len;i++)
	{
		if(c.s[i]>=10)
			c.len++;
		c.s[i+1]=c.s[i+1]+c.s[i]/10;
		c.s[i]=c.s[i]%10;
	}
	return c;
}

void work()
{
	for(int i=0;i<=200;i++)
		arr[i].init();
	arr[0].s[1]=1, arr[0].len=1;
	arr[1].s[1]=1, arr[1].len=1;
	for(int i=2;i<=101;i++)
	{
		for(int j=0;j<=i-1;j++)
		{
			arr[i]=arr[i]+arr[j]*arr[i-1-j];
		}
	}
}

int main()
{
	work();
	while(~scanf("%d",&nnum) && nnum!=-1)
	{
		//printf("%d\n",arr[nnum].len);
		int i=arr[nnum].len;
		while(arr[nnum].s[i]==0)
			i--, arr[nnum].len--;
		for(int i=arr[nnum].len;i>=1;i--)
			printf("%d",arr[nnum].s[i]);
		printf("\n");
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator