地址:http://poj.org/problem?id=2096
Time Limit: 10000MS | Memory Limit: 64000K | |
Total Submissions: 1884 | Accepted: 888 | |
Case Time Limit: 2000MS | Special Judge |
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
题意:一个软件有s个子系统,会产生n种BUG。
程序员每天会发现一个BUG,发现的BUG属于的任意种类与子系统的概率是相同的。
求发现了n种BUG,每个子系统至少一个BUG的期望天数。
思路:因为是第一次做求期望的题目,首先想到的是通过概率来计算期望,然后推出了四种情况:
发现一种新的BUG,在新的子系统中发现BUG,在新的子系统中发现一种新的BUG,以及在以前子系统中发现老的BUG。
这样可以通过累加来求期望,但对于1000来说会超时,所以放弃了这种方法。
因为有公式E(aA+bB)=aE(A)+bE(B),可以通过期望来求期望。
开数组p[i][j]表示从已发现i种BUG在j各子系统开始到发现n种BUG存在于s各子系统的期望天数。
因此p[n][s]已经完成了,所以为0,而答案是p[0][0]。
这样求期望的数组p[i][j]也存在上述4种情况,可以分别写为:
p[i+1][j]*(n-i)*j/(n*s),p[i][j+1]*i*(s-j)/(n*s),p[i+1][j+1]*(n-i)*(s-j)/(n*s),p[i][j]*i*j/(n*s)
公式p[i][j]=p[i+1][j]*(n-i)*j/(n*s)+p[i][j+1]*i*(s-j)/(n*s)+p[i+1][j+1]*(n-i)*(s-j)/(n*s)+p[i][j]*i*j/(n*s)+1 (最后加1表示加上本步,该公式需化简)
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
//#include<algorithm>
using namespace std;
double p[1010][1010];
int main()
{
int n,s;
while(scanf("%d%d",&n,&s)>0)
{
p[n][s]=0;
for(int i=n;i>=0;i--)
for(int j=(i==n?s-1:s);j>=0;j--)
p[i][j]=(i*(s-j)*p[i][j+1]+(n-i)*j*p[i+1][j]+(n-i)*(s-j)*p[i+1][j+1]+n*s)/(n*s-i*j); //化简的公式
printf("%.4lf\n",p[0][0]);
}
return 0;
}