PROBLEM LINKSDIFFICULTYSIMPLE PREREQUISITESSimple Math PROBLEMIn 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 EXPLANATIONFirstly, by Bayes' Theorem we know that we must find
The result will be P/Q. EXPLANATIONQ = ^{(S1)}C_{(N1)} Since you are already chosen, we need to select N1 more students out of the S1 students left. To find P, we can iterate over the number of friends chosen. P = 0 for f = K to min(M1,N1) P += ^{(M1)}C_{f} * ^{(SM)}C_{(Nf1)} We iterate from K to min(M1,N1), 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
We can precalculate the values of ^{n}C_{r} 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 SOLUTIONCan be found here. TESTER'S SOLUTIONCan be found here.
This question is marked "community wiki".
asked 14 May '13, 15:26

2nd Part is quite obvious. Please Clarify the first part. answered 15 May '13, 10:45
Haven't you checked the line
(15 May '13, 12:16)
I think you have misinterpreted my question. I want reason for 1st part.
(15 May '13, 12:23)
2
Suppose, if number of students going N1(Alice being already selected) is greater than SM(students who are not Alice's friend). i.e, N1>SM then atleast N1(SM) = N1+MS friends of Alice will certainly go. Hence, we start iteration from max(K, N1+MS). Hope this helps.
(15 May '13, 18:35)
@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.
(13 Oct '13, 12:28)

:O 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!) >.< answered 14 May '13, 17:36

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 answered 15 May '13, 15:25

I was afraid that this will lead to WA, that I used formulas how to calculate ^{n}C_{r} from ^{n}C_{r  1} or ^{n}C_{r + 1}. Because we can see, that in P += ^{(M1)}C_{f} * ^{(SM)}C_{Nf1} M1 and SM are constants... answered 14 May '13, 15:46

Shouldn't the limit be "min(M1, N1)" instead of "max(M1, N1)"? answered 14 May '13, 16:05

I used Pascal's Triangle for precomputing nCr answered 14 May '13, 16:59

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((nk)!). 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 : answered 14 May '13, 18:06

In the fear of Overflow , I used 'Gamma function' for approx. factorial and got AC :) answered 14 May '13, 20:16

Hey.. I solved this problem by dynamic programming.. using pascal's triangle. I was able to solve it using topdown approach. However getting segmentation error in bottom up approach. :( answered 28 May '13, 20:48
1
finally.. bottom dyn progrmmng up also wrkd.. :D http://www.codechef.com/viewsolution/2183722
(28 May '13, 21:22)

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