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

最好来个记忆化。以下代码绝不是因为超时而不过的。

Posted by fanhqme at 2008-10-01 16:35:29 on Problem 2696
In Reply To:哎,超时真麻烦,那位大牛给点思路下面是我的代码,谢谢啊 Posted by:ZhangJunhua at 2008-07-26 09:42:18
> #include<iostream>
> using namespace std;
int used[1001],dp[1001];
> int p(int a,int b,int c,int d,int e,int f,int g,int h,int i)
> {
        if (used[i])return dp[i];
> 	int m,n;
> 	if(i==0)
> 		return used[i]=1,dp[i]=a;
> 	if(i==1)
> 		return used[i]=1,dp[i]=b;
> 	if(i==2)
> 		return used[i]=1,dp[i]=c;
> 	if(i>=3)
> 	{
> 		if(i%2!=0)
> 		{
> 			m=d*p(a,b,c,d,e,f,g,h,i-1)+e*p(a,b,c,d,e,f,g,h,i-2)-f*p(a,b,c,d,e,f,g,h,i-3);
> 			if(m>=0)
> 				return used[i]=1,dp[i]=m%g;
> 			else return used[i]=1,dp[i]=g+m%g;
> 		}
> 		else
> 		{
> 			n=f*p(a,b,c,d,e,f,g,h,i-1)-d*p(a,b,c,d,e,f,g,h,i-2)+e*p(a,b,c,d,e,f,g,h,i-3);
> 			if(n>=0)
> 				return used[i]=1,dp[i]=n%h; 
> 			else
> 				return used[i]=1,dp[i]=n%h+h;
> 		}
> 	}
> }
> int main()
> {
> 	int n;
> 	cin>>n;
> 	int k;
> 	int a,b,c,d,e,f,g,h,i;
> 	for(k=0;k<n;k++)
> 	{
                for (int z=0;z<1001;z++)used[z]=0;
> 		cin>>a>>b>>c>>d>>e>>f>>g>>h>>i;
> 		cout<<p(a,b,c,d,e,f,g,h,i)<<endl;
> 	}
> 	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