HDU 5105 Math Problem(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105

Problem Description

Here has an function:

f(x)=|a?x3+b?x2+c?x+d|(L≤x≤R)

Please figure out the maximum result of f(x).

Input

Multiple test cases(less than 100). For each test case, there will be only 1 line contains 6 numbers a, b, c, d, L and R.
(?10≤a,b,c,d≤10,?100≤L≤R≤100)

Output

For each test case, print the answer that was rounded to 2 digits after decimal point in 1 line.

Sample Input

1.00 2.00 3.00 4.00 5.00 6.00

Sample Output

310.00

Source

BestCoder Round #18

PS:

分a等于零 和a不等于零的情况分别求导讨论!

代码如下:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
    double a, b, c, d, L, R;
    while(~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&L,&R))
    {
        double ansL = 0, ansR = 0;
        double maxans = 0;
        if(a != 0)
        {
            double t = 4*b*b-12*a*c;
            ansL = a*L*L*L+b*L*L+c*L+d;
            if(ansL < 0)
                ansL = -ansL;
            ansR = a*R*R*R+b*R*R+c*R+d;
            if(ansR < 0)
                ansR = -ansR;
            maxans = max(ansL,ansR);
            if(t >= 0)
            {
                double x1 = (-2*b+sqrt(t))/(6*a);
                double t1 = a*x1*x1*x1+b*x1*x1+c*x1+d;
                if(t1 < 0)
                    t1 = -t1;
                maxans = max(maxans,t1);
                double x2 = (-2*b-sqrt(t))/(6*a);
                double t2 = a*x2*x2*x2+b*x2*x2+c*x2+d;
                if(t2 < 0)
                    t2 = -t2;
                maxans = max(maxans,t2);
            }
        }
        else if(a == 0)
        {
            if(b != 0)
            {
                double tt = -c/(2*b);
                double t1 = a*tt*tt*tt+b*tt*tt+c*tt+d;
                if(t1 < 0)
                    t1 = -t1;
                maxans = t1;
            }
            double t1 = a*L*L*L+b*L*L+c*L+d;
            if(t1 < 0)
                t1 = -t1;
            maxans = max(maxans,t1);
            double t2 = a*R*R*R+b*R*R+c*R+d;
            if(t2 < 0)
                t2 = -t2;
            maxans = max(maxans,t2);
        }
        printf("%.2lf\n",maxans);
    }
    return 0;
}
时间: 2024-11-05 19:33:45

HDU 5105 Math Problem(数学)的相关文章

hdu 5105 Math Problem(数学)

题目链接:hdu 5105 Math Problem 题目大意:给定a,b,c,d,l,r,表示有一个函数f(x)=|a?x3+b?x2+c?x+d|(L≤x≤R),求函数最大值. 解题思路:考虑极点即可,将函数求导后得到f′(x)=0的x,即为极值点.在极值点处函数的单调性会发生变化,所以最大值一定就在区间边界和极值点上.注意a=0,b=0的情况,以及极值点不在区间上. #include <cstdio> #include <cstring> #include <cmath

HDU 5105 Math Problem --数学,求导

官方题解: f(x)=|a∗x3+b∗x2+c∗x+d|, 求最大值.令g(x)=a∗x3+b∗x2+c∗x+d,f(x)的最大值即为g(x)的正最大值,或者是负最小值.a!=0时, g′(x)=3∗a∗x2+2∗b∗x+c 求出g′(x)的根(若存在,x1,x2,由导数的性质知零点处有极值.ans=max(f(xi)|L≤xi≤R).然后考虑两个端点的特殊性有ans=max(ans,f(L),f(R)). 当时 x = -c/(2*b) 写成 x = -c/2*b 了,然后过pretest了.

HDU 5105 Math Problem

让求  f(x)=|a∗x3+b∗x2+c∗x+d|(L≤x≤R)的最大值 这个题目讨论a和b的值,如果a==0的话,那么这个方程就变成了一个一元二次方程,直接找端点和对称轴(如果对称轴在给定的区间内)处的函数值就行,如果a != 0,那么求导,求导之后判断二次方程的delta,如果delta小于等于0,说明是单调的,那么最值还是端点处取到,如果delta大于0, 那么就要比较两个极点(如果极点在给定的区间内)处的值和端点值的大小就行了. /***************************

hdu 6182A Math Problem(快速幂)

You are given a positive integer n, please count how many positive integers k satisfy kk≤nkk≤n. InputThere are no more than 50 test cases. Each case only contains a positivse integer n in a line. 1≤n≤10^18OutputFor each test case, output an integer i

hdu 5170 GTY&#39;s math problem(水,,数学,,)

题意: 给a,b,c,d. 比较a^b和c^d的大小 思路: 比较log(a^b)和log(c^d)的大小 代码: int a,b,c,d; int main(){ while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){ double x1 = b*log((double)a); double x2 = d*log((double)c); if(fabs(x1-x2)<eps){ puts("=")

hdu 1757 A Simple Math Problem (乘法矩阵)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =

HDU 5615 Jam&#39;s math problem

Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is trying to factorize ax^2+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m).He could only solve the problem in which p,q,m,k are positive numbers.Ple

BestCoder Round #70 Jam&#39;s math problem(hdu 5615)

Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ax^2+bx+cax?2??+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m)pqx?2??+(qk+mp)x+km=(px+k)(qx+m). He could only solve the problem in which p,q,m,

hdu 5615 Jam&#39;s math problem(十字相乘判定)

d. Jam有道数学题想向你请教一下,他刚刚学会因式分解比如说,x^2+6x+5=(x+1)(x+5) 就好像形如 ax^2+bx+c => pqx^2+(qk+mp)x+km=(px+k)(qx+m) 但是他很蠢,他只会做p,q,m,kp,q,m,k为正整数的题目 请你帮助他,问可不可以分解 题意就是问一个一元二次方程能不能进行十字相乘的分解? s. 官方题解:第一道题比较简单,可以说是简单的模拟题,我们考虑到a,b,c都是10^9??的,所以我们决定要把时间复杂度降下来, 对于每一个数,因为