1079: [SCOI2008]着色方案

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2759  Solved: 1649
[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

Sample Output

10

HINT

 

 100%的数据满足:1 <= k <= 15, 1 <= ci <= 5

注意ci≤5这个限制

设f[a][b][c][d][e],表示可以涂1次的油漆有a种,可以涂2次的油漆有b种,以此类推。。。

于是DP转移为:

  f[a][b][c][d][e]=a*f[a-1][b][c][d][e]+b*f[a+1][b-1][c][d][e]+c*f[a][b+1][c-1][d][e]+d*f[a][b][c+1][d-1][e]+e*f[a][b][c][d+1][e-1]

但是我们还没有考虑同色的情况,因此我们加一维,f[a][b][c][d][e][l],l表示前一木块涂完后的颜色的所剩次数

那么新的转移为:

  f[a][b][c][d][e]=(a-(l==2))*f[a-1][b][c][d][e]+(b-(l==3))*f[a+1][b-1][c][d][e]+(c-(l==4))*f[a][b+1][c-1][d][e]+(d-(l==5))*f[a][b][c+1][d-1][e]+e*f[a][b][c][d+1][e-1]

l==6的情况显然不用考虑

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 
 5 #define LL long long
 6 const int mod=1000000007;
 7 
 8 int k,a[6];
 9 LL f[16][16][16][16][16][6];
10 
11 LL dp(int a,int b,int c,int d,int e,int l)
12 {
13     if((a|b|c|d|e)==0) return f[a][b][c][d][e][l]=1;
14     if(f[a][b][c][d][e][l]) return f[a][b][c][d][e][l];
15     LL t=0;
16     if(a) t+=(a-(l==2))*dp(a-1,b,c,d,e,1);
17     if(b) t+=(b-(l==3))*dp(a+1,b-1,c,d,e,2);
18     if(c) t+=(c-(l==4))*dp(a,b+1,c-1,d,e,3);
19     if(d) t+=(d-(l==5))*dp(a,b,c+1,d-1,e,4);
20     if(e) t+=e*dp(a,b,c,d+1,e-1,5);
21     return f[a][b][c][d][e][l]=t%mod;
22 }
23 
24 int main()
25 {
26     scanf("%d",&k);
27     for(int i=1;i<=k;i++)
28     {
29         int x;
30         scanf("%d",&x);
31         a[x]++;
32     }
33     printf("%lld",dp(a[1],a[2],a[3],a[4],a[5],0));
34     return 0;
35 }
原文地址:https://www.cnblogs.com/InWILL/p/9478183.html