11181-probability| Given
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_ problem&problem=2122
Ideas:
Take sample one's "Three choice two" as an example:
How do you find the denominator? p= buy * Buy * do not buy + buy * do not buy * buy + do not buy * buy = 0.092, this is three people just two of the probability of buying things.
What do the molecules ask? P (No. 1th) = buy * Buy * do not buy + buy * do not buy * buy = 0.038, so in the case of 2 people bought something, the probability of buying something 1th is 0.038/0.092≈0.413403
But there's a repeat calculation, so we might as well count the corresponding molecules in the enumeration combination (calculating the denominator), as detailed in the code.
Complete code:
01./*0.076s*/02.
#include <cstdio> #include <cstring> 05.const int N = 25;
07.int N;
08.double P[n], ans[n]; 10.double DFS (int cnt, int r, double pi) 11. {A. if (CNT = n) return r? 0.0:pi;///r was 0, and returned Pi 13 after the election.
Double sum = 0.0; 15 if (R). {sum = = DFS (cnt + 1, r-1, pi * p[cnt]);///Select this person 17. ANS[CNT] + = sum;///molecule 18. } 19. sum = = DFS (cnt + 1, R, Pi * (1-p[cnt));///don't choose this person 20.
return sum;
21.} 22. 23.int Main () 24.
{A. int cas = 0, R, I;
Double P; while (scanf ("%d%d", &n, &r), N) 28.
{for (i = 0; i < n; ++i) scanf ("%lf", &p[i));
memset (ans, 0, sizeof (ans));
printf ("Case%d:\n", ++cas); P = DFS (0, R, 1.0);///Denominator 33. for (i = 0; i < n; ++i) 34.
printf ("%.6f\n", Ans[i]/P); 35.} 36.
return 0; Notoginseng.}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/