#盧卡斯定理 求$C_m^n~mod~p$es6
設$m={a_0}^{p_0}+{a_1}^{p_1}+\cdots+{a_k}^{p_k},n={b_0}^{p_0}+{b_1}^{p_1}+\cdots+{b_k}^{p_k}$ui
則$C_m^n\equiv\prod{C_{a_i}^{b_i}}(mod~p)$es5
#擴展盧卡斯定理遞歸
好像這也不是什麼定理,只是一個計算方法es7
計算$C_m^n~mod~p$,其中$p={p_1}^{q_1}\times{p_2}^{q_2}\times\cdots{p_k}^{q_k}$時,咱們能夠先求出$C_m^n~mod~{p_i}^{q_i}$,而後用CRT合併。擴展
那麼怎麼計算$C_m^n~mod~{p_i}^{q_i}$呢?gc
$C_m^n=\frac{m!}{n!(m-n)!}$,咱們只須要算出$m!,{n!}^{-1},{(m-n)!}^{-1}$,而後乘在一塊兒。方法
z<font color=red>jt</font>大爺:$n!$可能在模${p_i}^{q_i}$的意義下沒有逆元啊,那這就是錯的了啊im
其實這裏求得不是逆元(可能沒有逆元),求出來的是$a\times {p_i}^b(gcd(a,p)=1)$,前面的$a$用逆元,後面的次數加加減減一下就行了es8
問題轉換成求$n!~mod~p^q$
例如$n=19,p=3,q=2$:
$$ \begin{align} &19!\ =&1\times2\times3\times\cdots\times19\ =&(1\times2\times4\times5\times7\times8\cdots\times16\times17\times19)\times(3\times6\times9\times12\times15\times18)\ =&(1\times2\times4\times5\times7\times8\cdots\times16\times17)\times19\times3^6\times(1\times2\times3\times4\times5\times6)\ =&{(1\times2\times4\times5\times7\times8)}^2\times19\times3^6\times(1\times2\times3\times4\times5\times6) \end{align} $$
上面這個式子分爲四部分:
第一部分:${(1\times2\times4\times5\times7\times8)}^2$。這部分的數不超過$p^q$個,能夠暴力算
第二部分:$19$。這部分的數不超過$p^q$個,能夠暴力算
第三部分:$3^6$。這個在最後處理時求出$m!,n!,(m-n)!$分別有多少個$p$(設爲$x,y,z$),則答案要乘上$p^{x-y-z}$
第四部分:$1\times2\times3\times4\times5\times6$。這個是$\lfloor\frac{n}{p}\rfloor!$,能夠遞歸處理