跳过正文
题解:UVA10056 What is the Probability ?

题解:UVA10056 What is the Probability ?

xyx404
作者
xyx404
Have a nice day.

思路:
#

为了计算第 $i$ 个玩家的获胜概率,我们需要考虑所有可能的情况,即所有玩家轮流投掷直到某人获胜。给定 $n$ 个玩家,每个玩家投掷成功的概率为 $p$,失败的概率为 $1-p$。

第 $i$ 个玩家的获胜方式可以分为以下几种情况:

  • 在第一轮中,第 $i$ 个玩家直接获胜,这发生在前 $i-1$ 个玩家都失败了之后,所以概率为 $(1-p)^{i-1} \cdot p$。
  • 第 $i$ 个玩家在第二轮获胜,这意味着所有 $n$ 个玩家在第一轮都失败了,然后前 $i-1$ 个玩家在第二轮也失败了,第 $i$ 个玩家在第二轮获胜,所以概率为 $(1-p)^{n} \cdot (1-p)^{i-1} \cdot p = (1-p)^{n+i-1} \cdot p$。
  • 同理,第i个玩家在第三轮获胜的概率为 $(1-p)^{2n+i-1} \cdot p$。
  • 这个模式可以无限延续下去,形成一个无穷等比数列,其中首项为 $(1-p)^{i-1} \cdot p$,公比为 $(1-p)^{n}$。

因此,第i个玩家获胜的总概率P可以表示为这个无穷等比数列的和:

$$P = (1-p)^{i-1} \cdot p + (1-p)^{n+i-1} \cdot p + (1-p)^{2n+i-1} \cdot p + \ldots $$

这是一个等比数列的和,可以用公式计算:

$$ P = \frac{(1-p)^{i-1} \cdot p}{1-(1-p)^{n}} $$

代码:
#

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define itn int
#define ull unsigned long long
int T=1; 
double solve(int n,int i,double p){
	double q=1-p;// 失败概率
	double fz=pow(q,i-1)*p;// 分子
	double fm=1-pow(q,n);// 分母
	double ans=fz/fm;
	return ans;
}
int main(){
	cin>>T;
	while(T--){
		int n,i;
		double p;
		cin>>n>>p>>i;
		printf("%.4lf\n",p!=0?solve(n,i,p):0.0);// 三目运算符特判 
	}
	return 0;
}

AC 记录