首页 > 代码库 > POJ 2096 Collecting Bugs(dp 期望)
POJ 2096 Collecting Bugs(dp 期望)
题目链接:http://poj.org/problem?id=2096
Description
Two companies, Macrosoft and Microhard are in tight competition. Microhard wants to decrease sales of one Macrosoft program. They hire Ivan to prove that the program in question is disgusting. However, Ivan has a complicated problem. This new program has s subcomponents, and finding bugs of all types in each subcomponent would take too long before the target could be reached. So Ivan and Microhard agreed to use a simpler criteria --- Ivan should find at least one bug in each subsystem and at least one bug of each category.
Macrosoft knows about these plans and it wants to estimate the time that is required for Ivan to call its program disgusting. It‘s important because the company releases a new version soon, so it can correct its plans and release it quicker. Nobody would be interested in Ivan‘s opinion about the reliability of the obsolete version.
A bug found in the program can be of any category with equal probability. Similarly, the bug can be found in any given subsystem with equal probability. Any particular bug cannot belong to two different categories or happen simultaneously in two different subsystems. The number of bugs in the program is almost infinite, so the probability of finding a new bug of some category in some subsystem does not reduce after finding any number of bugs of that category in that subsystem.
Find an average time (in days of Ivan‘s work) required to name the program disgusting.
Input
Output
Sample Input
1 2
Sample Output
3.0000
Source
题意:
一个人受雇于某公司要找出某个软件的bugs和subcomponents,这个软件一共有n个bugs和s个subcomponents,每次他都能同时随机发现1个bug和1个subcomponent,问他找到所有的bugs和subcomponents的期望次数。
PS:http://kicd.blog.163.com/blog/static/126961911200910168335852/
我们用E(i,j)表示他找到了i个bugs和j个subcomponents,离找到n个bugs和s个subcomponents还需要的期望次数,这样要求的就是E(0,0),而E(n,s)=0,对任意的E(i,j),1次查找4种情况,没发现任何新的bugs和subcomponents,发现一个新的bug,发现一个新的subcomponent,同时发现一个新的bug和subcomponent,用概率公式可得:
E(i,j)=1+(i*j/n/s)*E(i,j)+(i*(s-j)/n/s)E(i,j+1)+
((n-i)*j/n/s)*E(i+1,j)+(n-i)*(s-j)/n/s*E(i+1,j+1);
这样根据边界就可解出所有的E(i,j),注意因为当我们找到n个bugs和s个subcomponents就结束,对i>n||j>s均无解的情况,并非期望是0.(数学上常见问题,0和不存在的区别)
那这题是否也是要用高斯消元呢? 用高斯消元得话复杂度是O(n^3),达到10^18 根本是不可解的!!
但其实,注意观察方程,当我们要解E(i,j)的话就需要E(i+1,j),E(I,j+1),E(i+1,j+1), 一开始已知E(n,s),那其实只要我们从高往低一个个解出I,j就可以了! 即可根据递推式解出所有的E(I,j) 复杂度是O(n),10^6 ,完美解决。
代码如下:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define MAXN 1017 double dp[MAXN][MAXN]; //dp(i,j)表示找到了i个bugs和j个subcomponents,离找到n个bugs和s个subcomponents还需要的期望次数 //dp[i][j] = 1+(i*1.0*j/n/s)*dp[i][j]+(i*1.0*(s-j)/n/s)*dp[i][j+1] // +((n-i)*j*1.0/n/s)*dp[i+1][j]+(n-i)*(s-j)*1.0/n/s*dp[i+1][j+1]; //因为dp[i][j]未知,移项化简得 //dp[i][j] = ((i*1.0*(s-j))*dp[i][j+1]+((n-i)*j*1.0)*dp[i+1][j]+(n-i)*(s-j)*1.0*dp[i+1][j+1])/(n*s-i*j); int main() { int n, s; while(~scanf("%d%d",&n,&s)) { memset(dp,0,sizeof(dp)); 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] = (n*s+(i*(s-j))*dp[i][j+1]+((n-i)*j)*dp[i+1][j]+(n-i)*(s-j)*dp[i+1][j+1])/(n*s-i*j); } } printf("%.4lf\n",dp[0][0]); } return 0; }
POJ 2096 Collecting Bugs(dp 期望)