//poj 2151 概率dp
1 #include "iostream" 2 #include "cstdio" 3 #include "cstring" 4 #include "algorithm" 5 using namespace std; 6 double dp[33][33]; 7 int M, T, N; //problem, team, least 8 double p[1010][33]; 9 int main() 10 { 11 int i, j, k; 12 while(scanf("%d%d%d", &M, &T, &N) && (M || T || N)) { 13 for(i = 1; i <= T; ++i) 14 for(j = 1; j <= M; ++j) 15 scanf("%lf", &p[i][j]); 16 double res_1 = 1, res_2 = 1; //所有队伍都解出一题以上的概率,所有队伍都没解到超过 N 道的概率 17 for(i = 1; i <= T; ++i) { 18 dp[0][0] = 1; 19 for(j = 1; j <= M; ++j) { 20 for(k = 0; k <= j && k < N; ++k) { 21 if(k >= 1) 22 dp[j][k] = dp[j - 1][k] * (1 - p[i][j]) + dp[j - 1][k - 1] * p[i][j]; 23 else 24 dp[j][k] = dp[j - 1][k] * (1 - p[i][j]); 25 } 26 } 27 res_1 *= (1 - dp[M][0]); 28 double tmp = 0; 29 for(k = 1; k < N; ++k) 30 tmp += dp[M][k]; 31 res_2 *= tmp; 32 } 33 printf("%.3f\n",res_1 - res_2); 34 } 35 }
//优化下空间
1 #include "iostream" 2 #include "cstring" 3 #include "algorithm" 4 #include "cstdio" 5 using namespace std; 6 int M, T, N; //problem, team, least 7 double dp[35][35]; 8 9 int main() 10 { 11 int i, j, k; 12 double p, res_1, res_2; //所有队伍都解出一题以上的概率,所有队伍都没解到超过 N 道的概率 13 while(scanf("%d%d%d", &M, &T, &N) && (N || T || M)) { 14 res_1 = res_2 = 1; 15 for(i = 1; i <= T; ++i) { 16 dp[0][0] = 1; 17 for(j = 1; j <= M; ++j) { 18 scanf("%lf", &p); 19 for(k = 0; k <= j && k < N; ++k) { 20 if(k >= 1) 21 dp[j][k] = dp[j - 1][k] * (1 - p) + dp[j - 1][k - 1] * p; 22 else 23 dp[j][k] = dp[j - 1][k] * (1 - p); 24 } 25 } 26 res_1 *= (1 - dp[M][0]); 27 double tmp = 0; 28 for(k = 1; k<N; ++k) 29 tmp += dp[M][k]; 30 res_2 *= tmp; 31 } 32 printf("%.3f\n", res_1 - res_2); 33 } 34 }
时间: 2024-10-11 18:12:40