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

传送门

手动消元

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]=p0f[i1][j1]+(1p0)f[i][j1]
发现这个是带环的,但是所有人概率加起来显然是等于 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]);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值