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
題意:
給出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[N][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
*/