从零开始刷HDOJ(3)【HDOJ2899 - Strange fuction 】

本文详细解析了HDOJ2899 Strangefunction问题,通过数学方法证明了该函数在[0,100]区间内为单峰函数,并给出了使用二分法寻找最小值的具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从零开始刷HDOJ(3)【HDOJ2899 - Strange function 】

题面

Strange fuction

Time limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7321 Accepted Submission(s): 5057

Problem Description

Now, here is a function:

F(x)=6x7+8x6+7x3+5x2yx(0x100)

Can you find the minimum value when x is between 0 and 100.

Input

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)

Output

Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

Sample Input

2
100
200

Sample Output

-74.4291
-178.8534

提交传送门:Submit

翻译

​ 就是把求一下当 y 等于你输入的值时F(x)在[0, 100]上的零点。

思路

​ 其实这个函数在[0, 100]上是单峰的。

Proof:

(kxμ)=kμ(x)(μ1)

F(x)=42x6+48x5+21x2+10xy

y=ax6[0,100]y=ax5[0,100]y=ax2[0,100]y=ax[0,100]

F(x)[0,100]

​ 既然我们可以证明 F(x) 的导函数 F(x) 在定义域单调增,那么我们就可以在定义域中二分零点(具体方法参考高中数学必修一),找到的这个点就是我们要求的取值点了。最后输出 F(ans) 就好了。

代码

#include <cmath>
#include <iostream>

// F(x) = 6x7 + 8x6 + 7x3 + 5x2 – xy 

double y;

inline double f(const double x)
{
    return 6 * std::pow(x, 7) + 8 * std::pow(x, 6) + 7 * std::pow(x, 3) + 5 * std::pow(x, 2) - y * x;
}

inline double F(const double x)
{
    return 42 * std::pow(x, 6) + 48 * std::pow(x, 5) + 21 * std::pow(x, 2) + 10 * x;
}

int main(int argc, char ** argv)
{
    std::ios_base::sync_with_stdio(false);
    std::cout.setf(std::ios_base::fixed);
    std::cout.precision(4);
    double ans;
    double l = 0.0, r = 100.0;
    int times = 1000;
    int T;
    std::cin >> T;
    while (T--)
    {
        std::cin >> y;
        l = 0.0;
        r = 100.0;
        times = 10000;
        double eps = 1e-10;
        while (times--)
        {
            double mid = (l + r) / 2.0;
            if (F(mid)>y)
                r = mid;
            else
                l = mid;
        }
        std::cout << f(l) << std::endl;
    }
    std::cin.get();
    std::cin.get();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值