标签:class pre size 同余 [1] highlight 组合数 max cpp
Lucas定理:A、B是非负整数,p是质数。A B写成p进制:A=a[n]a[n-1]…a[0],B=b[n]b[n-1]…b[0]。
则组合数C(A,B)与C(a[n],b[n])C(a[n-1],b[n-1])…*C(a[0],b[0]) mod p同余
即:Lucas(n,m,p)=C(n%p,m%p)*Lucas(n/p,m/p,p)
ll fact[maxn], a[maxn], inv[maxn]; //fact为阶乘 void init() { a[0] = a[1] = 1; fact[0] = fact[1] = 1; inv[1] = 1; for( ll i = 2; i <= 100005; i ++ ) { fact[i] = fact[i-1] * i % mod; inv[i] = (mod - mod/i)*inv[mod%i]%mod; a[i] = a[i-1] * inv[i] % mod; } } ll C( ll n, ll m ) { return fact[n]*a[n-m]%mod*a[m]%mod; }
标签:class pre size 同余 [1] highlight 组合数 max cpp
原文地址:https://www.cnblogs.com/l609929321/p/9740055.html