I am trying to find a cubic root(t) for the following equation, constraints
F(t)=A(t^3)+B(t^2)+C*(t)+D, F(t)<=10^18.
Help find t such that F(t+1) > K, and F(t) <= K
I have tried the following approach;
What I did is followed binary search approach.Intially put t value as k/2 in the equation. If the result value is greater than k, I will try with t value k/4.Else if result less than k, I will check wit t value k/4. The program seems works with smaller values of k but the problem is the values exceeding the range for larger k. I guess I should pickup a better pivot value(intiall value) or change some equation
int a,b,c,d;
long long int k,x,y;
long long int i,f,temp1;
int def;
def=0;
cin>>a>>b>>c>>d>>k;
i=1;
f=k;
temp1=(1+k)/2;
while((temp1>=1)&&(temp1<=k))
{
x=a*(temp1+1)*(temp1+1)*(temp1+1)+b*(temp1+1)*(temp1+1)+c*(temp1+1)+d;
y=a*(temp1)*(temp1)*(temp1)+b*temp1*temp1+c*temp1+d;
if((x>k)&&(y<=k))
{
cout<<temp1<<endl;
def=1;
break;
}
else if(x<k)
{
i=temp1;
temp1=(f+temp1)/2;
}
else if(x>k)
{
f=temp1;
temp1=(i+temp1)/2;
}
}
if(def==0)
cout<<def<<endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire