2020ICPC南京 F Fireworks(概率,三分)

链接

题目描述

Kotori is practicing making fireworks for the upcoming hanabi taikai1. It takes her nn minutes to make a single firework, and as she is not really proficient in making fireworks, each firework only has a probability of p × 1 0 − 4 p \times 10^{-4} p×104 to be perfect.

After she finishes making a firework, she can just start making the next firework, or take mm minutes to light all the remaining fireworks finished before. If there is at least one perfect firework among the lit ones, she will be happy and go to rest. Otherwise, she will continue practicing. Can you tell her the minimum expected practicing time before she goes to rest if she takes the optimal strategy?

Notice that no matter how many fireworks remain, it always takes mm minutes to light them all.

Hanabi taikai: Romaji of the Japanese word “花火大會”, which means the firework… err… party?

输入描述:

There are multiple test cases. The first line of the input contains an integer T T T ( 1 ≤ T ≤ 1 0 4 ) (1 \le T \le 10^4) (1T104) indicating the number of test cases. For each test case:

The first and only line contains three integers n n n, m m m and p p p ( 1 ≤ n , m ≤ 1 0 9 , 1 ≤ p ≤ 1 0 4 ) (1 \le n, m \le 10^9, 1 \le p \le 10^4) (1n,m109,1p104).

输出描述:

For each test case, output one line containing one number indicating the minimum expected practicing time.

Your answer will be considered correct if and only if the absolute or relative error does not exceed 1 0 − 4 10^{-4} 104.

输入

3
1 1 5000
1 1 1
1 2 10000

输出

4.0000000000
10141.5852891136
3.0000000000

思路

设做好 x x x 个烟花再点燃是最策略, d p x dp_x dpx 代表这一策略花费时间的期望,那么有: d p x = n × i + m + ( i − p ) i d p x dp_x=n\times i +m+(i-p)^idp_x dpx=n×i+m+(ip)idpx 。其中 ( 1 − p ) i d p x (1-p)^idp_x (1p)idpx 代表已经制作好的 i i i 个烟花有 ( 1 − p ) i (1-p)^i (1p)i 的概率都是不完美的,那么就需要再制造 k k k 个烟花来点燃。

那么 d p x = n × i + m 1 − ( 1 − p ) i dp_x=\frac{n\times i+m}{1-(1-p)^i} dpx=1(1p)in×i+m

f ( x ) = a x + b 1 − c x f(x)=\frac{ax+b}{1-c^x} f(x)=1cxax+b ( 0 < c < 1 ) (0<c<1) (0<c<1),那么 f ′ ( x ) = a ( 1 − c x ) + c x l n c ( a x + b ) ( 1 − c x ) 2 f'(x)=\frac{a(1-c^x)+c^xlnc(ax+b)}{(1-c^x)^2} f(x)=(1cx)2a(1cx)+cxlnc(ax+b)。先递减再递增,是个单峰函数,使用三分求出最值。

#include<bits/stdc++.h>
using namespace std;
int T,n,m; double p;

double qpow(double a,int b){
	double ret=1;
	while(b){
		if(b&1) ret=ret*a;
		a=a*a; b>>=1;
	}
	return ret;
}

double calc(int i){
	return ((double)n*i+m)/(1-qpow(1-p*1e-4,i));
}


void solve(){
	cin>>n>>m>>p;
	int l=1,r=1e9;
	while(l<r){
		int midl=(l+r)>>1;
		int midr=midl+1;
		if(calc(midl)<calc(midr)) r=midr-1;
		else l=midl+1;
	}
	cout<<setprecision(10)<<fixed<<calc(l)<<"\n";
}

int main(){
	ios::sync_with_stdio(false);
	for(cin>>T;T;T--) solve();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_51864047

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值