hdu Strange fuction

时间:2024-04-30 16:52:21

本题是一道二分题,但是要利用导数来求最小值。对原函数进行求导,得到的导函数为f(x)=42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y;因为0<=x<=100,所以当

y>46802200时,f(x)恒小于0,故F(x)单调递减。当y<46802200时,可知原函数先递减后递增,因此可利用二分找到导函数为零的x的值,再将此x带入原函数就可以得到最小值。

#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"cmath"
#define mi 1e-8
using namespace std;
double cf(double x,double y)
{
return *pow(x,)+*pow(x,)+*pow(x,)+*x*x-y*x;
}
double cff(double x)
{
return *pow(x,)+*pow(x,)+*pow(x,)+*x;
}
int main()
{
int t;
cin>>t;
while(t--){
double y;
cin>>y;
if(y>=)
{double t=cf(100.0,y);printf("%.4lf\n",t);}
else
{
double a=0.0;
double b=100.0;
while(b-a>mi)
{
double m=(b+a)/2.0;
if(cff(m)<y) a=m;
else b=m;
}
printf("%.4lf\n",cf(a,y));
}
}
return ;
}