Collecting Bugs POJ - 2096(期望dp)

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 categories. Each day he discovers exactly one bug in the program and adds information about it and its category into a spreadsheet. When he finds bugs in all bug categories, he calls the program disgusting, publishes this spreadsheet on his home page, and forgets completely about the program.
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.
某个系统中有n个子系统和m个bug类型,该系统每天会出现一个bug (属于某个子系统和某个bug类型),bug的类型是等概率的,bug也是等概率地出现在每个子系统的。问所有子系统都出现bug且所有的bug类型都出现的期望天数。
Input
Input file contains two integer numbers, n and s (0 < n, s <= 1 000).
Output
Output the expectation of the Ivan’s working days needed to call the program disgusting, accurate to 4 digits after the decimal point.
Sample Input
1 2
Sample Output
3.0000

虽然是暑假写过的题,但我还是不会写。。。

思路:
定义dp[i][j]为i个系统有bug,出现j个类型bug,到出现所有类型bug 所有系统bug的期望天数。
那么dp[n][m] = 0。

子状态为:

  1. 1,代表花费了1天,概率为1.
  2. dp[i][j],对,就是自己!期望dp得考虑自己,概率p1为i / n * j / m
  3. dp[i + 1][j],多了一个系统出现bug,概率p2为(1 - i / n) * j / m
  4. dp[i][j + 1],多了一个类型的bug,概率p3为 i / n * (1 - j / m)
  5. dp[i + 1][j + 1],多了一个系统出现bug,多了一个类型bug,概率p4为(1 - i/n) * (1 - j / m).

结果就是
dp[i][j] = 1 + p1 * dp[i][j] + p2 * dp[i + 1][j] + p3 * dp[i][j + 1] + p4 * dp[i + 1][j + 1].
注意答案里面有”自己“,我们把“自己”给移项移到左边,得到
dp[i][j] = ( 1 + p2 * dp[i + 1][j] + p3 * dp[i][j + 1] + p4 * dp[i + 1][j + 1]) / (1 - p1)

一个值得注意的点是:为什么状态定义为状态(i,j)到目标状态的期望天数,导致我们必须反向dp?

为什么不定义为dp[i][j]为到了状态(i,j)的目标天数,那么结果就是dp[n][m].

冷静分析,假设按照后者定义方式,那么转移到dp[n][m]的时候会出现p1 = 1。那么除数变成了0!成了极限了。因此反向dp可以有效避免这种情况

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

double dp[1005][1005];

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        dp[n][m] = 0;
        for(int i = n;i >= 0;i--)
        {
            for(int j = m;j >= 0;j--)
            {
                if(i == n && j == m)continue;
                double ii = (double)i,jj = (double)j,nn = (double)n,mm = (double)m;
                double p1 = (double)(ii / nn) * (jj / mm);
                double p2 = (double)(ii / nn) * (1.0 - jj / mm);
                double p3 = (double)(1.0 - ii / nn) * (jj / mm);
                double p4 = (double)(1.0 - ii / nn) * (1.0 - jj / mm);
                dp[i][j] =  1.0 + p2 * dp[i][j + 1] + p3 * dp[i + 1][j] + p4 * dp[i + 1][j + 1];
                dp[i][j] /= (1.0 - p1);
            }
        }
        printf("%.4f\n",dp[0][0]);
    }
    
    return 0;
}

发布了668 篇原创文章 · 获赞 18 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104237093