你的支持是我最大的动力,你的意见是我前进的导航。
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 |
Author
Redow
|
Recommend
lcy
|
此题大意:F(x) = 6*x^7+8*x^6+7*x^3+5*x^2-y*x, x ∈ [0, 100], 对于y ∈ (0, 10^10),求F(x)的最小值
首先,对该式子,求一阶导,得到F'(x) = 42*x^6 + 48*x^5+21*x^2+10*x-y,可以发现,抛开y不管,那么42*x^6 + 48*x^5+21*x^2+10*x是递增的,范围在[0, 10^13],所以我们可以发现无论y取什么值,总可以找到x1∈[0, 100],使F'(x1)=0,又因为此时x∈[0, x1]时,函数递减;x∈[x1, 100]时,函数递增。所以F(x1)便是极小值,也是最小值。
综上,先对F'(x)使用二分法即可,再输出F(x)的值,便是最小值。
代码如下
1 //author: mission 2 #include <stdio.h> 3 #include <math.h> 4 double y; 5 double df(double x) // f'(x) 6 { 7 return 42 * pow(x, 6) + 48 * pow(x, 5) + 21 * pow(x, 2) + 10 * x - y; 8 } 9 double f(double x) //f(x) 10 { 11 return 6 * pow(x, 7) + 8 * pow(x, 6) + 7 * pow(x, 3) + 5 * pow(x, 2) - y * x; 12 } 13 int main() 14 { 15 int T; 16 double x1, x2, x3, y1, y2, y3; 17 scanf("%d", &T); 18 while (T--) 19 { 20 scanf("%lf", &y); 21 x1 = 0; 22 x2 = 100; 23 y1 = df(x1); 24 y2 = df(x2); 25 //dichotomy 26 while (fabs(x1 - x2) > 1e-6) 27 { 28 x3 = (x1 + x2) / 2; 29 y3 = df(x3); 30 if (y3 > 0) 31 x2 = x3; 32 else 33 x1 = x3; 34 } 35 printf("%.4f\n", f(x3)); 36 } 37 return 0; 38 }
1e-6