[BZOJ 2142]礼物:扩展Lucas定理
2017-05-24 20:56
549 查看
点击这里查看原题
扩展Lucas定理教程
此题即为C(n,w1) * C(n-w1,w2) * …%mod
扩展Lucas定理教程
此题即为C(n,w1) * C(n-w1,w2) * …%mod
/* User:Small Language:C++ Problem No.:2142 */ #include<bits/stdc++.h> #define ll long long #define inf 999999999 using namespace std; ll mod,n,m,w[10],ans,sum; ll pow(ll a,ll b,ll p){ ll res=1; while(b){ if(b&1LL) res=res*a%p; a=a*a%p; b>>=1LL; } return res; } void exgcd(ll a,ll b,ll &x,ll &y){ if(b==0){ x=1,y=0; return; } exgcd(b,a%b,y,x); y-=a/b*x; } ll inv(ll a,ll b){ ll x,y; exgcd(a,b,x,y); x=(x%b+b)%b; if(!x) x+=b; return x; } ll mul(ll n,ll pi,ll pk){ if(!n) return 1; ll res=1; if(n/pk){ for(ll i=2;i<=pk;i++) if(i%pi) res=res*i%pk; res=pow(res,n/pk,pk); } for(ll i=2;i<=n%pk;i++) if(i%pi) res=res*i%pk; return res*mul(n/pi,pi,pk)%pk; } ll c(ll n,ll m,ll mod,ll pi,ll pk){ if(m>n) return 0; ll a=mul(n,pi,pk),b=mul(m,pi,pk),c=mul(n-m,pi,pk),res=0; ll k=0; for(ll i=n;i;i/=pi) k+=i/pi; for(ll i=m;i;i/=pi) k-=i/pi; for(ll i=n-m;i;i/=pi) k-=i/pi; res=a*inv(b,pk)%pk*inv(c,pk)%pk*pow(pi,k,pk)%pk; res=res*(mod/pk)%mod*inv(mod/pk,pk)%mod; return res; } ll exlucas(ll n,ll m,ll p){ ll res=0,x=p; for(ll i=2;i*i<=x;i++){ if(x%i==0){ ll pk=1; while(x%i==0){ x/=i; pk*=i; } res=(res+c(n,m,p,i,pk))%p; } } if(x!=1) res=(res+c(n,m,p,x,x))%p; return res; } int main(){ freopen("data.in","r",stdin);// scanf("%lld%lld%lld",&mod,&n,&m); ans=1; for(int i=1;i<=m;i++){ scanf("%lld",&w[i]); sum+=w[i]; } if(sum>n){ printf("Impossible\n"); return 0; } for(int i=1;i<=m;i++){ ans=ans*exlucas(n,w[i],mod)%mod; n-=w[i]; } printf("%lld\n",ans); return 0; }
相关文章推荐
- 【bzoj2142】礼物 扩展Lucas定理+中国剩余定理
- [BZOJ2142]礼物(扩展Lucas定理+中国剩余定理)
- BZOJ 2142 礼物 扩展lucas定理
- [BZOJ2142]礼物-扩展lucas定理-中国剩余定理
- BZOJ2142 礼物 [扩展lucas定理]
- 【BZOJ2142】礼物(扩展lucas定理,中国剩余定理合并方程)
- [BZOJ2142] 礼物 - Lucas定理及扩展 - 中国剩余定理 - 扩展欧几里得算法
- [bzoj2142]礼物(扩展lucas定理+中国剩余定理)
- BZOJ2142 礼物-扩展lucas
- [BZOJ2142]礼物(扩展Lucas)
- [BZOJ2142]-扩展Lucas+中国剩余定理
- bzoj2142 礼物 ( 扩展Lucas )
- [BZOJ]2142 礼物 扩展Lucas
- 【BZOJ2142】【扩展lucas】礼物 题解
- 【bzoj2142】【礼物】拓展Lucas定理+孙子定理
- [BZOJ]2142: 礼物 扩展lucas
- BZOJ2142 礼物 【扩展Lucas】
- 【BZOJ2142】礼物【扩展Lucas】
- BZOJ.2142.礼物(扩展Lucas)
- BZOJ 2142: 礼物 [Lucas定理]