题解:记忆化搜索
本质不同的颜色就6种,能图0,1,2,3,4,5块木块的颜色
f[x][a][b][c][d][e]表示上一块图的是本质为x的颜色,本质为1,2,3,4,5的颜色分别剩余a,b,c,d,e种的方案数
转移枚举这一块填什么就行了
还是写错了一个地方没查出来QWQ
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mm=1000000007;
int n,m;
int c[6];
int ans;
int f[6][16][16][16][16][16];//最后1块填的是剩余数为i中的颜色,剩余数为1,2,3,4,5的颜色为a,b,c,d,e种的方案数
int Dp(int x,int a,int b,int c,int d,int e){
if(f[x][a][b][c][d][e])return f[x][a][b][c][d][e];
if(a){
Dp(0,a-1,b,c,d,e);
if(1==x){
f[x][a][b][c][d][e]=(f[x][a][b][c][d][e]+f[0][a-1][b][c][d][e]*1LL*(a-1))%mm;
}else{
f[x][a][b][c][d][e]=(f[x][a][b][c][d][e]+f[0][a-1][b][c][d][e]*1LL*a)%mm;
}
}
if(b){
Dp(1,a+1,b-1,c,d,e);
if(2==x){
f[x][a][b][c][d][e]=(f[x][a][b][c][d][e]+f[1][a+1][b-1][c][d][e]*1LL*(b-1))%mm;
}else{
f[x][a][b][c][d][e]=(f[x][a][b][c][d][e]+f[1][a+1][b-1][c][d][e]*1LL*b)%mm;
}
}
if(c){
Dp(2,a,b+1,c-1,d,e);
if(3==x){
f[x][a][b][c][d][e]=(f[x][a][b][c][d][e]+f[2][a][b+1][c-1][d][e]*1LL*(c-1))%mm;
}else{
f[x][a][b][c][d][e]=(f[x][a][b][c][d][e]+f[2][a][b+1][c-1][d][e]*1LL*c)%mm;
}
}
if(d){
Dp(3,a,b,c+1,d-1,e);
if(4==x){
f[x][a][b][c][d][e]=(f[x][a][b][c][d][e]+f[3][a][b][c+1][d-1][e]*1LL*(d-1))%mm;
}else{
f[x][a][b][c][d][e]=(f[x][a][b][c][d][e]+f[3][a][b][c+1][d-1][e]*1LL*d)%mm;
}
}
if(e){
Dp(4,a,b,c,d+1,e-1);
if(5==x){
f[x][a][b][c][d][e]=(f[x][a][b][c][d][e]+f[4][a][b][c][d+1][e-1]*1LL*(e-1))%mm;
}else{
f[x][a][b][c][d][e]=(f[x][a][b][c][d][e]+f[4][a][b][c][d+1][e-1]*1LL*e)%mm;
}
}
// printf("%d %d %d %d %d %d %d %d\n",x,n-a-b-c-d-e,a,b,c,d,e,f[x][a][b][c][d][e]);
return f[x][a][b][c][d][e];
}
int main(){
scanf("%d",&m);
while(m--){
int x;
scanf("%d",&x);
c[x]++;
n+=x;
}
f[0][0][0][0][0][0]=1;
if(c[1]!=0)ans=(ans+Dp(0,c[1]-1,c[2],c[3],c[4],c[5])*1LL*c[1])%mm;
if(c[2]!=0)ans=(ans+Dp(1,c[1]+1,c[2]-1,c[3],c[4],c[5])*1LL*c[2])%mm;
if(c[3]!=0)ans=(ans+Dp(2,c[1],c[2]+1,c[3]-1,c[4],c[5])*1LL*c[3])%mm;
if(c[4]!=0)ans=(ans+Dp(3,c[1],c[2],c[3]+1,c[4]-1,c[5])*1LL*c[4])%mm;
if(c[5]!=0)ans=(ans+Dp(4,c[1],c[2],c[3],c[4]+1,c[5]-1)*1LL*c[5])%mm;
printf("%d\n",ans);
return 0;
}