题意:
是有n个星球,1代表地球,然后输入一个m表示火箭的重量,然后输入了两组n个数,第一组表示在每个星球起飞时消耗一吨燃料的质量数,a[i]就表示每a[i]吨就要消耗1吨燃料,第二组就表示在每个星球降落时消耗一吨燃料的质量数,然后问当火箭从1飞到2到3....到n星球后又返回1星球最少需要加多少燃料。
思路:
二分答案,注意double二分的写法,以及如何控制出口
代码:
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
#include<list> #define fst first
#define sc second
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x))
#pragma Gcc optimize(2) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const int maxn = + ;
const int maxm = 5e3 + ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
int scan(){
int res=,ch,flag=;
if((ch=getchar())=='-')
flag=;
else if(ch>=''&&ch<='')
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return flag?-res:res;
} double a[maxn], b[maxn];
int n, m;
bool ok(double ans){
for(int i = ; i <= n; i++){
ans -= (m+ans)/a[i];
ans -= (m+ans)/b[i+];
}
if(ans < ) return false;
else return true;
}
int main(){
scanf("%d",&n);
scanf("%d", &m); for(int i = ; i <= n; i++){
scanf("%lf", &a[i]);
if(a[i] <= ){
printf("-1");
return ;
}
}
for(int i = ; i <= n; i++){
scanf("%lf", &b[i]);
if(b[i] <= ){
printf("-1");
return ;
}
}b[n+] = b[];
double ans = ;
double l = , r = ;
for(int i = ; i <= ; i++){
if(r-l < 1e-) break;
double mid = (l+r)/2.0;
if(ok(mid))r = mid;
else l = mid;
}
printf("%.10lf", l);
return ;
}
/*
*/