1079: [SCOI2008]着色方案
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2097 Solved: 1265
[ 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
dp[a][b][c][d][e][last]: 表示能染1块的颜色还有a种,2块的颜色还有b种.........
且上次染得是能够染last次的颜色中的一种
状态转移很好列,注意每次染完次数为x的颜色种的一种时次数为x-1的颜色数会多一种,
这就不用多说了吧。。。。
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 10000000
#define mod 1000000007
#define maxn 505
#define eps 1e-10
ll dp[16][16][16][16][16][16],cnt[16];
ll dfs(ll a,ll b,ll c,ll d,ll e,ll last)
{
ll ans=0;
if(a+b+c+d+e==0)
return dp[a][b][c][d][e][last]=1;
if(dp[a][b][c][d][e][last])
return dp[a][b][c][d][e][last];
if(a>0)
ans+=(a-(last==2))*dfs(a-1,b,c,d,e,1),ans%=mod;
if(b>0)
ans+=(b-(last==3))*dfs(a+1,b-1,c,d,e,2),ans%=mod;
if(c>0)
ans+=(c-(last==4))*dfs(a,b+1,c-1,d,e,3),ans%=mod;
if(d>0)
ans+=(d-(last==5))*dfs(a,b,c+1,d-1,e,4),ans%=mod;
if(e>0)
ans+=e*dfs(a,b,c,d+1,e-1,5),ans%=mod;
return dp[a][b][c][d][e][last]=ans%mod;
}
int main(void)
{
int k,i,x;
scanf("%d",&k);
for(i=1;i<=k;i++)
{
scanf("%d",&x);
cnt[x]++;
}
printf("%lld\n",dfs(cnt[1],cnt[2],cnt[3],cnt[4],cnt[5],0));
return 0;
}