题目链接:https://www.luogu.org/problemnew/show/P3807
Lucas定理用来求大组合数对一个质数取模的值,不大于10^5用逆元,大于10^5就用Lucas转化成10^5以内,如果不是质数还要用扩展Lucas
Lucas(n,m,p) = C(n%p,m%p)*Lucas(n/p,m/p,p)
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 100010; 6 int n, m, p, T; 7 long long a[maxn], b[maxn]; 8 long long Lucas(int n, int m, int p) 9 { 10 if(n < m) return 0; 11 else if(n < p) return b[n]*a[m]*a[n-m]%p; 12 else return Lucas(n%p,m%p,p)*Lucas(n/p,m/p,p)%p; 13 } 14 int main() 15 { 16 scanf("%d",&T); 17 while(T--) 18 { 19 scanf("%d%d%d",&n,&m,&p); 20 a[0] = 1, a[1] = 1, b[0] = 1, b[1] = 1; 21 for(int i = 2; i <= m+n; i++) b[i] = b[i-1]*i%p; 22 for(int i = 2; i <= m+n; i++) a[i] = (p-p/i)*a[p%i]%p; 23 for(int i = 2; i <= m+n; i++) a[i] = a[i-1]*a[i]%p; 24 printf("%lld ",Lucas(n+m,m,p)); 25 } 26 return 0; 27 }