Sunday, 10 July 2016

Google APAC 2017 University Test Round A Problem C. Jane's Flower Shop

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva ), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

#define READ freopen("C-large-practice.in", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)

int m;
vector<double> v;

double chk(double x)
{
    double val = v[0], r=x;
    for(int i=1;i<=m;i++)
    {
        val+=v[i]*r;
        r*=x;
    }
    return val;
}

int main()
{
    READ;
    WRITE;
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int tc,kk=1;
    double x;
    cin>>tc;
    while(tc--)
    {
        v.clear();
        cin>>m;
        for(int i=0;i<=m;i++)
        {
            cin>>x;
            if(!i) x*=-1.0;
            v.push_back(x);
        }
        reverse(v.begin(), v.end());

        double hi=2.0, lo=0.0, mid;
        for(int i=0; i<100; i++)
        {
            mid=(hi+lo)/2.0;
            if(chk(mid)<=0.0)
                hi=mid;
            else lo=mid;
        }
        cout<<"Case #"<<kk++<<": "<< setprecision(10)<<fixed<< mid-1.0 <<"\n";
    }
    return 0;
}

No comments:

Post a Comment