serasesi's blog

By serasesi, history, 9 years ago, In English

I want to find nCr(n choose r, if you will) modulo a prime. The trouble is that all the numbers i.e. n, r and prime are large and are of the order of 10^9.

One method I could come up with is computing each of the three factorials individually(using FFT and multipoint evaluation) but that seems too tedious. It may also timeout.

Is there anything else I can do ?

  • Vote: I like it
  • -6
  • Vote: I do not like it

| Write comment?
»
9 years ago, hide # |
Rev. 2  
Vote: I like it +9 Vote: I do not like it

There is an ongoing competition with the same problem... :)

»
9 years ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

This problem is from an ongoing contest.

»
8 years ago, hide # |
Rev. 3  
Vote: I like it +8 Vote: I do not like it

Can anyone explain the intended solution, now?
The editorial mentions caching factorials for 0, 106, 2 × 106, 3 × 106, ...!
How can this be achieved (once we have this it is easy to get factorials upto 2 × 109) efficiently?

  • »
    »
    8 years ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    Lets say you need to find x! where 2*109 > x.

    Now, we have stored 0! , 106 ! , 2*106 !, 3*106 ! .... and so on till 2000*106 ! .

    Now there will exist some integer 'z' such that z*106 <= x <= (z+1)*106.

    Since 'z' will always be <=2000, we would have already calculated z*106!
    Now note that the difference between (z+1)*106 and z*106 is 106.

    Therefore we would need to multiply the pre-calculated z*106! value by atmost 106 integers.