1. 程式人生 > >[BZOJ1079][SCOI2008]著色方案 dp

[BZOJ1079][SCOI2008]著色方案 dp

總數 log esc d+ 很難 hint tput scu 輸出

1079: [SCOI2008]著色方案

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

Source

設F[a][b][c][d][e][f]表示剩余1,2,3,4,5個塊的個數分別為a,b,c,d,e且上一次染的是剩余f塊的方案數。

之後記憶化搜索即可。

技術分享圖片
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cstdio>
 5
#include<cmath> 6 #include<algorithm> 7 #define maxn 800000 8 #define mod 1000000007 9 using namespace std; 10 int F[16][16][16][16][16][6]; 11 int sum[20]; 12 int k; 13 long long dp(int a,int b,int c,int d,int e,int f) { 14 long long ans=F[a][b][c][d][e][f]; 15 if(ans) return ans; 16 if
(a+b+c+d+e==0) return F[a][b][c][d][e][f]=1; 17 if(a) ans+=(a-(f==2))*dp(a-1,b,c,d,e,1); 18 if(b) ans+=(b-(f==3))*dp(a+1,b-1,c,d,e,2); 19 if(c) ans+=(c-(f==4))*dp(a,b+1,c-1,d,e,3); 20 if(d) ans+=(d-(f==5))*dp(a,b,c+1,d-1,e,4); 21 if(e) ans+=e*dp(a,b,c,d+1,e-1,5); 22 ans%=mod; 23 return F[a][b][c][d][e][f]=ans; 24 } 25 int main() { 26 scanf("%d",&k); 27 for(int i=1;i<=k;i++) {int x;scanf("%d",&x);sum[x]++;} 28 printf("%lld\n",dp(sum[1],sum[2],sum[3],sum[4],sum[5],0)); 29 }
View Code

[BZOJ1079][SCOI2008]著色方案 dp