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
100%的数据满足:1 <= k <= 15, 1 <= ci <= 5
题解
神题
如果状压的话5^15的复杂度不可接受
但是15^5呢??
设f[i][a][b][c][d][e]为 上一位涂的颜色是还剩下i种可用的,a~e表示能使用1~5次的个数
那么搜一下,对于那些能使用次数相同的颜色,其实本质相同。
那么处理答案的时候判一下就可以了
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const LL mod=1000000007;
LL f[6][16][16][16][16][16];
int h[15],k,c[15];
LL dfs(int last,int a1,int a2,int a3,int a4,int a5)
{
if(a1<0 || a2<0 || a3<0 || a4<0 || a5<0)return 0;
if(a1==0 && a2==0 && a3==0 && a4==0 && a5==0)return 1;
if(f[last][a1][a2][a3][a4][a5]!=0)return f[last][a1][a2][a3][a4][a5];
LL ret=0;
ret=(ret+(a1-((last==2)?1:0))*dfs(1,a1-1,a2,a3,a4,a5))%mod;
ret=(ret+(a2-((last==3)?1:0))*dfs(2,a1+1,a2-1,a3,a4,a5))%mod;
ret=(ret+(a3-((last==4)?1:0))*dfs(3,a1,a2+1,a3-1,a4,a5))%mod;
ret=(ret+(a4-((last==5)?1:0))*dfs(4,a1,a2,a3+1,a4-1,a5))%mod;
ret=(ret+a5*dfs(5,a1,a2,a3,a4+1,a5-1))%mod;
f[last][a1][a2][a3][a4][a5]=ret;
return ret;
}
int main()
{
scanf("%d",&k);
memset(f,0,sizeof(f));
for(int i=1;i<=k;i++)
{
scanf("%d",&c[i]);
h[c[i]]++;
}
printf("%lld\n",dfs(0,h[1],h[2],h[3],h[4],h[5])%mod);
return 0;
}