Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2199
/*
Binary search;
*/
#include <iostream>#include <cmath>using namespace std;double fun(double x){ return 8*pow(x,4.0)+7*pow(x,3.0)+2*pow(x,2.0)+3*x+6;}double l,m,r,y;int main(){ int t; scanf("%d",&t); while(t--) { scanf("%lf",&y); if(fun(0)<=y && y<=fun(100)) { l = 0,r = 100; while(r-l > 1e-6) { m = (l+r)/2; double ans = fun(m); if(ans > y) { r = m - 1e-7; } else { l = m + 1e-7; } } m = (l+r)/2.0; printf("%.4lf\n",m); } else { puts("No solution!"); } }}
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2899
/*
The function is F (x) = 6 * x ^ 7 + 8 * x ^ 6 + 7 * x ^ 3 + 5 * x ^ 2-y * x;
Then its function is f' (x) = 42 * x ^ 6 + 48 * x ^ 5 + 21 * x ^ 2 + 10 * x-y;
Design the function and calculate the value of the function G' (x) = 42 * x ^ 6 + 48 * x ^ 5 + 21 * x ^ 2 + 10 * x, subtract the input y value;
It can be seen that the G' (x) function is a monotonic increasing function;
If for 100, g' (x)-y <= 0, then for any number [0-100] and G' (x) both <= 0, F (100) minimum value of the entire function;
Otherwise, we perform binary search. Code:
*/
#include <iostream>#include <cmath>using namespace std;double l,m,r,y;double g(double x){return 42*pow(x,6.0)+48*pow(x,5.0)+21*pow(x,2.0)+10*x;}double f(double x){return 6.0*pow(x,7.0)+8.0*pow(x,6.0)+7.0*pow(x,3.0)+5.0*pow(x,2.0)-y*x;}int main(){int t;scanf("%d",&t);while(t--){scanf("%lf",&y);if(g(100.0)-y>0){l = 0,r = 100;while(r-l > 1e-8){m = (l+r)/2;if(g(m) > y)r = m - 1e-7;elsel = m + 1e-7;}m = (l+r)/2.0;printf("%.4lf\n",f(m));}elseprintf("%.4lf\n",f(100.0));}}