Describes a fast seek \ (\ dbinom {n} { m} \) method.
We know \ (\ dbinom {n} { m} \ mod (1e9 + 7) = \ frac {n \ times (n-1) \ times \ dots \ times (n-m + 1)} {1 \ times 2 \ Times \ DOTS \ m} Times \ MOD (+ 1E9. 7) \) .
To facilitate expression, we set \ (X = n-\ Times (n--. 1) \ Times \ DOTS \ Times (n--m +. 1) \) (i.e., molecules on the right), \ (Y =. 1 \ Times 2 \ Times \ DOTS \ Times m \) (i.e., the denominator on the right).
Then there
\[\dbinom{n}{m} \mod (1e9+7)=\frac{x}{y} \mod (1e9+7)\]
Of Fermat's little theorem
\ [Y \ times and ^ {(^ 9 + 7 10) -2} \ mod (10 ^ 9 + 7) = 1 \]
At the same time on both sides by \ (y ^ {- 1} \) to give
\ [^ {And (10 ^ 9 + 7) -2} \ mod (10 ^ 9 + 7) = y ^ {- 1} \]
Since \ (y ^ {- 1} \) is \ (Y \) is inverse, so there
\[\frac{x}{y} \mod(10^9+7) = x \times y^{(10^9+7)-2} \mod (10^9+7)\]
The code is not difficult to implement.
inline LL qpow(LL a, LL b) //快速幂,用于求逆元
{
LL res = 1;
while (b)
{
if (b & 1) res = (res * a) % mod;
a = a * a % mod, b >>= 1;
}
return res % mod;
}
inline LL work(int n, int m) //求 C(n, m)
{
LL s = 1, c = 1;
for (int i = 1; i <= m; i+=1)
s = (s * i) %mod; //计算分母
for (int i = n - m + 1; i <= n; i++)
c = (c * i) % mod; //计算分子
return (c * qpow(s, mod - 2)) % mod; //求出值
}
PS from the portion of the reference AtCoder Beginner Contest 156 D problem explanations