【洛谷P5249】【LnOI2019】—加特林轮盘赌(概率dp)

传送门

手动消元

f[i][j]f[i][j]表示还剩ii个人,第jj个人最后活下来的概率

显然f[i][j]=p0f[i1][j1]+(1p0)f[i][j1]f[i][j]=p0*f[i-1][j-1]+(1-p0)*f[i][j-1]
发现这个是带环的,但是所有人概率加起来显然是等于11

就可以像解方程那样解出来回带就可以了

#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]);
}
posted @ 2019-04-03 22:04  Stargazer_cykoi  阅读(112)  评论(0编辑  收藏  举报