您的位置:首页 > 其它

POJ 2096 Collecting Bugs(概率DP)

2016-02-03 23:16 441 查看
题意:一个人要找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);

[code]#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
[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: