BZOJ4710 JSOI2011分特产(容斥原理+组合数学)
显然可以容斥去掉每人都不为空的限制。每种物品分配方式独立,各自算一个可重组合乘起来即可。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define P 1000000007 #define N 1010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,m,a[N],C[N<<1][N<<1],f[N],ans; void inc(int &x,int y){x+=y;if (x>=P) x-=P;} int main() { #ifndef ONLINE_JUDGE freopen("bzoj4710.in","r",stdin); freopen("bzoj4710.out","w",stdout); const char LL[]="%I64d\n"; #else const char LL[]="%lld\n"; #endif m=read(),n=read(); for (int i=1;i<=n;i++) a[i]=read(); C[0][0]=1; for (int i=1;i<=2000;i++) { C[i][0]=C[i][i]=1; for (int j=1;j<i;j++) C[i][j]=(C[i-1][j-1]+C[i-1][j])%P; } for (int i=1;i<=m;i++) { f[i]=1; for (int j=1;j<=n;j++) f[i]=1ll*f[i]*C[a[j]+i-1][i-1]%P; } for (int i=m;i>=1;i--) if (m-i&1) inc(ans,P-1ll*C[m][i]*f[i]%P); else inc(ans,1ll*C[m][i]*f[i]%P); cout<<ans; return 0; }