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; }
相关文章推荐
- 图片上传
- 11个提问频率最高的PHP面试题
- hdoj 2516 取石子游戏
- could only be replicated to 0 nodes instead of minReplication (=1)
- JSF2.0学习笔记
- Android隐藏状态栏、导航栏
- Catch Bug
- 【poj1284-Primitive Roots】欧拉函数-奇素数的原根个数
- Linux Bash基本知识
- 通过java反射实现简单的关于MongoDB的对象关系映射(ORM).
- 使用HTML全局事件的一些功能,HTML元素,JS
- 1.8字符串- 翻转子串
- ios通过ipa快速提取里面的图片资源
- Joseph(hdu1443)
- uvalive 5873
- @property_@synthesize 配套使用
- qt LAN 通讯软件开发 进度 2016.2.4
- [转] ubuntu开启SSH服务
- C语言 乘法 大数相乘
- botnet