BZOJ 2142: 礼物
2017-05-31 17:18
344 查看
BZOJ2142
令sk=∑ki=1wi
ans=∏i=1mCwin−si−1
展开一下。ans=n!∏mi=1wi!∗(n−sm)!
因为p不是质数,将p分解质因数后,对于每一个因子分别处理然后CRT合并就行了,那么就考虑如何求n!∏mi=1wi!∗(n−sm)!。
求法贴别人的好了QAQ,是真的懒得写。。
PoPoQQQ大爷
令sk=∑ki=1wi
ans=∏i=1mCwin−si−1
展开一下。ans=n!∏mi=1wi!∗(n−sm)!
因为p不是质数,将p分解质因数后,对于每一个因子分别处理然后CRT合并就行了,那么就考虑如何求n!∏mi=1wi!∗(n−sm)!。
求法贴别人的好了QAQ,是真的懒得写。。
PoPoQQQ大爷
【代码】
#include <cstdio> #include <iostream> #include <algorithm> #define N 100005 #define INF 0x7fffffff using namespace std; typedef long long ll; ll read() { ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} return x*f; } ll ans,tot,All,Mod; int n,m,cnt; int w ; ll p ,pc ,Fac ; void Divide(int x) { for(int i=2;i*i<=x;i++) if(x%i==0) { p[++cnt]=i;pc[cnt]=1; while(x%i==0) pc[cnt]*=i,x/=i; } if(x>1) p[++cnt]=pc[cnt]=x; } void Exgcd(ll a,ll b,ll &x,ll &y){ if(b){ Exgcd(b,a%b,y,x); y-=(a/b)*x; } else x=1,y=0; } ll Qpow(ll x,ll y,ll mod){ ll rtn=1; while(y) { if(y&1) rtn=rtn*x%mod; x=x*x%mod;y>>=1; } return rtn; } ll Inv(ll a,ll b){ ll x,y; Exgcd(a,b,x,y); return (x%b+b)%b; } ll Calc(ll n,ll p,ll pc) { if(n<p) return Fac ; tot+=n/p; return Fac[n%pc]*Qpow(Fac[pc-1],n/pc,pc)%pc*Calc(n/p,p,pc)%pc; } ll Get_Fact(int p,int pc) { Fac[0]=1; for(int i=1;i<pc;i++) Fac[i]=Fac[i-1]*(i%p?i:1)%pc; tot=0; ll t1=Calc(n,p,pc),t2=1; ll tmp=tot; for(int i=1;i<pc;i++) Fac[i]=Fac[i-1]*(i%p?Inv(i,pc):1)%pc; tot=0; for(int i=1;i<=m;i++) t2=t2*Calc(w[i],p,pc)%pc; return t1*t2%pc*Qpow(p,tmp-tot,pc)%pc; } void Solve() { for(int i=1;i<=cnt;i++) { ll t1=Get_Fact(p[i],pc[i]),t2=Inv(Mod/pc[i],pc[i]); ans=(ans+t1*t2%Mod*(Mod/pc[i])%Mod)%Mod; } printf("%lld\n",ans); } int main() { Mod=read();n=read(),m=read(); for(int i=1;i<=m;i++) All+=(w[i]=read()); if(All>n) { printf("Impossible\n"); return 0; } if(All<n) w[++m]=n-All; Divide(Mod); Solve(); return 0; }
相关文章推荐
- bzoj2142 礼物
- BZOJ 2142 礼物 组合数学+数论
- bzoj 2142: 礼物【中国剩余定理+组合数学】
- bzoj 2142: 礼物(组合数取模终极版) 组合数学+中国剩余定理+exgcd
- 【BZOJ 2142】 礼物
- bzoj 2142: 礼物
- 【bzoj2142】【礼物】拓展Lucas定理+孙子定理
- BZOJ2142 礼物-扩展lucas
- BZOJ 2142 礼物 神TM数论之最终版
- 【bzoj2142】礼物 扩展Lucas定理+中国剩余定理
- 【BZOJ2142】【扩展lucas】礼物 题解
- BZOJ_2142_礼物_扩展lucas+组合数取模+CRT
- [BZOJ2142]礼物(扩展Lucas定理+中国剩余定理)
- bzoj2142 礼物
- BZOJ 2142: 礼物
- [BZOJ 2142]礼物
- bzoj-2142 礼物
- Bzoj2142礼物:组合数取模
- bzoj2142 礼物
- [BZOJ2142]礼物-扩展lucas定理-中国剩余定理