BZOJ 1079 着色方案(DP)
如果把当前格子涂什么颜色当做转移的话,状态则是每个格子的颜色数还剩多少,以及上一步用了什么颜色,这样的状态量显然是5^15.不可取。
如果把当前格子涂颜色数还剩几个的颜色作为转移的话,状态则是每个格子的还剩多少个的颜色数,以及上一步用了还剩几个的颜色,这样的状态量为15^5.
那么定义dp[a][b][c][d][e][last].表示涂到当前格子时还剩1个的颜色数为a.......且上一步涂了还剩last个的颜色。
转移方程就很明显了.
/************************************************************** Problem: 1079 User: freeloop Language: C++ Result: Accepted Time:52 ms Memory:56588 kb ****************************************************************/ # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-8 # define MOD 1000000007 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int res=0, flag=0; char ch; if((ch=getchar())=='-') flag=1; else if(ch>='0'&&ch<='9') res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+(ch-'0'); return flag?-res:res; } void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=20; //Code begin... int vis[6]; LL dp[16][16][16][16][16][6]; bool mark[16][16][16][16][16][6]; LL dfs(int a, int b, int c, int d, int e, int last) { if (mark[a][b][c][d][e][last]) return dp[a][b][c][d][e][last]; LL ans=0; if (a) ans+=dfs(a-1,b,c,d,e,1)*(last==2?a-1:a); if (b) ans+=dfs(a+1,b-1,c,d,e,2)*(last==3?b-1:b); if (c) ans+=dfs(a,b+1,c-1,d,e,3)*(last==4?c-1:c); if (d) ans+=dfs(a,b,c+1,d-1,e,4)*(last==5?d-1:d); if (e) ans+=dfs(a,b,c,d+1,e-1,5)*e; mark[a][b][c][d][e][last]=1; return dp[a][b][c][d][e][last]=ans%MOD; } int main () { int n, x; scanf("%d",&n); FOR(i,1,n) scanf("%d",&x), ++vis[x]; FOR(i,1,5) dp[0][0][0][0][0][i]=1, mark[0][0][0][0][0][i]=1; dfs(vis[1],vis[2],vis[3],vis[4],vis[5],0); printf("%lld\n",dp[vis[1]][vis[2]][vis[3]][vis[4]][vis[5]][0]); return 0; }