标签:define bit ++ .net bzoj2142 ret 情况 input detail
若不存在可行方案,则输出“Impossible”,否则输出一个整数,表示模P后的方案数。
1 #include <bits/stdc++.h> 2 #define il inline 3 #define RG register 4 #define ll long long 5 6 using namespace std; 7 8 ll a[10],m,n,pp,sum,ans,rhl; 9 10 il ll qpow(RG ll a,RG ll b,RG ll p){ 11 RG ll ans=1; 12 while (b){ 13 if (b&1) (ans*=a)%=p; 14 (a*=a)%=p,b>>=1; 15 } 16 return ans; 17 } 18 19 il ll inv(RG ll a,RG ll p,RG ll pr){ 20 return qpow(a,pr/p*(p-1)-1,pr); 21 } 22 23 il ll get(RG ll n,RG ll p){ 24 RG ll ans=0; for (;n;n/=p) ans+=n/p; return ans; 25 } 26 27 il ll fac(RG ll n,RG ll p,RG ll pr){ 28 if (!n) return 1; RG ll ans=qpow(pp,n/pr,pr); 29 for (RG ll i=2;i<=n%pr;++i) if (i%p) (ans*=i)%=pr; 30 return ans*fac(n/p,p,pr)%pr; 31 } 32 33 il ll C(RG ll n,RG ll m,RG ll p,RG ll pr){ 34 if (n<m) return 0; RG ll a,b,c,ans; pp=1; 35 for (RG ll i=2;i<=pr;++i) if (i%p) (pp*=i)%=pr; 36 ans=qpow(p,get(n,p)-get(m,p)-get(n-m,p),pr); 37 a=fac(n,p,pr),b=fac(m,p,pr),c=fac(n-m,p,pr); 38 (ans*=a*inv(b,p,pr)%pr*inv(c,p,pr)%pr)%=pr; 39 return ans*(rhl/pr)%rhl*inv(rhl/pr%pr,p,pr)%rhl; 40 } 41 42 int main(){ 43 #ifndef ONLINE_JUDGE 44 freopen("gift.in","r",stdin); 45 freopen("gift.out","w",stdout); 46 #endif 47 cin>>rhl>>n>>m; 48 for (RG ll i=1;i<=m;++i) cin>>a[i],sum+=a[i]; 49 if (sum>n) puts("Impossible"),exit(0); ans=1; 50 for (RG ll i=1,P,res,pr;i<=m;++i){ 51 P=rhl,res=0; 52 for (RG ll j=2;j*j<=P;++j){ 53 if (P%j) continue; pr=1; 54 while (P%j==0) P/=j,pr*=j; 55 (res+=C(n,a[i],j,pr))%=rhl; 56 } 57 if (P!=1) (res+=C(n,a[i],P,P))%=rhl; 58 (ans*=res)%=rhl,n-=a[i]; 59 } 60 cout<<ans; return 0; 61 }
标签:define bit ++ .net bzoj2142 ret 情况 input detail
原文地址:http://www.cnblogs.com/wfj2048/p/7514072.html