POJ_2151_Check the difficulty of problems_概率DP

今天早上五点醒,困成狗了哦凑。电工实习焊了一天的电路,做了一个充电器,搭焊简直就是全世界劳动者的阶级敌人哦凑,太丧心病狂了。
今天还刷了一道水题,POJ3295,用编译原理写Parser的思想即可,挺水的,就是注意在判断或和与的时候C++会自动优化,导致可能只判与或的第一部分,这样Parser的指针就没有前进了。上个学期竟然觉得这题没法做。。。。

题意:
一场ACM比赛,一共T个队,M个题,已知每个队伍做出每道题目的概率,求满足下列情况的概率:

  1. 所有队伍至少做出一道题
  2. 题数最多的队伍最少做出M道题

Input

The input consists of several test cases. The first line of each test case contains three integers M (0 < M <= 30), T (1 < T <= 1000) and N (0 < N <= M). Each of the following T lines contains M floating-point numbers in the range of [0,1]. In these T lines, the j-th number in the i-th line is just Pij. A test case of M = T = N = 0 indicates the end of input, and should not be processed.

Output

For each test case, please output the answer in a separate line. The result should be rounded to three digits after the decimal point.

这题怎么能被分到二分搜索或哈希的专题里面去。。。
首先注意到队伍数很少,因此多重循环是可以接受的。题目所求为所有队伍解题数均大于1且最高解题数大于M,可以转化为所有队伍解题数大于1且非所有队伍解题数大于1小于M。用概率DP求出队伍i解出j道题的概率dp[i][j]。所有队伍解题数在l到r范围内的概率就变成了Sum( dp[i][j] ) (j from l to r)

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define mxt 1010
#define mxm 40
int m,t,n;
double p[mxt][mxm];
double dp[mxt][mxm][mxm];
double calc(int l,int r){
    if(r<l) return 0.0;
    double ret=1.0;
    for(int i=0;i<t;++i){
        double tem=0.0;
        for(int j=l;j<=r;++j)
            tem+=dp[i][m][j];
        ret*=tem;
    }
    return ret;
}
int main(){
    while(scanf("%d%d%d",&m,&t,&n)!=EOF){
        if(!m&&!t&&!n)  break;
        for(int i=0;i<t;++i)
            for(int j=1;j<=m;++j)
                scanf("%lf",&p[i][j]);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<t;++i)
            for(int j=0;j<=m;++j){
                dp[i][j][0]=1.0;
                for(int k=1;k<=j;++k)
                    dp[i][j][0]*=1-p[i][k];
            }
        for(int i=0;i<t;++i)
            for(int j=1;j<=m;++j)
                for(int k=1;k<=j;++k)
                    dp[i][j][k]=dp[i][j-1][k]*(1-p[i][j])+dp[i][j-1][k-1]*p[i][j];
        double ans=calc(1,m)-calc(1,n-1);
        printf("%.3lf\n",ans);
    }
    return 0;
}
点赞