概率DP合集

http://www.bnuoj.com/v3/contest_show.php?cid=7521#problem/D
根据kuangbin的概率DP集合刷的 Orz 感谢kuangbin巨巨
http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710606.html
上边的blog已经总结的很好了…
剩下的题有些东西没学以后再补…

B.POJ 2096 Collecting Bugs

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define sfor(i,a,h) for(i=h[a];i!=-1;i=e[i].next)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
#define cheak(i) printf("%d ",i)
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define inf 0x3f3f3f3f
#define lowbit(x) (x&(-x))
typedef long long LL;
#define maxn 1005
#define f(x) (x*1.0)
#define maxm maxn*maxn
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
double dp[maxn][maxn];
int main()
{
    int i,j,n,s;
    scanf("%d%d",&n,&s);
    dp[n][s]=0;
    lfor(i,n,0)
    {
        lfor(j,s,0)
        {
            if(i==n&&j==s) continue;
            double x=n*s*1.0,y=i*j*1.0;
            //dp[i][j]=(1+dp[i+1][j]*(n-i)*j/x+dp[i][j+1]*i*(s-j)/x+dp[i+1][j+1]*(n-i)*(s-j)/x)/(1-i*j/x);
            dp[i][j]=(1+dp[i+1][j]*(1.0-f(i)/f(n))*(f(j)/f(s))+dp[i][j+1]*(1.0-f(j)/f(s))*(f(i)/f(n))+dp[i+1][j+1]*(1.0-f(i)/f(n))*(1.0-f(j)/f(s)))/(1.0-y/x);
        }
    }
    printf("%.4f\n",dp[0][0]);
    return 0;
} 

C. ZOJ 3329 One Person Game

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
using namespace std;
typedef long long LL;
#define maxn 1005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
double dp[maxn],p[maxn],A[maxn],B[maxn];
int main()
{
    int T,n,k1,k2,k3,a,b,c,i,j,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
        mem(dp,0);mem(p,0);
        double x=1.0/(1.0*k1*k2*k3);
        rfor(i,1,k1)
         rfor(j,1,k2)
          rfor(k,1,k3)
          if(i!=a||j!=b||k!=c)
          p[i+j+k]+=x;
        mem(A,0);mem(B,0);
        lfor(i,n,0)
        {
            rfor(j,3,k1+k2+k3)
            {
                A[i]+=p[j]*A[i+j];
                B[i]+=p[j]*B[i+j];
            }
            A[i]+=x;
            B[i]++;
        }
        printf("%.15lf\n",B[0]/(1-A[0]));
    }
    return 0;
}

D. HDU 4405 Aeroplane chess

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
using namespace std;
typedef long long LL;
#define maxn 100005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
double dp[maxn];
int fly[maxn];
int main()
{
    int n,m,i,j,x,y;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        mem(fly,-1);mem(dp,0);
        rfor(i,1,m)
        {
            scanf("%d%d",&x,&y);
            fly[x]=max(fly[x],y);
        }
        double x=1.0/6.0;
        lfor(i,n-1,0)
        {
            if(fly[i]==-1)
            rfor(j,1,6)
            {
                dp[i]+=dp[i+j]*x;
            }
            else dp[i]=dp[fly[i]];
            if(fly[i]==-1) dp[i]++;
        }
        printf("%.4f\n",dp[0]);
    }
    return 0;
}

E. HDU 4089 Activation

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
using namespace std;
typedef long long LL;
#define maxn 2005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
/*
1 激活失败 留在队列中等待下一次激活(概率为p1)
2 失去连接 出队列 然后排在队列的最后(概率为p2)
3 激活成功 离开队列(概率为p3)
4 服务器瘫痪 服务器停止激活 所有人都无法激活了
*/
/*
if(j==1)
dp[i][1]=dp[i][1]*p1+dp[i][i]*p2+p4;
else if(j<=k)
dp[i][j]=dp[i][j]*p1+dp[i][j-1]*p2+dp[i-1][j-1]*p3+p4;
else
dp[i][j]=dp[i][j]*p1+dp[i][j-1]*p2+dp[i-1][j-1]*p3;
*/
/* 
dp[i][j] i 个人 排在 j  1<=j<=i
a=p2/(1-p1) b=p3/(1-p1) c=p4/(1-p1)
if(j==1)
dp[i][1]=a*dp[i][i]+c;
else if(j<=k)
dp[i][j]=a*dp[i][j-1]+b*dp[i-1][j-1]+c;
else
dp[i][j]=a*dp[i][j-1]+b*dp[i-1][j-1]; 
*/
//dp[i][j]=sigma(dp[i][j]*a|1<=j<=i)+sigma(b*dp[i-1][j-1]+c)+j<=k?c:0;
int n,m,k;
double p1,p2,p3,p4,a,b,c;
double dp[maxn][maxn],A[maxn];
int main()
{
    int i,j;
    while(~scanf("%d%d%d%lf%lf%lf%lf",&n,&m,&k,&p1,&p2,&p3,&p4))
    {
        if(p4<(1e-10))
        {
            printf("0.00000\n");
            continue;
        }
        a=p2/(1.0-p1),b=p3/(1.0-p1),c=p4/(1.0-p1);
        //printf("%lf %lf %lf\n",a,b,c);
        dp[1][1]=(p4)/(1-p1-p2);
        rfor(i,2,n)
        {
            rfor(j,2,(i<=k?i:k))
            A[j]=b*dp[i-1][j-1]+c;
            rfor(j,k+1,i)
            A[j]=b*dp[i-1][j-1];
            double p=1,tmp=0;
            lfor(j,i,2)
            {
                tmp+=p*A[j];
                p*=a;
            }
            dp[i][i]=(tmp+c*p)/(1.0-p*a);
            dp[i][1]=a*dp[i][i]+c;
            rfor(j,2,i-1)
            dp[i][j]=a*dp[i][j-1]+A[j];
        }
        printf("%.5f\n",dp[n][m]);
    }
    return 0;
}

