Tanky WooRSS

HDU/HDOJ 2899 Strange fuction

01 Feb 2011
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2899

好题,很好的题,非常好的题!

再次感受到了,算法的世界还有数学!

分析:求导算单调性,然后二分。

代码:

#include 
#include 
#include 
using namespace std;

double y = 0;
double cal(double x)
{
    return 42.0*pow(x,6.0) + 48.0*pow(x,5.0) + 21.0*pow(x,2.0) + 10*x - y;
}

double ans(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;
    cin >> T;
    while(T--)
    {
        cin >> y;
        if(cal(100.0) <= 0.0)
        {
            cout << fixed << setprecision(4) << ans(100.0) << endl;
            continue;
        }

        double l=0.0, r=100.0, mid;
        while(r-l > 1e-6)
        {
            mid = (l+r)/2.0;
            double mid = (l+r)/2.0;
            if(cal(mid) < 0.0)
                l = mid;
            else
                r = mid;
        }
        cout << fixed << setprecision(4) << ans(mid) << endl;

    }
    return 0;
}