# BZOJ4710：[Jsoi2011]分特产 （容斥原理+组合数学+DP）

$g\left(i\right)=\sum _{j=1}^{i}{C}_{i}^{j}f\left(j\right)$

$f\left(i\right)=\sum _{j=1}^{i}\left(-1{\right)}^{i-j}{C}_{i}^{j}g\left(j\right)$

g(i)可以通过h数组求得，而答案就是f(n)。时间复杂度$O\left(nm\right)$$O(nm)$

CODE：

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
using namespace std;

const int maxn=1010;
const long long M=1000000007;
typedef long long LL;

LL f[maxn][maxn];
LL sum[maxn][maxn];

LL fac[maxn];
LL nfac[maxn];

int num[maxn];
int n,m;
LL ans=0;

LL C(int N,int mm)
{
LL val=fac[N];
val=val*nfac[mm]%M;
val=val*nfac[N-mm]%M;
return val;
}

int main()
{
freopen("4710.in","r",stdin);
freopen("4710.out","w",stdout);

scanf("%d%d",&n,&m);
int Mn=0;
for (int i=1; i<=m; i++) scanf("%d",&num[i]),Mn=max(Mn,num[i]);

f[0][0]=1;
for (int j=0; j<=Mn; j++) sum[0][j]=1;
for (int i=1; i<=n; i++)
{
for (int j=0; j<=Mn; j++) f[i][j]=sum[i-1][j];
sum[i][0]=f[i][0];
for (int j=1; j<=Mn; j++) sum[i][j]=(sum[i][j-1]+f[i][j])%M;
}

fac[0]=1;
for (LL i=1; i<=n; i++) fac[i]=fac[i-1]*i%M;
nfac[0]=nfac[1]=1;
for (LL i=2; i<=n; i++)
{
LL x=M/i,y=M%i;
nfac[i]=M-x*nfac[y]%M;
}
for (int i=2; i<=n; i++) nfac[i]=nfac[i-1]*nfac[i]%M;

for (int i=1; i<=n; i++)
{
LL temp=1;
for (int j=1; j<=m; j++) temp=temp*f[i][ num[j] ]%M;
temp=temp*C(n,i)%M;
if ((n-i)&1) temp=(M-temp)%M;
ans=(ans+temp)%M;
}
printf("%lld\n",ans);

return 0;
}

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客