➤ Problem Link : 1010A. Fly
✅ C++ Solution :
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
bool func(double fuel,int lift[],int land[],int n,int m)
{
double d = (fuel+m)/lift[0];
fuel-=d;
if(fuel<=0)
return 0;
for(int i=1;i<n;i++)
{
d=(m+fuel)/land[i];
fuel-=d;
if(fuel<=0)
return 0;
d=(m+fuel)/lift[i];
fuel-=d;
if(fuel<=0)
return 0;
}
d=(fuel+m)/land[0];
fuel-=d;
if(fuel<0)
return 0;
return 1;
}
int main()
{
int n,m;
cin>>n>>m;
int lift[n],land[n];
for(int i=0;i<n;i++)
cin>>lift[i];
for(int i=0;i<n;i++)
cin>>land[i];
double low=0,high = pow(10,9)+1,mid;
int times=1000;
while(times--)
{
mid = low + (high-low)/2;
if(func(mid,lift,land,n,m))
high=mid;
else
low=mid;
}
if(mid>pow(10,9))
cout<<"-1";
else
cout<<setprecision(10)<<fixed<<mid;
}
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!
