手动消元
令 f [ i ] [ j ] f[i][j] f[i][j]表示还剩 i i i个人,第 j j j个人最后活下来的概率
显然
f
[
i
]
[
j
]
=
p
0
∗
f
[
i
−
1
]
[
j
−
1
]
+
(
1
−
p
0
)
∗
f
[
i
]
[
j
−
1
]
f[i][j]=p0*f[i-1][j-1]+(1-p0)*f[i][j-1]
f[i][j]=p0∗f[i−1][j−1]+(1−p0)∗f[i][j−1]
发现这个是带环的,但是所有人概率加起来显然是等于
1
1
1的
就可以像解方程那样解出来回带就可以了
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define gc getchar
inline int read(){
char ch=gc();
int res=0;
while(!isdigit(ch))ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return res;
}
const int N=10005;
struct coe{
long double x,y;
coe(double _x=0,double _y=0):x(_x),y(_y){}
friend inline coe operator +(const coe &a,const coe &b){
return coe(a.x+b.x,a.y+b.y);
}
friend inline coe operator *(const coe &a,const long double &b){
return coe(a.x*b,a.y*b);
}
}g[N],sum;
long double f[2][N];
long double p0;
int n,k,tmp;
inline void calc(int p){
sum=g[1]=coe(1,0);
for(int i=2;i<=p;i++)g[i]=coe(0,p0*f[tmp^1][i-1])+g[i-1]*(1-p0),sum=sum+g[i];
f[tmp][1]=(1-sum.y)/sum.x;
for(int i=2;i<=p;i++)f[tmp][i]=p0*f[tmp^1][i-1]+(1-p0)*f[tmp][i-1];
}
int main(){
scanf("%Lf",&p0);
scanf("%d%d",&n,&k);
if(p0==0){puts("0.000000000");return 0;}
f[tmp][1]=1;
for(int i=2;i<=n;i++)tmp^=1,calc(i);
printf("%.10Lf",f[tmp][k]);
}