码迷,mamicode.com
首页 > 其他好文 > 详细

Collecting Bugs (概率dp)

时间:2017-04-12 18:49:50      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:code   any   require   its   blog   ber   iostream   category   iter   

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.

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


题意:某公司请人去找程序的 bugs ,他每天可以找出一个bug 。bugs一共有 n 种,程序有 s 个子系统(0 < n, s <= 1 000)
要使每一个子系统至少有一个bug,并且所有种类的bug也至少找出一个的天数期望是多少?(精确到小数后4位)

入门概率dp:
dp[i][j] 代表的状态是有 i 种bug种类至少有一个,j 个子系统找出至少有一个bug
那么,再过一天,可能发生的情况有 4 种: (pi 指发生这种事情的概率)
找到一个已经出现过的种类的bug,并且在已经找出bug的子系统里面 P1 * dp[i][j]
找到一个未出现过的种类的bug,并且在已经找出bug的子系统里面 P2 * dp[i+1][j]
找到一个已经出现过的种类的bug,并且不在已经找出bug的子系统里面 P3 * dp[i][j+1]
找到一个是既未出现过的种类的bug,又不在已经找出bug的子系统里面 P4 * dp[i+1][j+1]
那么,就得出转移方程:
dp[i][j] = P1 * dp[i][j] + P2 * dp[i+1][j] + p3 * dp[i][j+1] + p4 * dp[i+1][j+1] + 1 (+1 是因为要过一天)
将它化简,合并掉两边的 dp[i][j]
显然 dp [n][s]=0
然后从 i = n , j = s 逆推到 i=0,j=0 dp[0][0]即为答案
技术分享
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 using namespace std;
 5 #define MAXN 1010
 6 
 7 double dp[MAXN][MAXN];
 8 
 9 int main()
10 {
11     int n,s;
12     while(scanf("%d%d",&n,&s)!=EOF)
13     {
14         memset(dp,0,sizeof(dp));
15         dp[n][s]=0.0;
16         for (int i=n;i>=0;i--)
17         {
18             for (int j=s;j>=0;j--)
19             {
20                 if (i==n&&j==s) continue;
21                 dp[i][j]=(n*s+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-i*j);//注意数据类型的隐式转换
22             }
23         }
24         printf("%.4lf\n",dp[0][0]);
25     }
26     return 0;
27 }
View Code

 





Collecting Bugs (概率dp)

标签:code   any   require   its   blog   ber   iostream   category   iter   

原文地址:http://www.cnblogs.com/haoabcd2010/p/6700483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!