HDU 2199 | HDU 2899 (Binary Search + mathematical problem)

Source: Internet
Author: User
Tags cmath

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));}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.