POJ 2096 Collecting Bugs(概率DP)

来源:互联网 发布:ssh 端口修改 编辑:程序博客网 时间:2024/05/23 10:24

题意:一个人要找BUG,现在有n个种类,s个子系统。每个BUG有两个属性,一个是每个BUG属于一个子系统和一个分类,问发现n种BUG并且每个子系统都有BUG的平均天数
思路:
期望==概率*值
所以只需要求出所有情况的概率就行了
dp[i][j]代表已经发现了i个种类和j个子系统的BUG
现在有四种情况
1.发现的BUG是属于之前的种类并且属于之前的子系统
2.发现的BUG是属于之前的种类并且不属于之前的子系统
3.发现的BUG是不属于之前的种类并且属于之前的子系统
4.发现的BUG是不属于之前的种类并且不属于之前的子系统
四种情况发生的概率分别统计一下
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);

#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 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;            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);        }    }    printf("%.4f\n",dp[0][0]);    return 0;} 
0 0
原创粉丝点击