BZOJ 1079: [SCOI2008]着色方案

考虑从 \(5\) 下手
把状态设为 \(f_{a_1,a_2,a_3,a_4,a_5,type}\) 表示当前剩下 \(i\) 次涂色机会的颜色种类数为 \(a_i\),而最后一个颜色用的是上一轮剩下 \(type\) 次涂色机会的
记忆化搜索即可
对于这种数据范围小的,可以考虑多设几个维度方便DP

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) >> 1)
#define ll long long
#define db double
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define Edg int ccnt=1,head[N],to[N*2],ne[N*2];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[N*2],ne[N*2],c[N*2];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1e9 + 7;
void M(int &x){if(x>=MOD)x-=MOD;if(x<0)x+=MOD;}
int qp(int a,int b=MOD-2){int ans=1;for(;b;a=1LL*a*a%MOD,b>>=1)if(b&1)ans=1LL*ans*a%MOD;return ans%MOD;}

int dp[20][20][20][20][20][6], k, a[7];

int DP(int a1, int a2, int a3, int a4, int a5, int type) {
	int &ans = dp[a1][a2][a3][a4][a5][type];
	if (!a1 && !a2 && !a3 && !a4 && !a5) return ans = 1;
	if (~ans) return ans;
	ans = 0;
	if (a1) {
		int times = type == 2 ? a1 - 1 : a1;
		M(ans += 1LL * times * DP(a1 - 1, a2, a3, a4, a5, 1) % MOD);
	}
	if (a2) {
		int times = type == 3 ? a2 - 1 : a2;
		M(ans += 1LL * times * DP(a1 + 1, a2 - 1, a3, a4, a5, 2) % MOD);
	}
	if (a3) {
		int times = type == 4 ? a3 - 1 : a3;
		M(ans += 1LL * times * DP(a1, a2 + 1, a3 - 1, a4, a5, 3) % MOD);
	}
	if (a4) {
		int times = type == 5 ? a4 - 1: a4;
		M(ans += 1LL * times * DP(a1, a2, a3 + 1, a4 - 1, a5, 4) % MOD);
	}
	if (a5) {
		int times = a5;
		M(ans += 1LL * times * DP(a1, a2, a3, a4 + 1, a5 - 1, 5) % MOD);
	}
	return ans;
}

int main() {
	memset(dp, -1, sizeof(dp));
	scanf("%d", &k);
	rep (i, 0, k) {
		int x;
		scanf("%d", &x);
		a[x]++;
	}
	printf("%d\n", DP(a[1], a[2], a[3], a[4], a[5], 0));
	return 0;
}
posted @ 2020-02-22 17:32  Mrzdtz220  阅读(90)  评论(0编辑  收藏  举报