PROBLEM LINKS
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 Bayes' Theorem 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.