So, I was solving this problem: http://www.spoj.com/problems/IMMERSED/
A fantastic discovery is about to happen in the biology field, and you are part of the team making the research. The research is about measuring the cells growth in an environment oxygenless and in presence of a toxic substance. The team came to a courious hypothesis, the analyzed data tells them that: the growth, the number of days and the toxicity; are related by this formula:
P = N*NcN;
where P is the growth measured in thousands of cells.
N is the elapsed number of days.
and c is the constant that relates to the toxicity level of the experiment.
Your biology partners need to takeout some tissues from the cells when these cells reach a specific growth. They require you to write a program that tells them the exact time when this will happen, given a toxicity level and the growth required.
Input
The first line is T (1 ≤ T ≤ 40,000), the number of test cases, then T test cases follow.
Each test case is a line with 2 integers(P c) separated by a space.
P (1 ≤ P ≤ 1015)
c (1 ≤ c ≤ 5)
Output
For each test case you have to output the expected time in decimal format.
What I did was used binary search for finding the number of days as follows:
#define eps 1e-7
const double cont = 14.0;
double p,c;
double binary (double start, double end);
double func (double n);
int main (void)
{
int t;
cin>>t;
while (t != 0)
{
cin>>p>>c;
double mid,ans,start=0,end=cont;
ans = binary(start,end);
cout.precision(6);
cout<<fixed<<ans<<"\n";
t--;
}
return 0;
}
double func (double n)
{
double ret = n*pow(n,c*n);
return ret;
}
double binary (double start, double end)
{
if (start <= end)
{
double mid = start + (end-start)/2.0;
if (func(mid)-p <= eps)
return mid;
else if (func(mid)-p > eps)
return binary(start,mid);
else
return binary(mid,end);
}
}
However, on running my code, it gives incorrect answer on even the given test cases which are:
Input:
3
1 1
3 4
100 1
Output:
1.000000
1.207384
3.086308
My output (for the above input)
0.875
0.875
1.75
PS: I didn't post the libraries and all to avoid cluttering. Also, I will set it to be 6 decimal places once I get the correct value. I just want to know, is my logic incorrect or my binary search has been implemented incorrectly?
Edit: New code I submitted
double binary (double start, double end)
{
if (start <= end)
{
double mid = start + (end-start)/2.0;
double delta = func(mid) - p;
if (delta < -1*eps)
return binary(mid,end);
else if (delta > eps)
return binary(start,mid);
else
return mid;
}
}