首页 > 代码库 > POJ 2151 Check the difficulty of problems (概率DP)

POJ 2151 Check the difficulty of problems (概率DP)

题意:ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 ,求每队至少解出一题且冠军队至少解出N道题的概率。

析:概率DP,dp[i][j][k] 表示第 i 个队伍,前 j 个题,解出 k 个题的概率,sum[i][j] 表示第 i 个队伍,做出 1-j 个题的概率,ans1等于,

T个队伍,至少解出一个题的概率,ans2 表示T个队伍,至少解出一个题,但不超过N-1个题的概率,最后用ans1-ans2即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;

}
double dp[2][35][35];
double a[1005][35];
double sum[1005][35];

int main(){
    int t;
    while(scanf("%d %d %d", &n, &m, &t) == 3 && m+n+t){
        for(int i = 1; i <= m; ++i)
            for(int j = 1; j <= n; ++j)
                scanf("%lf", &a[i][j]);
        memset(dp, 0, sizeof dp);
        dp[1][0][0] = dp[0][0][0] = 1.0;
        int cnt = 0;
        for(int i = 1; i <= m; ++i, cnt ^= 1){
            for(int j = 1; j <= n; ++j)
                for(int k = 0; k <= j; ++k)
                    dp[cnt][j][k] = dp[cnt][j-1][k] * (1.0 - a[i][j]) + dp[cnt][j-1][k-1] * a[i][j];
            sum[i][0] = 0.0;
            for(int k = 1; k <= n; ++k)
                sum[i][k] = sum[i][k-1] + dp[cnt][n][k];
        }

        double ans1 = 1.0, ans2 = 1.0;
        for(int i = 1; i <= m; ++i)  ans1 *= sum[i][n];
        for(int i = 1; i <= m; ++i)  ans2 *= sum[i][t-1];
        printf("%.3f\n", ans1-ans2);
    }
    return 0;
}

 

POJ 2151 Check the difficulty of problems (概率DP)