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

Re:这是我的源代码,但是运行超时,请高手帮忙改一下。

Posted by huatao at 2008-03-23 10:17:46 on Problem 1737
In Reply To:这是我的源代码,但是运行超时,请高手帮忙改一下。 Posted by:huatao at 2008-03-23 10:17:19
> #include <iostream>
> #include <math.h>
> using namespace std;
> 
> long factorial(int n)
> {
> 	int i;
> 	long result = 1;
> 	for (i=1; i<=n; i++)
> 		result *= i;
> 	return result;
> }
> 
> long C(int n, int m)
> {
> 	return factorial(n) / factorial(m) / factorial(n-m);
> }
> 
> long f(int n)
> {
> 	if (n==1 || n==2)
> 		return 1;
> 	if (n==3)
> 		return 4;
> 	int m = C(n, 2);
> 	int p = pow(2, m);
> 	long result = p;
> 	int i;
> 	for (i=1; i<n; i++)    //递归
> 	{
> 		result -= C(n, i)*f(i);
> 	}
> 	return result;
> }
> 
> int main()
> {
> 	int n;
> 	cin>>n;
> 	long result;
> 	while (n!=0)
> 	{
> 		result = f(n);
> 		cout<<result<<endl;
> 		cin>>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