题目:
http://oj.changjun.com.cn/problem/detail/pid/2027
考虑记忆化搜索。
因为每种颜色能涂的木块<=5,设f[a][b][c][d][e][last]代表当前还剩a个能涂1的木块......上一个涂的木块是剩last的木块
则f[a,b,c,d,e,last]=(a-(last==2))*f[a-1,b,c,d,e]+(b-(last==3))*f[a+1,b-1,c,d,e]+
(c-(last==4))*f[a,b+1,c-1,d,e]+...+( e(last==6))*f[a,b,c,d+1,e-1]。
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
#define mod 1000000007
using namespace std;
int g[],n=,k;
LL f[][][][][][];//第六维判重
bool bj[][][][][][];
LL dfs(int a,int b,int c,int d,int e,int Last)
{
if(a+b+c+d+e==) return ;
if(bj[a][b][c][d][e][Last]) return f[a][b][c][d][e][Last]%mod;
LL ans=;
if(a) ans+=(a-(Last==))*dfs(a-,b,c,d,e,),ans%=mod;//放a
if(b) ans+=(b-(Last==))*dfs(a+,b-,c,d,e,),ans%=mod;//放b
if(c) ans+=(c-(Last==))*dfs(a,b+,c-,d,e,),ans%=mod;//放c
if(d) ans+=(d-(Last==))*dfs(a,b,c+,d-,e,),ans%=mod;//放d
if(e) ans+=(e-(Last==))*dfs(a,b,c,d+,e-,),ans%=mod;//放e
f[a][b][c][d][e][Last]=ans%mod;
bj[a][b][c][d][e][Last]=;
return ans%mod;
}
int main()
{
freopen("!.in","r",stdin);
freopen("!.out","w",stdout);
scanf("%d",&k);
int op;
for(int i=;i<=k;i++)
scanf("%d",&op),g[op]++;//能涂op个的数量
dfs(g[],g[],g[],g[],g[],);
printf("%lld",f[g[]][g[]][g[]][g[]][g[]][]);
return ;
}