來自Perm排列計數的悲傷ide
lucas說過spa
C(n,m)%p=C(n%p,m%p)*C(n/p,m/p)%p3d
因而乎code
在好記的狀況下沒有搞原理證實blog
致使PermWA了一上午event
1 ll pow(ll a,ll b){ 2 ll ans=1; 3 while(b){ 4 if(b&1)ans=(ans*a)%p; 5 a=(a*a)%p; 6 b>>=1; 7 } 8 return ans%p; 9 } 10 ll C(int a,int b){ 11 if(a<b)return 0; 12 if(b==0)return 1; 13 return jc[a]*pow(jc[a-b]*jc[b]%p,p-2)%p; 14 } 15 ll lucas(int a,int b){ 16 if(b>a)return 0; 17 if(b==0)return 1; 18 if(a>p||b>p)return C(a%p,b%p)*lucas(a/p,b/p)%p; 19 return C(a,b)%p; 20 } 21 jc[0]=1; 22 for(int i=1;i<=n;++i)jc[i]=jc[i-1]*1ll*i%p;
注意事項:{class
(1)能mod即mod原理
代碼中主要爲相乘運算cli
很容易爆long longsed
(2)特判:
m==0 return 1;
n<m return 0;
(3)lucas細節
只有當
n>mod||m>mod 纔有lucas
}
最後
逆元建議線性推
本人太懶
拿了快速冪取模湊數