F. HDU 4035 Maze

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
using namespace std;
typedef long long LL;
#define maxn 10005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
vector<int>vec[maxn];
struct node
{
    double die,out;
}A[maxn];
double dp[maxn],a[maxn],b[maxn],c[maxn];
int dfs(int u,int fa)
{
    int Size=vec[u].size(),i;
    a[u]=A[u].die;
    b[u]=(1-A[u].die-A[u].out)/Size;
    c[u]=1-A[u].die-A[u].out;
    double tmp=0;
    rfor(i,0,Size-1)
    {
        int v=vec[u][i];
        if(v!=fa)
        {
            if(!dfs(v,u)) return 0;
            a[u]+=(1-A[u].die-A[u].out)/f(Size)*a[v];
            c[u]+=(1-A[u].die-A[u].out)/f(Size)*c[v];
            tmp+=(1-A[u].die-A[u].out)/f(Size)*b[v];
        }
    }
    if(fabs(tmp-1)<1e-9) return 0;
    a[u]/=1-tmp;
    b[u]/=1-tmp;
    c[u]/=1-tmp;
    return 1;
}
int main()
{
    int o=0,T,i,n,u,v;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        rfor(i,1,n) vec[i].clear();
        rfor(i,1,n-1)
        {
            scanf("%d%d",&u,&v);
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
        rfor(i,1,n)
        {
            scanf("%d %d",&u,&v);
            A[i].die=f(u)/100.0;
            A[i].out=f(v)/100.0;
        }
        printf("Case %d: ",++o);
        if(dfs(1,0)&&fabs(1-a[1])>1e-9)
        printf("%.6lf\n",c[1]/(1-a[1]));
        else printf("impossible\n");
    }
    return 0;
}

G. HDU 3853 LOOPS

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
using namespace std;
typedef long long LL;
#define maxn 1005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
double A[maxn][maxn],B[maxn][maxn],C[maxn][maxn];
double dp[maxn][maxn];
//不动  向右  向下 
int main()
{
    int i,j,n,m;
    while(~scanf("%d%d",&n,&m))
    {
        rfor(i,1,n)
        {
            rfor(j,1,m)
            {
                scanf("%lf%lf%lf",&A[i][j],&B[i][j],&C[i][j]);
            }
        }
        mem(dp,0);
        dp[n][m]=0;
        lfor(i,n,1)
        {
            lfor(j,m,1)
            {
                if(i==n&&j==m) continue;
                if(A[i][j]==1.0) continue;
                dp[i][j]=(dp[i+1][j]*C[i][j]+dp[i][j+1]*B[i][j]+2)/(1.0-A[i][j]);
            }
        }
        printf("%.3f\n",dp[1][1]);
    }
    return 0;
} 

