您的位置:首页 > 其它

HDU 2899 Strange fuction

2016-02-04 22:24 316 查看

Strange fuction

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5253 Accepted Submission(s): 3750


[align=left]Problem Description[/align]
Now, here is a fuction:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.

[align=left]Input[/align]
The
first line of the input contains an integer T(1<=T<=100) which
means the number of test cases. Then T lines follow, each line has only
one real numbers Y.(0 < Y <1e10)

[align=left]Output[/align]
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

[align=left]Sample Input[/align]

2

100

200

[align=left]Sample Output[/align]

-74.4291
-178.8534

解析:要求F(x)=6*x^7+8*x^6+7*x^3+5*x^2-y*x(0<=x<=100)的最小值,可以求F(x)的导数F'(x)来研究F(x)。易知:对于任意的y(0<y<1e10),F'(0)<0,F'(100)>0。而F'(x)在区间(0,100)上单调递增(因为F(x)的二阶导数F''(x)在区间(0,100)上恒大于0),故在区间(0,100)上必存在x0,使得F'(x0)=0。这个点是F(x)的极小值点,就是F(x)在区间(0,100)的最小值点。可以用二分法求出x0,代入F(x)即可。

#include <cstdio>
#include <cmath>

double f(double x,double y)
{
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}

double f1(double x,double y)
{
return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y;
}

int main()
{
int T,y;
scanf("%d",&T);
while(T--){
scanf("%d",&y);
double low = 0,high = 100;
while(high-low>1e-6){
double mid = (low+high)/2;
if(f1(mid,y)<0)
low = mid+1e-8;
else
high = mid-1e-8;
}
double x0 = (low+high)/2;
printf("%.4f\n",f(x0,y));
}
return 0;
}


本题也可用三分。

#include <cstdio>
#include <cmath>

double f(double x,double y)
{
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}

int main()
{
int T,y;
scanf("%d",&T);
while(T--){
scanf("%d",&y);
double low = 0,high = 100;
double lowmid,highmid;
while(high-low>1e-6){
lowmid = (2*low+high)/3;
highmid = (low+2*high)/3;
if(f(lowmid,y)<f(highmid,y))
high = highmid-1e-8;
else
low = lowmid+1e-8;
}
printf("%.4f\n",f(low,y));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: