Analysis:"R personal bought something" this time is called E, "I personally buy things" this time is called Ei, then the conditional probability P (ei| E).
According to the conditional probability formula, P (ei| e) =p (EiE)/P (E)
P (E) is still available in the full probability formula, for example, n=4,r=2 has 6 possible: 1100,1010,1001,0110,0101,0011, where 1100 of the probability is p1*p2* (1-P3) * (1-P4), other similar, set a "K" Indicates whether the K person buys something, (1 means buy, 0 means no buy), and the recursive method is used to enumerate the conditions of the exact R a[k]=1.
How to calculate P (EiE), the same way, only when the enumeration to ensure that the first a[i]=1, it is not difficult to find, in fact, you can use the enumeration to calculate all the values, using tot to denote the sum of the above probabilities, sums "I" means the probability of a[i], the answer P (Ei)/P (E) =sum[i]/ Totdirectly according to the definition of conditional probabilities
P (a| b) = P (AB)/p (b)
In this question, B is the R person who buys something.
A It's someone who buys something.
then consider the probabilities of all cases, the probabilities of each event that are added up
start enumerating all the combinations will not be written, and later think of is similar to the idea of DP, DFS implementation
#include < Cstdio>double p[25],ans[25];int n,r,vis[25];void dfs (int k,int count) {if (count==r) {double tem=1; for (int i=1;i<=n;i++) {if (vis[i]) tem *= P[i]; else tem *= (1-p[i]); } Ans[0]+=tem; for (int i=1;i<=n;i++) if (vis[i]) Ans[i]+=tem; } else{for (int i=k+1;i<=n;i++) {vis[i]=1; DFS (I,COUNT+1); vis[i]=0; }}}int Main () {int t=0; while (scanf ("%d%d", &n,&r)) {if (n==0&&r==0) break; t++; printf ("Case%d:\n", t); for (int i=0;i<25;i++) {ans[i]=0; vis[i]=0; } for (int i=1;i<=n;i++) scanf ("%lf", &p[i]); DFS (0,0); for (int i=1;i<=n;i++) printf ("%.6lf\n", Ans[i]/ans[0]); } return 0;}
uva11181-probability| Given (discrete conditional probability)