poj 2096 Collecting Bugs(期望)

http://poj.org/problem?id=2096

程序的bug有n个子集,s个种类。每一个bug属于每个子集的概率为1/n,每一个bug属于每个种类的概率为1/s,问每个子集且每个种类都有bug的期望。

求期望,设dp[i][j]表示已有bug属于i个子集,j个种类的期望,现已知终态为dp[n][s] = 0,dp[i][j]可由逆推而得:

dp[i][j],新的bug属于已有的i个子集j个分类,概率为i/n * j/s;

dp[i][j+1],新的bug属于已有的i个子集但不属于已有的j个分类,概率为i/n * (1 - j/s);

dp[i+1][j],新的bug不属于已有的i个子集但属于已有的j个分类,概率为(1-i/n)*j/s;

dp[i+1][j+1],新的bug不属于已有的i个子集也不属于已有的j个分类,概率为(1 - i/n) * (1 - j/s);

因此dp[i][j] = i/n*j/s*dp[i][j] +  i/n * (1 - j/s)*dp[i][j+1] + (1-i/n)*j/s*dp[i+1][j] + (1 - i/n) * (1 - j/s)*dp[i+1][j+1] + 1。

#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
//#define LL __int64
#define LL long long
#define eps 1e-12
#define PI acos(-1.0)
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 4010;
double dp[1010][1010];

int main()
{
	int n,s;
	while(~scanf("%d %d",&n,&s))
	{
		dp[n][s] = 0;
		for(int i = n; i >= 0; i--)
		{
			for(int j = s; j >= 0; j--)
			{
				if(i == n && j == s)
					continue;
				dp[i][j] = (dp[i+1][j]*(n-i)*j + dp[i][j+1]*i*(s-j) + dp[i+1][j+1]*(n-i)*(s-j)+n*s)/(n*s - i*j)*1.0;
			}
		}
		printf("%.4f\n",dp[0][0]);
	}
	return 0;
}
时间: 2024-10-19 10:30:11

poj 2096 Collecting Bugs(期望)的相关文章

poj 2096 Collecting Bugs——期望DP

题目:http://poj.org/problem?id=2096 f[ i ][ j ] 表示收集了 i 个 n 的那个. j 个 s 的那个的期望步数. #include<cstdio> #include<cstring> #include<algorithm> #define db double using namespace std; const int N=1005; db n,s,f[N][N]; int main() { scanf("%lf%l

POJ - 2096 Collecting Bugs 期望dp

期望是一个 DAG 模型,逆着递推即可~ #include <cstdio> #define N 1005 #define setIO(s) freopen(s".in","r",stdin) using namespace std; double f[N][N]; int main() { int n,s,i,j; scanf("%d%d",&n,&s); for(int i=n;i>=0;--i) { for

[ACM] poj 2096 Collecting Bugs (概率DP,期望)

Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 2026   Accepted: 971 Case Time Limit: 2000MS   Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material st

poj 2096 Collecting Bugs 【概率DP】【逆向递推求期望】

Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 3523   Accepted: 1740 Case Time Limit: 2000MS   Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material s

Poj 2096 Collecting Bugs (概率DP求期望)

C - Collecting Bugs Time Limit:10000MS     Memory Limit:64000KB     64bit IO Format:%I64d & %I64u Submit Status Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stuff, he collects software b

POJ 2096 Collecting Bugs(概率DP求期望)

传送门 Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 4333 Accepted: 2151 Case Time Limit: 2000MS Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu

poj 2096 Collecting Bugs DP

Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 2842   Accepted: 1405 Case Time Limit: 2000MS   Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material s

POJ 2096 Collecting Bugs(dp 期望)

题目链接:http://poj.org/problem?id=2096 Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stuff, he collects software bugs. When Ivan gets a new program, he classifies all possible bugs into n ca

POJ 2096 Collecting Bugs:期望dp

题目链接:http://poj.org/problem?id=2096 题意: 有一个程序猿,他每天都会发现一个bug. bug共有n个种类.属于某一个种类的概率为1/n. 有s个子系统,每个bug属于一个系统.属于某一个系统的概率为1/s. 问你发现的bug能够覆盖到n个种类和s个系统的期望天数. 题解: 期望dp转移的套路: 倒着推. 利用性质:期望 = ∑ (P(子期望)*φ(子期望)) 状态表示: dp[i][j] = expectation i:覆盖到i个种类 j:覆盖到j个系统 dp