H POJ 2151 Check the difficulty of problems

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
using namespace std;
typedef long long LL;
#define maxn 1005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
// M道题 T个队 
// i队解决j道题的概率
// 问冠军至少N道题&&每队AC一题概率==每队至少AC一道-每队AC都在1~N-1
//每队AC概率==(1-每道题都不AC)的乘积
// 每队AC一题的概率==(1-(1-p[i][j]))的乘积
double A[maxn][35],dp[maxn][35][35],p[maxn][35];
int main()
{
    int i,j,k,m,t,n;
    while(~scanf("%d%d%d",&m,&t,&n))
    {
        if(m==0&&t==0&&n==0) break;
        rfor(i,1,t)
        {
            rfor(j,1,m)
            scanf("%lf",&A[i][j]);
        }
        rfor(i,1,t)//第i个队
        {
            dp[i][0][0]=1.0;
            double x=1;
            rfor(j,1,m)//前j道题 
            {
                x*=(1-A[i][j]);
                dp[i][j][0]=x;
                rfor(k,1,n)//解出k道题
                dp[i][j][k]=dp[i][j-1][k-1]*A[i][j]+dp[i][j-1][k]*(1-A[i][j]);
            }
        }
        /*rfor(i,1,t)
        {
            rfor(j,1,m)
            printf("%lf ",dp[i][m][j]);
            printf("\n");
        }printf("\n");*/
        double p1=1;
        rfor(i,1,t)
        {
            double x=1;
            rfor(j,1,m)
            x*=(1-A[i][j]);
            p1*=(1.0-x);
        }
        double p2=1;
        rfor(i,1,t)
        {
            double x=0;
            rfor(j,1,n-1)
            x+=dp[i][m][j];
            p2*=x;
        }
        printf("%.3lf\n",p1-p2);
    }
    return 0;
}

I. Codeforces 148D Bag of mice

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
using namespace std;
typedef long long LL;
#define maxn 1005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
double dp[maxn][maxn];
int main()
{
    int w,b,i,j;
    while(~scanf("%d%d",&w,&b))
    {
        mem(dp,0);
        rfor(i,1,w) dp[i][0]=1;
        rfor(i,1,b) dp[0][i]=0;
        rfor(i,1,w)
        {
            rfor(j,1,b)
            {
                dp[i][j]+=f(i)/f(i+j);
                if(j>=3)
                dp[i][j]+=f(j)/f(i+j)*f(j-1)/f(i+j-1)*f(j-2)/f(i+j-2)*dp[i][j-3];
                if(i>=2)
                dp[i][j]+=f(j)/f(i+j)*f(j-1)/f(i+j-1)*f(i)/f(i+j-2)*dp[i-1][j-2];
            }
        }
        printf("%.9f\n",dp[w][b]);
    }
    return 0;
} 

J .POJ 3071 Football

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
using namespace std;
typedef long long LL;
#define maxn 505
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
double A[maxn][maxn],dp[8][maxn];
int main()
{
    int i,j,k,n;
    while(~scanf("%d",&n)&&n!=-1)
    {
        int m=1<<n;
        rfor(i,1,m)
        {
            rfor(j,1,m)
            {
                scanf("%lf",&A[i][j]);
            }
        }
        mem(dp,0);
        rfor(i,1,m) 
        if(i%2) dp[1][i]=A[i][i+1];
        else dp[1][i]=A[i][i-1];
        rfor(i,2,n)
        {
            rfor(j,1,m)
            {
                int t=1<<i,pos=1;
                while(j>=t+pos) pos+=t;

                if(j<pos+t/2) pos+=t/2;//printf("%d %d %d\n",i,t,pos);
                rfor(k,pos,pos+t/2-1)
                dp[i][j]+=dp[i-1][k]*A[j][k]*dp[i-1][j];
            }
        }
        //rfor(i,1,m) printf("%lf ",dp[n][i]);printf("\n");
        int flag=1;
        rfor(i,2,m)
        if(dp[n][flag]<dp[n][i]) flag=i;
        printf("%d\n",flag);
    }
    return 0;
}

K. SGU 495 Kids and Prizes

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
using namespace std;
typedef long long LL;
#define maxn 100005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
//n-n*((n-1)/n)^m
int main()
{
    double n,m;
    while(~scanf("%lf%lf",&n,&m))
    {
        double ans;
        ans=n-n*pow((n-1)/n,m);
        printf("%.9lf\n",ans);
    }
    return 0;
}

M. ZOJ 3640 Help Me Escape

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<string>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
using namespace std;
typedef long long LL;
#define maxn 20005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
const double tt=(1.0+sqrt(5.0))/2.0;
double dp[maxn];
int A[105];
int main()
{
    int i,j,n,f;
    while(~scanf("%d%d",&n,&f))
    {
        double x=f(1)/f(n);
        rfor(i,1,n) scanf("%d",&A[i]);
        sort(A+1,A+n+1);
        mem(dp,0);
        double t=0;
        rfor(i,1,n)
        {
            int tmp=tt*A[i]*A[i];
            t+=(double)tmp*x;
        }
        rfor(i,A[n]+1,A[n]*2) dp[i]=t;
        lfor(i,A[n],f)
        {
            rfor(j,1,n)
            {
                if(i>A[j])
                {
                    int tmp=tt*A[j]*A[j];
                    t=tmp*x;
                    dp[i]+=t;
                }
                else dp[i]+=(1+dp[i+A[j]])*x;
            }
        }
        printf("%.3lf\n",dp[f]);
    }
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值