FTRIP - Editorial

PROBLEM LINKS

Practice
Contest

DIFFICULTY

SIMPLE

PREREQUISITES

Simple Math

PROBLEM

In your class of S students, N students are being selected to go on a trip. Your friend circle has M students in the class, including you. You will only enjoy the trip if you have at least K other friends with you.

What is the probability that you will enjoy the trip if you are selected?

QUICK EXPLANATION

Firstly, by BayesTheorem we know that we must find

  • P = number of ways that N students are selected from S students, such that you are selected and at least K of your friends are selected (from your M-1 other friends)
  • Q = number of ways that N students are selected from S students, such that you are selected

The result will be P/Q.

EXPLANATION

Q = (S-1)C(N-1)

Since you are already chosen, we need to select N-1 more students out of the S-1 students left.

To find P, we can iterate over the number of friends chosen.

P = 0

for f = K to min(M-1,N-1)
    P += (M-1)Cf * (S-M)C(N-f-1)

We iterate from K to min(M-1,N-1), since the cap on the number of friends who can come with you on the trip is defined by either the remaining number of slots, or the number of friends you have.

The term that we add for counting the number of ways of choosing f friends should be straight forward. The two terms in the product are

  • number of ways of choosing f friends from a corpus of M-1 friends, and
  • number of ways of choosing the students for the remaining slots from a corpus of students who are not your friends

We can precalculate the values of nCr for all n and r because the limits are small. None of the calculations will overflow.

The overall complexity of the algorithm is dominated by the precalculation of the combinations. Otherwise, the complexity of processing each case is linear.

SETTER’S SOLUTION

Can be found here.

TESTER’S SOLUTION

Can be found here.

3 Likes

I was afraid that this will lead to WA, that I used formulas how to calculate nCr from nCr - 1 or nCr + 1. Because we can see, that in

P += (M-1)Cf * (S-M)CN-f-1

M-1 and S-M are constants…

Shouldn’t the limit be “min(M-1, N-1)” instead of “max(M-1, N-1)”?

I used Pascal’s Triangle for precomputing nCr

Link for solution

:open_mouth: For some reason I thought C[1000,500] had 2500 digits, so I used logarithms instead. http://ww2.codechef.com/viewsolution/2111679

Edit: Turns out I’d checked the digits of 1000!/500!*500! and not 1000!/(500!*500!) >.<

3 Likes

logarithms, good point :wink:

it should. thanks for pointing it out! Fixed.

I used the natural logarithm while checking whether the number whose logarithm was being taken is 0 or not. C(1000,500) had approx. 300 digits, far too big to be stored in an integer. I rather stored the ln of the factorials, and computed ln C(n,k) = ln(n!)-ln(k!)-ln((n-k)!).
My soln. had precalculated 1000 values, and so I was interested to know whether anyone else used logs or not, and was happy to read sanchit_h ’ s comment!

If you don’t have allergy to Pascal , here is my soln :

http://ww2.codechef.com/viewsolution/2080331

Cool solution! :slight_smile:

In the fear of Overflow , I used ‘Gamma function’ for approx. factorial and got AC :slight_smile:

http://discuss.ww2.codechef.com/questions/9554/field-trip-getting-tle. Some body plz give the exact reason for tle. So that I could keep that in mind in future.

  1. Why “k = Max(K, N-1 + M-S)” is used as initial value of k in the solution ?
  2. Its given atleast K friends so loop starts only from K and so on upto min(m-1,n-1).

2nd Part is quite obvious. Please Clarify the first part.

3 Likes

Haven’t you checked the line We iterate from K to min(M-1,N-1), since the cap on the number of friends who can come with you on the trip is defined by either the remaining number of slots, or the number of friends you have. ?

I think you have misinterpreted my question. I want reason for 1st part.

i too feared overflow … so i used prime numbers to calculate factorials and got AC :slight_smile: i used the property that n! contains ([n/p]+[n/p^2]+[n/p^3]+…) powers for any prime p

3 Likes

Suppose, if number of students going N-1(Alice being already selected) is greater than S-M(students who are not Alice’s friend).
i.e, N-1>S-M then atleast N-1-(S-M) = N-1+M-S friends of Alice will certainly go. Hence, we start iteration from max(K, N-1+M-S).
Hope this helps.

2 Likes

Hey… I solved this problem by dynamic programming… using pascal’s triangle.

I was able to solve it using top-down approach.

However getting segmentation error in bottom up approach. :frowning:

finally… bottom dyn progrmmng up also wrkd… :smiley:
http://www.codechef.com/viewsolution/2183722

1 Like

@bit_cracker007 without considering that case also the solution is turning out to be same, could you please tell what is the reason? This solution is the proof, the assert statement should give error but the code was accepted.