欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

codeforces 1010A Fly

程序员文章站 2024-03-17 15:30:16
...

codeforces 1010A Fly

原题地址:https://codeforces.com/problemset/problem/1010/A

题目

Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on n−2 intermediate planets. Formally: we number all the planets from 1 to n. 1 is Earth, n is Mars. Natasha will make exactly n flights: 1→2→…n→1.

Flight from x to y consists of two phases: take-off from planet x and landing to planet y. This way, the overall itinerary of the trip will be: the 1-st planet → take-off from the 1-st planet → landing to the 2-nd planet → 2-nd planet → take-off from the 2-nd planet → … → landing to the n-th planet → the n-th planet → take-off from the n-th planet → landing to the 1-st planet → the 1-st planet.

The mass of the rocket together with all the useful cargo (but without fuel) is m tons. However, Natasha does not know how much fuel to load into the rocket. Unfortunately, fuel can only be loaded on Earth, so if the rocket runs out of fuel on some other planet, Natasha will not be able to return home. Fuel is needed to take-off from each planet and to land to each planet. It is known that 1 ton of fuel can lift off ai tons of rocket from the i-th planet or to land bi tons of rocket onto the i-th planet.

For example, if the weight of rocket is 9 tons, weight of fuel is 3 tons and take-off coefficient is 8 (ai=8), then 1.5 tons of fuel will be burnt (since 1.5⋅8=9+3). The new weight of fuel after take-off will be 1.5 tons.

Please note, that it is allowed to burn non-integral amount of fuel during take-off or landing, and the amount of initial fuel can be non-integral as well.

Help Natasha to calculate the minimum mass of fuel to load into the rocket. Note, that the rocket must spend fuel to carry both useful cargo and the fuel itself. However, it doesn’t need to carry the fuel which has already been burnt. Assume, that the rocket takes off and lands instantly.

Input
The first line contains a single integer n (2≤n≤1000) — number of planets.

The second line contains the only integer m (1≤m≤1000) — weight of the payload.

The third line contains n integers a1,a2,…,an (1≤ai≤1000), where ai is the number of tons, which can be lifted off by one ton of fuel.

The fourth line contains n integers b1,b2,…,bn (1≤bi≤1000), where bi is the number of tons, which can be landed by one ton of fuel.

It is guaranteed, that if Natasha can make a flight, then it takes no more than 109 tons of fuel.

Output
If Natasha can fly to Mars through (n−2) planets and return to Earth, print the minimum mass of fuel (in tons) that Natasha should take. Otherwise, print a single number −1.

It is guaranteed, that if Natasha can make a flight, then it takes no more than 109 tons of fuel.

The answer will be considered correct if its absolute or relative error doesn’t exceed 10−6. Formally, let your answer be p, and the jury’s answer be q. Your answer is considered correct if |p−q|max(1,|q|)≤10−6.

Examples
inputCopy
2
12
11 8
7 5
outputCopy
10.0000000000
inputCopy
3
1
1 4 1
2 5 3
outputCopy
-1
inputCopy
6
2
4 6 3 3 5 6
2 6 3 6 5 3
outputCopy
85.4800000000
Note
Let’s consider the first example.

Initially, the mass of a rocket with fuel is 22 tons.

At take-off from Earth one ton of fuel can lift off 11 tons of cargo, so to lift off 22 tons you need to burn 2 tons of fuel. Remaining weight of the rocket with fuel is 20 tons.
During landing on Mars, one ton of fuel can land 5 tons of cargo, so for landing 20 tons you will need to burn 4 tons of fuel. There will be 16 tons of the rocket with fuel remaining.
While taking off from Mars, one ton of fuel can raise 8 tons of cargo, so to lift off 16 tons you will need to burn 2 tons of fuel. There will be 14 tons of rocket with fuel after that.
During landing on Earth, one ton of fuel can land 7 tons of cargo, so for landing 14 tons you will need to burn 2 tons of fuel. Remaining weight is 12 tons, that is, a rocket without any fuel.
In the second case, the rocket will not be able even to take off from Earth.

题目大意

飞船要在n个星球上从编号1开始按照编号顺序依次游览至回到1,每次起飞与降落都会按照当前火箭的总重量和每个星球的起飞与降落的消耗燃料的系数来减少火箭重量。给出火箭重量和每个星球消耗的燃料系数,问最少要多少燃料能让火箭完成这次旅行

解析

第一种方法,假设燃料全部耗完,然后直接根据系数逆着推就可以了。
第二种方法,初值设为0和inf(0x3f3f3f3f),然后二分答案,判断答案时注意当最后剩下的值满足|p−q|/max(1,|q|)≤10−6时就退出循环,这时的mid就是答案。.

AC代码

代码1(逆推)

#include<cstdio>
using namespace std;
const int inf=0x3f3f3f3f;
int n,i;
double a[1010],b[1010],w,t;
int main(){	
	scanf("%d",&n);
	scanf("%lf",&w);
	t=w;
	for(i=1;i<=n;i++)
		scanf("%lf",&a[i]);
	for(i=1;i<=n;i++)	
		scanf("%lf",&b[i]);
	w=w*1.0*b[1]/(b[1]-1);
	for(i=n;i>=2;i--){
		w=w*a[i]*1.0/(a[i]-1);
		w=w*b[i]*1.0/(b[i]-1);
	}
	w=w*1.0*a[1]/(a[1]-1);
	if(w-t>=inf)
		printf("-1");
	else
		printf("%.10lf",w-t);
	return 0;
} 

代码2(二分答案)

#include<cstdio>
using namespace std;
typedef long long ll;
ll n,m,i,f;
double l,r,mid,a[1010],b[1010],w;
bool jd(double mid){
	double w1=w+mid;
	w1-=1.0*w1/a[1];
	for(i=2;i<=n;i++){
		w1-=1.0*w1/b[i];
		w1-=1.0*w1/a[i];
	}
	w1-=1.0*w1/b[1];
	if(w1<=1e-6+w&&w1>=w-1e-6)
		f=1;
	return w1>=w;
}
int main(){
	f=0;
	scanf("%lld",&n);
	scanf("%lf",&w);
	for(i=1;i<=n;i++)
		scanf("%lf",&a[i]);
	for(i=1;i<=n;i++)
		scanf("%lf",&b[i]);
	l=0.0;
	r=1e9;
	while(l+1e-7<r){
		mid=(l+r)/2.0;
		if(jd(mid))
			r=mid;
		else
			l=mid;
		if(f)
			break;
	}
	if(f)
		printf("%.10lf",mid);
	else if(r==1e9)
		printf("-1");
	else
		printf("%.10lf",l);
	return 0;
}