Thursday, September 13, 2018

FILED TRIP

  • Problem Description

    Alice's school is planning to take some students from her class on a field trip. Alice is really excited about it. There are a total of S students in her class. But due to budget constraints, the school is planning to take only N students for the trip. These students will be picked randomly. And each student has equal chance of being picked.

    Alice's friend circle has M students including her. Though she is excited about the field trip, she will enjoy it only if there are atleast K of her friends with her on the trip. She is wondering what are the chances of that happening. She needs your help. Tell her the probability that she will enjoy given that she goes on the trip

    Input:
    First line of input contains a single integer T, the number of test cases.
    Each test starts with a single line having 4 space separated integers, S, N, M and K.

    Output:
    For each test case, output a line containing the required probability. The answer will be accepted if the relative error is not more than 10-6.

    Constraints:
    1<=T<=100
    1<=S <=1000
    1<=N <= S
    1 <= M <= S
    0 <= K < M

    Case #1: Every student will be taken to the trip. So all her 4 friends will accompany her to the trip no matter what. 
    Case #2: Alice wants 4 out of her 5 friends to come along with her which isn't possible because the school is willing to pick only 4 students for the trip.
  • CODING ARENA
  • #include <stdio.h>
    long double func(long double n,long double r)
    {
      int i;
      long double res=1.0000000;
      for(i=1;i<=r;++i)
      {
        res=(res*(n-r+i))/i;
      }
      return res;
    }
    int main()
    {
      long double s,n,m,k;
      int t;
      int i;
      scanf("%d",&t);
      while(t--)
      {
        scanf("%Lf %Lf %Lf %Lf",&s,&n,&m,&k);
        long double res=0.0000000;
        i=k;
        while((i<=m-1))
        {
          if((n-i-1)<=(s-m)&&(n-i-1)>=0)
          {
            res=res+func(m-1,i)*func(s-m,n-i-1);
          }
          ++i;
        }
        res=res/func(s-1,n-1);
        printf("%.7Lf\n",res);
      }
      return 0;
    }
      
  • Test Case 1

    Input (stdin)
    3
    
    10 10 5 3
    
    10 4 6 4
    
    3 2 2 1
    
    
    Expected Output
    1.0000000
    
    0.0000000
    
    0.5000000
  • Test Case 2

    Input (stdin)
    3
    
    10 8 5 2
    
    10 4 6 3
    
    3 2 1 1
    
    
    Expected Output
    1.0000000
    
    0.1190476
    
    0.0000000

No comments:

Post a Comment