【lucas模板】

求(P是质数)\[C_n^m\% p\]

这个就是卢卡斯定理!!

它的精华就是\[C(n,m,p) \equiv C(n/p,m/p,p)*C(n\% p,m\% p,p)(\% p)\]

然后呢就是一个开心的递归处理,

如果$n < p$就就算一下,逆元阶乘都很easy

上代码!

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 typedef long long lt;
 5 lt n,m,p,T;
 6 lt inv[100010],fac[100010];
 7 lt lucas(lt n,lt m,lt p)
 8 {
 9     if(n<m)return 0;
10     else if(n<p)return fac[n]*inv[m]*inv[n-m]%p;
11     else return lucas(n/p,m/p,p)*lucas(n%p,m%p,p)%p;
12 }
13 int main()
14 {
15     //freopen("testdata.in","r",stdin);
16     scanf("%lld",&T);
17     while(T--)
18     {
19         scanf("%lld%lld%lld",&n,&m,&p);n=n+m;
20         fac[0]=fac[1]=inv[1]=inv[0]=1;
21         for(int i=2;i<=n;i++)fac[i]=fac[i-1]*i%p;
22         for(int i=2;i<=p;i++)inv[i]=(p-p/i)*inv[p%i]%p;
23         for(int i=2;i<=p;i++)inv[i]=inv[i-1]*inv[i]%p;
24         printf("%lld\n",lucas(n,m,p));
25     }
26     return 0;
27 }

原文地址:https://www.cnblogs.com/Qin-Wei-Kai/p/10121757.html

时间: 2024-08-30 00:25:13

【lucas模板】的相关文章

lucas模板

1 ll PowMod(ll a,ll b,ll MOD){ 2 ll ret=1; 3 while(b){ 4 if(b&1) ret=(ret*a)%MOD; 5 a=(a*a)%MOD; 6 b>>=1; 7 } 8 return ret; 9 } 10 ll fac[110219]; 11 ll Get_Fact(ll p) 12 { 13 fac[0]=1; 14 for(int i=1;i<=p;i++) 15 fac[i]=(fac[i-1]*i)%p; 16 }

摘自队友devil的lucas模板

typedef long long LL;const int MOD=1e9+7;LL quick_mod(LL a,LL b){ LL ans=1; a%=MOD; while(b) { if(b&1) { ans=ans*a%MOD; b--; } b>>=1; a=a*a%MOD; } return ans;}LL C(LL n,LL m){ if(m>n) return 0; LL ans=1; for(int i=1; i<=m; i++) { LL a=(n+i

1067 - Combinations---LightOj(Lucas求组合数)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1067 模板求C(n,m)%p, Lucas模板; #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #include <algorithm> #include <map>

HDU 3037 Saving Beans

/* hdu3037 http://acm.hdu.edu.cn/showproblem.php?pid=3037 lucas 模板题 */ #include <cstdio> #include <cmath> const long long Nmax=100005; long long p; long long ex_gcd(long long a,long long b,long long &x,long long &y)//solve x,y in a*x+b

【hdu 3037】Saving Beans

Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2578    Accepted Submission(s): 956 Problem Description Although winter is far away, squirrels have to work day and night to save be

BZOJ 4403 2982 Lucas定理模板

思路: Lucas定理的模板题.. 4403 //By SiriusRen #include <cstdio> using namespace std; const int mod=1000003; #define int long long int cases,N,L,R,fac[mod],inv[mod]; int C(int n,int m){ if(n<m)return 0; if(n<mod&&m<mod)return fac[n]*inv[n-m]

大组合数取模之lucas定理模板,1&lt;=n&lt;=m&lt;=1e9,1&lt;p&lt;=1e6,p必须为素数

typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数 输入:C(n,m)%p 调用lucas(n,m,p) 复杂度:min(m,p)*log(m) ***********************************/ //ax + by = gcd(a,b) //传入固定值a,b.放回 d=gcd(a,b), x , y

模板—数学—Lucas

模板—数学—Lucas Code: #include <cstdio> #include <algorithm> using namespace std; #define N 100010 int n,m,p,inv[N],powq[N]; int lucas(int n,int m) { if(n<m) return 0; if(n<=p&&m<=p) return 1ll*powq[n]*inv[m]%p*inv[n-m]%p; return

Lucas定理模板

用于大组合数对p取模的计算. 1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 #define maxn 100010 8 typedef long long LL; 9 10 LL m,n,p; 11 LL Pow(LL a,LL b,L