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?
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.
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 :
i too feared overflow … so i used prime numbers to calculate factorials and got AC i used the property that n! contains ([n/p]+[n/p^2]+[n/p^3]+…) powers for any prime p
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. ?
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.
@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.