您的位置:首页 > 其它

POJ 2096 Collecting Bugs [概率DP]

2015-11-30 20:37 417 查看
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 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.

题意:

给出N,S,表示bug有N类,被包含在S个子系统中,现在来debug,每一天你会发现一个bug,等概率的属于其中一类,以及其中一个子系统,问在所有类和子系统中至少发现一个bug的期望天数。

范围:

N,S<=1000

解法:

概率DP,设DP[I][J]表示当前一发现了I类,J个子系统的bug,剩余的期望天数为DP[I][J],并且

P0 代表发现的新bug为已知的概率

Pi 表示发现的bug为新的1类,但是是已知的子系统的概率

Pj 表示发现的bug为新的1个子系统,但是是已知的类的概率

Pij 表示发现的bug为新的子系统,且是新的类别的概率

可知上面的概率都可以用I,J,N,S计算得出,例如P0=(I*J)/(n*s)

那么可以得到方程:

DP[I][J]=P0*DP[I][J]+PI*DP[I+1][J]+PJ*DP[I][J+1]+PIJ*DP[I+1][J+1]+1;

将P0*DP[I][J]这项左移,化简得

DP[I][J]=(PI*DP[I+1][J]+PJ*DP[I][J+1]+PIJ*DP[I+1][J+1]+1)/(1-P0)

显然,边界条件为DP
[S]=0.0

代码:

实现时,使用了记忆化搜索

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

int n,s;
double dp[1111][1111];
bool vis[1111][1111];

double dfs(int i,int j){
if(i>=n&&j>=s)return 0.0;
if(vis[i][j])return dp[i][j];
vis[i][j]=1;
double p0=double(i*j)/double(n*s);
double pi=double((n-i)*j)/double(n*s);
double pj=double(i*(s-j))/double(n*s);
double pij=double((n-i)*(s-j))/double(n*s);
//printf("[%d][%d] %f %f %f %f\n",i,j,p0,pi,pj,pij);
dp[i][j]=1.0/(1.0-p0);
if(i+1<=n)dp[i][j]+=pi*dfs(i+1,j)/(1.0-p0);
if(j+1<=s)dp[i][j]+=pj*dfs(i,j+1)/(1.0-p0);
if(i+1<=n&&j+1<=s)dp[i][j]+=pij*dfs(i+1,j+1)/(1.0-p0);
//printf("%lf\n",dp[i][j]);
return dp[i][j];

}
int main(){
while(scanf("%d%d",&n,&s)!=EOF){
rep(i,0,n)rep(j,0,s)vis[i][j]=0;
double ans=dfs(0,0);
printf("%.10f\n",ans);

}
return 0;
}
/*
2
500 2 2 2 1 1 1
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: