Finding binomial coefficient for large n and k modulo m

后端 未结 4 1365
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 04:06

I want to compute nCk mod m with following constraints:

n<=10^18

k<=10^5

m=10^9+7

I have read this article:

Calculating Binomia

4条回答
  •  离开以前
    2021-01-03 04:11

    First, you don't need to pre-compute and store all the possible aCb values! they can be computed per case.

    Second, for the special case when (k < m) and (n < m^2), the Lucas theorem easily reduces to the following result:

    (n choose k) mod m = ((n mod m) choose k) mod m

    then since (n mod m) < 10^9+7 you can simply use the code proposed by @kfx.

提交回复
热议问题