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
Sample Output
10
HINT
【样例2】 Input 5 2 2 2 2 2 Output 39480 【样例3】 Input 10 1 1 2 2 3 3 4 4 5 5 Output 85937576 数据规模】 50%的数据满足:1 <= k <= 5, 1 <= ci <= 3 100%的数据满足:1 <= k <= 15, 1 <= ci <= 5
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define inf 1000000
#define ll long long
#define mo 1000000007
ll dp[16][16][16][16][16][16],sum[6];
int n;
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll dfs(int a,int b,int c,int d,int e,int opt)
{
if (a+b+c+d+e==0) return 1;
if (dp[a][b][c][d][e][opt]!=-1) return dp[a][b][c][d][e][opt];
dp[a][b][c][d][e][opt]=0;
if (a) dp[a][b][c][d][e][opt]+=dfs(a-1,b,c,d,e,1)*(a-(opt==2)),dp[a][b][c][d][e][opt]%=mo;
if (b) dp[a][b][c][d][e][opt]+=dfs(a+1,b-1,c,d,e,2)*(b-(opt==3)),dp[a][b][c][d][e][opt]%=mo;
if (c) dp[a][b][c][d][e][opt]+=dfs(a,b+1,c-1,d,e,3)*(c-(opt==4)),dp[a][b][c][d][e][opt]%=mo;
if (d) dp[a][b][c][d][e][opt]+=dfs(a,b,c+1,d-1,e,4)*(d-(opt==5)),dp[a][b][c][d][e][opt]%=mo;
if (e) dp[a][b][c][d][e][opt]+=dfs(a,b,c,d+1,e-1,5)*e,dp[a][b][c][d][e][opt]%=mo;
return dp[a][b][c][d][e][opt];
}
int main()
{
n=read();memset(dp,-1,sizeof(dp));
for (int i=1;i<=n;i++)
{
int x=read();
sum[x]++;
}
printf("%lld",dfs(sum[1],sum[2],sum[3],sum[4],sum[5],0));
return 0;
}