Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
最好来个记忆化。以下代码绝不是因为超时而不过的。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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator