fork download
  1.  
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define pb push_back
  5. int n,m;
  6. double a[1005];
  7. double b[1005];
  8. double fun(double x) /// x is the initial fuel
  9. {
  10. double prev=x;
  11. double curr;
  12. double used;
  13. for(int i=0;i<n;i++)
  14. {
  15. used=1.0*(m+prev)/a[i];
  16. prev=prev-used;
  17. used=1.0*(m+prev)/b[i];
  18. prev=prev-used;
  19. }
  20. if(prev<0)
  21. return 0;
  22. return 1;
  23.  
  24. }
  25. int main()
  26. {
  27. cin>>n;
  28. cin>>m;
  29. for(int i=0;i<n;i++)
  30. {
  31. cin>>a[i];
  32. }
  33. for(int i=0;i<n;i++)
  34. {
  35. cin>>b[i];
  36. }
  37. double lo=0.0,hi=1000000000.000001,ans=-1,mid;
  38. while(lo<=hi)
  39. {
  40. mid=1.0*(lo+hi)/2;
  41. if(fun(mid))
  42. {
  43. ans=mid;
  44. hi=mid-0.000001;
  45. }
  46. else
  47. {
  48. lo=mid+0.000001;
  49. }
  50. }
  51. if(ans<0)
  52. {
  53. cout<<-1<<endl;
  54. return 0;
  55. }
  56. cout<<fixed<<setprecision(6)<<ans<<endl;
  57. }
Success #stdin #stdout 0s 4536KB
stdin
Standard input is empty
stdout
0.000001