UV 11181 Probability | Given (Probability dp)

Source: Internet
Author: User

UV 11181 Probability | Given (Probability dp)

 

Problem G
Probability | Given
Input:
Standard Input

Output:Standard Output

 

N friends go to the local super market together. the probability of their buying something from the market is respectively. after their marketing is finished you are given the information that exactly r of them has bought something and others have bought nothing. given this information you will have to find their individual buying probability.

 

Input

The input file contains at most 50 sets of inputs. The description of each set is given below:

 

First line of each set contains two integers N (1 ≤ N ≤ 20) and r (0 ≤ r ≤ N ). meaning of N and r are given in the problem statement. each of the next N lines contains one floating-point number (0.1 <1) which actually denotes the buying probability of the I-th friend. all probability values shoshould have at most two digits after the decimal point.

 

Input is terminated by a case where the value of N and r is zero. This case shocould not be processes.

 

OutputFor each line of input produce N + 1 lines of output. first line contains the serial of output. each of the next N lines contains a floating-point number which denotes the buying probability of the I-th friend given that exactly r has bought something. these values shocould have six digits after the decimal point. follow the exact format shown in output for sample input. small precision errors will be allowed. for reasonable precision level use double precision floating-point numbers. sample Input Output for Sample Input
3 2 
0.10 
0.20 
0.30 
5 1 
0.10 
0.10 
0.10 
0.10 
0.10 
0 0

Case 1:

0.413043

0.739130

0.847826

Case 2:

0.200000

0.200000

0.200000

0.200000

0.200000

 

Problem-setter: Shahriar Manzoor
Special Thanks: Derek Kisman


There are n people who know the probability of buying things. Now we know that r people have bought things and calculate the probability of I people buying things in turn.

Idea: ans [I] stores the probability that r people buy things and I people buy things. all indicates the probability that n people buy things for r.

 

 

#include
 
  #include
  
   #include
   
    #include#include
    
     #include
     
      #include
      
       #include
       
        #include
        
         #include
         #define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8//typedef __int64 ll;#define fre(i,a,b) for(i = a; i 
          
           = a;i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define ssf(n) scanf("%s", n)#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define bug pf("Hi\n")using namespace std;#define INF 0x3f3f3f3f#define N 25double ans[N];double p[N];int n,r;double dfs(int pos,int left,double pp){ if(pos>n) return left==0 ? pp : 0; double sum=0; if(left){ sum+=dfs(pos+1,left-1,pp*p[pos]); ans[pos]+=sum;} sum+=dfs(pos+1,left,pp*(1-p[pos])); return sum;}int main(){int i,j,ca=0; while(sff(n,r),n+r){fre(i,1,n+1) scanf("%lf",&p[i]);memset(ans,0,sizeof(ans)); double all=dfs(1,r,1); pf("Case %d:\n",++ca); for(i=1;i<=n;i++)pf("%.6lf\n",ans[i]/all);} return 0;}
          
        
       
      
     
    
   
  
 


 

 

 

 

 

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.