BZOJ3294: [Cqoi2011]放棋子
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3294
dp+容斥原理。
设g[o][i][j]为第o种颜色,占据了共i行,j列的情况数,有g[o][i][j]=C(i*j,a[o])-∑g[o-1][x][y]*C(i,x)*C(j,y)(i,x;j,y不同时相等)
设f[o][i][j]为前o种颜色,占据了共i行,j列的情况数,有f[o][i][j]=∑f[o-1][x][y]*g[o][x-i][y-j](i,x;j,y不同时相等,一开始令f[0][0][0]=1)
然后把所有f[p][i][j]全部加起来就是答案。
#include<cstring> #include<iostream> #include<algorithm> #include<cstdio> #define rep(i,l,r) for (int i=l;i<=r;i++) #define down(i,l,r) for (int i=l;i>=r;i--) #define clr(x,y) memset(x,y,sizeof(x)) #define mm 1000000009 #define ll long long using namespace std; ll c[1005][1005],f[11][31][31],g[11][31][31],a[11]; ll n,m,p; ll read(){ ll x=0,f=1; char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1; ch=getchar();} while (isdigit(ch)){x=x*10+ch-'0'; ch=getchar();} return x*f; } int main(){ //freopen("in.txt","r",stdin); n=read(); m=read(); p=read(); rep(i,1,p) a[i]=read(); c[0][0]=1; rep(i,1,1000){ c[i][0]=1; rep(j,1,i) c[i][j]=(c[i-1][j]+c[i-1][j-1])%mm; } rep(o,1,p){ rep(i,1,n) rep(j,1,m) if (i*j>=a[o]&&max(i,j)<=a[o]){ g[o][i][j]=c[i*j][a[o]]; rep(x,1,i) rep(y,1,j) { if (x==i&&j==y) continue; g[o][i][j]=(g[o][i][j]-g[o][x][y]*c[i][x]%mm*c[j][y]%mm+mm)%mm; } } } f[0][0][0]=1; rep(o,1,p) rep(i,1,n) rep(j,1,m){ rep(x,0,i) rep(y,0,j) { if (i*j<a[o]) continue; if (x==i&&j==y) continue; f[o][i][j]=(f[o][i][j]+f[o-1][x][y]*g[o][i-x][j-y]%mm*c[i][x]%mm*c[j][y])%mm; } } ll ans=0; rep(i,1,n) rep(j,1,m) ans=(ans+f[p][i][j]*c[n][i]%mm*c[m][j])%mm; printf("%lld\n",ans); return 0; }