HDU 2899 Strange fuction 牛顿迭代法 || 二分
2017-08-14 21:10
531 查看
题目:
http://acm.hdu.edu.cn/showproblem.php?pid=2899题意:
给定方程 F(x)=6∗x7+8∗x6+7∗x3+5∗x2−y∗x(0<=x<=100),求解F(x)在定义域内的最小值思路:
牛顿迭代法可解,求出导函数的零点。用二分法求的话,求导之后可以发现,函数的导函数在定义域内是单调的,那么这意味着F(x)的最小值最多有三种可能,min(f(0),f(100),f(x)),这个x是导函数的零点,可能不存在,因此二分求出导函数的零点,取三者中的最小值即可,但就这个题而言,经过分析可以发现,导函数零点处函数一定是最小值。最后还有一个很久之前我写的代码,每次取十个值,把区间分成十一个区间,找到这十个值之中的最小值,用这个最小值两侧的区间作为当前区间,接着十一分,知道求出答案,这个方法大概是意淫出来的。。。牛顿迭代法:
#include <bits/stdc++.h> using namespace std; const int N = 1000 + 10, INF = 0x3f3f3f3f; const double eps = 1e-8; double y; double ddf(double x) { return 42*6*pow(x, 5) + 48*5*pow(x, 4) + 21*2*pow(x, 1) + 10; } double df(double x) { return 42*pow(x, 6) + 48*pow(x, 5) + 21*pow(x, 2) + 10*pow(x, 1) - y; } double f(double x) { return 6*pow(x, 7) + 8*pow(x, 6) + 7*pow(x, 3) + 5*pow(x, 2) - y*x; } double newton_iteration(double x) { int tot = 0; while(fabs(df(x)) > eps) { x = x - df(x) / ddf(x); if(++tot > 50) return -1; } return x; } int main() { int t; scanf("%d", &t); while(t--) { scanf("%lf", &y); double ans = INF; for(int i = 0; i <= 100; i++) { double tmp = newton_iteration(i); if(tmp >= 0 && tmp <= 100) { ans = min(ans, f(tmp)); } } printf("%.4f\n", ans); } return 0; }
二分法:
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; double y; double df(double x) { return 42*pow(x, 6) + 48*pow(x, 5) + 21*pow(x, 2) + 10*pow(x, 1) - y; } double f(double x) { return 6*pow(x, 7) + 8*pow(x, 6) + 7*pow(x, 3) + 5*pow(x, 2) - y*x; } int main() { int t; scanf("%d", &t); while(t--) { scanf("%lf", &y); double l = 0.0, r = 100.0, x = -1; for(int i = 1; i <= 100; i++) { double mid = (l + r) / 2; if(df(mid) >= 0) x = mid, r = mid; else l = mid; } double ans = 1e9; if(fabs(df(x) - 0) < eps) ans = min(ans, f(x)); //ans = min(ans, min(f(0), f(100))); printf("%.4f\n", ans); } return 0; }
意淫的方法:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector> using namespace std; const double eps = 1e-8; double y; struct node { double i, d; }s[20]; double ans(double x) { return 6*x*x*x*x*x*x*x + 8*x*x*x*x*x*x + 7*x*x*x + 5*x*x - y*x; } void ser() { double l = 0.0, r = 100.0; double res; for(int i = 0; i < 100; i++) { for(int j = 0; j <= 10; j++) { s[j].i = (r - l) * j / 10 + l; s[j].d = ans(s[j].i); } double tmp = s[0].d; for(int j = 1; j <= 10; j++) if(tmp - s[j].d > eps) tmp = s[j].d; res = tmp; int tmp1; for(int j = 0; j <= 10; j++) if(fabs(tmp - s[j].d) < eps) tmp1 = j; if(tmp1 == 0) l = s[0].i, r = s[1].i; else if(tmp1 == 10) l = s[9].i, r = s[10].i; else l = s[tmp1-1].i, r = s[tmp1+1].i; } printf("%.4f\n", res); } int main() { int t; scanf("%d", &t); while(t--) { scanf("%lf", &y); ser(); } return 0; }
相关文章推荐
- HDU 2899 Strange fuction (二分)
- HDU - 2899 Strange fuction 二分/三分
- HDU 2899 Strange fuction 二分求方程解
- hdu 2899 Strange fuction(导数+二分)
- HDU--杭电--2899--Strange fuction--二分
- hdu 2899 Strange fuction (二分)
- hdu 2899 Strange fuction(二分+数学)
- hdu 2899 Strange fuction 二分
- HDU 2899 Strange fuction 二分 + 精度控制
- hdu 2899 Strange fuction (求导 + 二分)
- HDU 2899 Strange fuction(二分||三分)
- HDU 2899 Strange fuction(二分或三分)
- hdu 2899 Strange Fuction(二分)
- hdu 2899 Strange fuction 【二分,三分】
- HDU—2899—strange fuction—【二分】【精度控制】
- hdu - 2899 - Strange fuction(二分)
- HDU 2899 Strange fuction(二分查找根)
- hdu 2899 Strange fuction (二分)
- Hdu 2899 Strange fuction(二分三分可做,模拟退火解法)
- HDU 2899 Strange fuction(二分,三分)