1079: [SCOI2008]着色方案
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1617 Solved: 985
[ Submit][ Status][ Discuss]
Description
有n个木块排成一行,从左到右依次编号为1~n。你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块。
所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n。相邻两个木块涂相同色显得很难看,所以你希望统计任意两
个相邻木块颜色不同的着色方案。
Input
第一行为一个正整数k,第二行包含k个整数c1, c2, ... , ck。
Output
输出一个整数,即方案总数模1,000,000,007的结果。
Sample Input
3
1 2 3
1 2 3
Sample Output
10
HINT
100%的数据满足:1 <= k <= 15, 1 <= ci <= 5
Source
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
typedef long long LL;
const LL mo = 1000000007;
int n,TOT,tot[6],Max[6],f[17][17][17][17][17][6];
bool vis[17][17][17][17][17][6];
int dfs(int a,int b,int c,int d,int e,int last)
{
if (a < 0 || b < 0 || c < 0 || d < 0 || e < 0) return 0;
if (a + b + c + d + e > TOT) return 0;
if (a > Max[1] || b > Max[2] || c > Max[3] || d > Max[4] || e > Max[5]) return 0;
if (vis[a][b][c][d][e][last]) return f[a][b][c][d][e][last];
LL F = 0; vis[a][b][c][d][e][last] = 1;
if (last == 1) {
F += 1ll*(a+1)*dfs(a+1,b,c,d,e,0); //F %= mo;
F += 1LL*(a+1)*dfs(a+1,b,c,d,e,1); //F %= mo;
F += 1LL*a*dfs(a+1,b,c,d,e,2); //F %= mo;
F += 1LL*(a+1)*dfs(a+1,b,c,d,e,3); //F %= mo;
F += 1LL*(a+1)*dfs(a+1,b,c,d,e,4); //F %= mo;
F += 1LL*(a+1)*dfs(a+1,b,c,d,e,5); F %= mo;
}
else if (last == 2) {
F += 1ll*(b+1)*dfs(a-1,b+1,c,d,e,0); //F %= mo;
F += 1LL*(b+1)*dfs(a-1,b+1,c,d,e,1); //F %= mo;
F += 1LL*(b+1)*dfs(a-1,b+1,c,d,e,2); //F %= mo;
F += 1LL*b*dfs(a-1,b+1,c,d,e,3); //F %= mo;
F += 1LL*(b+1)*dfs(a-1,b+1,c,d,e,4); //F %= mo;
F += 1LL*(b+1)*dfs(a-1,b+1,c,d,e,5); F %= mo;
}
else if (last == 3) {
F += 1ll*(c+1)*dfs(a,b-1,c+1,d,e,0); //F %= mo;
F += 1LL*(c+1)*dfs(a,b-1,c+1,d,e,1); //F %= mo;
F += 1LL*(c+1)*dfs(a,b-1,c+1,d,e,2); //F %= mo;
F += 1LL*(c+1)*dfs(a,b-1,c+1,d,e,3); //F %= mo;
F += 1LL*c*dfs(a,b-1,c+1,d,e,4); //F %= mo;
F += 1LL*(c+1)*dfs(a,b-1,c+1,d,e,5); F %= mo;
}
else if (last == 4) {
F += 1ll*(d+1)*dfs(a,b,c-1,d+1,e,0); //F %= mo;
F += 1LL*(d+1)*dfs(a,b,c-1,d+1,e,1); //F %= mo;
F += 1LL*(d+1)*dfs(a,b,c-1,d+1,e,2); //F %= mo;
F += 1LL*(d+1)*dfs(a,b,c-1,d+1,e,3); //F %= mo;
F += 1LL*(d+1)*dfs(a,b,c-1,d+1,e,4); //F %= mo;
F += 1LL*d*dfs(a,b,c-1,d+1,e,5); F %= mo;
}
else if (last == 5) {
F += 1ll*(e+1)*dfs(a,b,c,d-1,e+1,0); //F %= mo;
F += 1LL*(e+1)*dfs(a,b,c,d-1,e+1,1); //F %= mo;
F += 1LL*(e+1)*dfs(a,b,c,d-1,e+1,2); //F %= mo;
F += 1LL*(e+1)*dfs(a,b,c,d-1,e+1,3); //F %= mo;
F += 1LL*(e+1)*dfs(a,b,c,d-1,e+1,4); //F %= mo;
F += 1LL*(e+1)*dfs(a,b,c,d-1,e+1,5); F %= mo;
}
return f[a][b][c][d][e][last] = F;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n;
for (int i = 1; i <= n; i++) {
int x; scanf("%d",&x);
++tot[x];
}
for (int i = 1; i <= 5; i++) {
for (int j = i; j <= 5; j++)
Max[i] += tot[j];
TOT += tot[i];
}
f[tot[1]][tot[2]][tot[3]][tot[4]][tot[5]][0] = 1;
vis[tot[1]][tot[2]][tot[3]][tot[4]][tot[5]][0] = 1;
cout << dfs(0,0,0,0,0,1);
return 0;
}