HDU 2899 Strange fuction

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2899 Strange fuction相关的知识,希望对你有一定的参考价值。

Strange fuction

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


Problem Description
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.
 

 

Input
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)
 

 

Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
 

 

Sample Input
2
100
200
 

 

Sample Output
-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‘(x)=0。这个点是F(x)的极小值点,就是F(x)在区间(0,100)的最小值点。可以用二分法求出x0,代入F(x)即可。
 
 
 
 1 #include <cstdio>
 2 #include <cmath>
 3 
 4 double f(double x,double y)
 5 {
 6     return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
 7 }
 8 
 9 double f1(double x,double y)
10 {
11     return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y;
12 }
13 
14 int main()
15 {
16     int T,y;
17     scanf("%d",&T);
18     while(T--){
19         scanf("%d",&y);
20         double low = 0,high = 100;
21         while(high-low>1e-6){
22             double mid = (low+high)/2;
23             if(f1(mid,y)<0)
24                 low = mid+1e-8;
25             else
26                 high = mid-1e-8;
27         }
28         double x0 = (low+high)/2;
29         printf("%.4f\n",f(x0,y));
30     }
31     return 0;
32 }

 

 

 

本题也可用三分。

 1 #include <cstdio>
 2 #include <cmath>
 3 
 4 double f(double x,double y)
 5 {
 6     return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
 7 }
 8 
 9 int main()
10 {
11     int T,y;
12     scanf("%d",&T);
13     while(T--){
14         scanf("%d",&y);
15         double low = 0,high = 100;
16         double lowmid,highmid;
17         while(high-low>1e-6){
18             lowmid = (2*low+high)/3;
19             highmid = (low+2*high)/3;
20             if(f(lowmid,y)<f(highmid,y))
21                 high = highmid-1e-8;
22             else
23                 low = lowmid+1e-8;
24         }
25         printf("%.4f\n",f(low,y));
26     }
27     return 0;
28 }

 

以上是关于HDU 2899 Strange fuction的主要内容,如果未能解决你的问题,请参考以下文章

HDU2899 Strange fuction 二分

HDU 2899 strange fuction

hdu 2899 Strange fuction——模拟退火

hdu2899Strange fuction(解方程+二分)

HDU 2899 Strange fuction(牛顿迭代)

HDU 2899 Strange fuction 二分