题目描述
Organizing a programming contest is not an easy job. To avoid making the problems too difficult, the organizer usually expect the contest result satisfy the following two terms:
1. All of the teams solve at least one problem.
2. The champion (One of those teams that solve the most problems) solves at least a certain number of problems.
Now the organizer has studied out the contest problems, and through the result of preliminary contest, the organizer can estimate the probability that a certain team can successfully solve a certain problem.
Given the number of contest problems M, the number of teams T, and the number of problems N that the organizer expect the champion solve at least. We also assume that team i solves problem j with the probability Pij (1 <= i <= T, 1<= j <= M). Well, can you calculate the probability that all of the teams solve at least one problem, and at the same time the champion team solves at least N problems?
算法思路
- 这一题用了高中数学一个十分重要的思想–正难则反。
- 所有的队伍都必须至少通过1题,看起来十分麻烦,因为正面去求的话要使用组合数。但是,这个条件的逆命题就是存在一个队伍一题都没有通过,很显然这是非常好求的。
- 剩下的第二个条件,冠军队至少要通过n题。也就是至少有一队要通过n题。看起来,这又要运用组合数了。但是我们看到,这一题的反面就是没有1支队可以通过至少n题。这也是非常好解的。
- 减小算法的空间复杂度,我们只需要dp[i][j]是当前所求的队伍中前i题过j题的概率即可。最后累加我们的结果就可以得到最后的解。算法的时间复杂度为O( nmt )
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define MAXN 35
#define MAXT 1005
#define INF 10e+20
#define ZERO 10e-6
double Arr[MAXT][MAXN];
double dp[MAXN][MAXN];
double noWin[MAXT];
int N,M,T;
double ans1,ans2;
void Solve()
{
int i,j,k;
double temp;
ans2=1.0;
for(i=0;i<T;i++){
memset(dp,0,sizeof(dp));
dp[1][0]=1-Arr[i][1];
dp[1][1]=Arr[i][1];
for(j=2;j<=M;j++){
dp[j][0] = dp[j-1][0]*(1-Arr[i][j]);
for(k=1;k<=j;k++)
dp[j][k] = dp[j-1][k-1]*Arr[i][j] + dp[j-1][k]*(1-Arr[i][j]);
}
temp = 0;
//add up to find the solution
for(j=1;j<N;j++)
temp += dp[M][j];
ans2 *= temp;
}
}
int main()
{
freopen("input","r",stdin);
int i,j;
while(scanf("%d%d%d",&M,&T,&N)!=EOF){
if(!T && !M && !N)break;
ans1 = 1.0;
for(i=0;i<T;i++){
noWin[i] = 1.0;
for(j=1;j<=M;j++){
scanf("%lf",&Arr[i][j]);
noWin[i] *= (1-Arr[i][j]);
}
//compute the probability of every team solve
//at least one problem
ans1 *= (1-noWin[i]);
}
Solve();
printf("%.3f\n",ans1-ans2);
}
return 0;
}