1079: [SCOI2008] Coloring scheme
Time Limit:10 Sec Memory limit:162 MB
submit:1126 solved:710
[Submit] [Status] [Discuss]
Description
There are n pieces of wood lined up, numbered from left to right in 1~n. You have a k color of paint, wherein the first color of the paint is enough to tu ci a wood block. All the paint is just enough to fill all the pieces of wood, namely c1+c2+...+ck=n. Adjacent two pieces of wood painted the same color looks very ugly, so you want to count any two adjacent wood color different coloring scheme.
Input
The first behavior is a positive integer k, the second line contains k integers c1, c2, ..., CK.
Output
Outputs an integer, which is the result of modulo 1,000,000,007 for the total number of scenarios.
Sample Input
3
1 2 3
Sample Output
10
HINT
100% of data meet: 1 <= k <=, 1 <= ci <= 5
This problem we can easily think of a 5^15 DP, but obviously this is not possible.
So we need a different kind of thinking, we can notice the ci<=5, so we can convert 5^15 to 15^5.
How to transform it?
We consider using f[a][b][c][d][e] The remaining 1 colors have a a .... Remaining 5
There is a color of E.
So how to ensure that the adjacent color can not put it?
It's good to open one more dimension, save the color of the last position, and then remember to search again.
#include <iostream>#include <cstdio>#include <cstring>using namespace Std;#define LL Long Long#define D 1000000007LL f[6][ -][ -][ -][ -][ -];intc[ -],num[6]={0}, N;bool check[6][ -][ -][ -][ -][ -]={false}; LL DP (int Last,intAintBintCintDinte) {LL num=0;if(a<0|| b<0|| c<0|| d<0|| e<0)return 0;if(!a&&!b&&!c&&!d&&!e)return 1;if(check[ Last][a][b][c][d][e])returnf[ Last][a][b][c][d][e]; Num= (num+ (A + ( Last==2?-1:0))*DP(1, A-1, b,c,d,e))%d; Num= (num+ (b + ( Last==3?-1:0))*DP(2, A +1, B-1, c,d,e))%d; Num= (num+ (c+ ( Last==4?-1:0))*DP(3, a,b+1, C-1, d,e))%d; Num= (num+ (d+ ( Last==5?-1:0))*DP(4, a,b,c+1, d1, e))%d; Num= (num+e*DP(5, a,b,c,d+1, E-1))%d; check[ Last][a][b][c][d][e]=true; f[ Last][a][b][c][d][e]=num;returnf[ Last][a][b][c][d][e];}intMain () {intI scanf"%d", &n); for(i=1; i<=n;++i) {scanf ("%d", &c[i]); num[c[i]]+=1; }printf("%lld\ n", DP (0, num[1],num[2],num[3],num[4],num[5]));}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"bzoj1079" "SCOI2008" "Coloring scheme"