UVa 11181 probability| Given:dfs and Bayesian formulas

Source: Internet
Author: User
Tags cas printf time limit

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.