# BZOJ1079 [SCOI2008]着色方案 【dp记忆化搜索】

3

1 2 3

10

## 提示

100%的数据满足：1 <= k <= 15, 1 <= ci <= 5

## 题解

f[a][b][c][d][e][k]表示可用1次的颜色有a个，可用2次的颜色有b个，可用3次的颜色有c个，可用4次的颜色有d个，可用5次的颜色有e个，上一次使用的颜色为当前还剩k个的颜色

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u]; k != -1; k = ed[k].nxt)
using namespace std;
const int maxn = 105,maxm = 16,INF = 1000000000,P = 1000000007;
inline int RD(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 1) + (out << 3) + c - '0'; c = getchar();}
return out * flag;
}
int N = 0,K,s[maxn];
LL f[maxm][maxm][maxm][maxm][maxm][6];
bool vis[maxm][maxm][maxm][maxm][maxm][6];
LL dp(int a,int b,int c,int d,int e,int k){
LL t = 0;
if (vis[a][b][c][d][e][k]) return f[a][b][c][d][e][k];
if (a + b + c + d + e == 0) return 1;
if (a) t = (t + (LL)(a - (k == 2)) * dp(a - 1,b,c,d,e,1)) % P;
if (b) t = (t + (LL)(b - (k == 3)) * dp(a + 1,b - 1,c,d,e,2)) % P;
if (c) t = (t + (LL)(c - (k == 4)) * dp(a,b + 1,c - 1,d,e,3)) % P;
if (d) t = (t + (LL)(d - (k == 5)) * dp(a,b,c + 1,d - 1,e,4)) % P;
if (e) t = (t + (LL)e * dp(a,b,c,d + 1,e - 1,5)) % P;
vis[a][b][c][d][e][k] = true;
return f[a][b][c][d][e][k] = t;
}
int main(){
K = RD();
REP(i,K) s[RD()]++;
printf("%lld\n",dp(s[1],s[2],s[3],s[4],s[5],0));
return 0;
}

posted @ 2017-12-28 20:33  Mychael  阅读(63)  评论(0编辑  收藏