zoukankan      html  css  js  c++  java
  • 【洛谷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]);
    }
    
  • 相关阅读:
    Leetcode#104 Maximum Depth of Binary Tree
    Leetcode#102 Binary Tree Level Order Traversal
    js 实时显示字数
    js获取链接参数
    DIV+CSS左右列高度自适应问题
    css 背景透明,文字不透明
    css position的值
    从头搭建vue项目
    vuejs怎么在服务器部署?
    windows下nginx安装、配置与使用
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/11145573.html
Copyright © 2011-2022 走看看