【BZOJ1079】[SCOI2008]着色方案【计数DP】【奇怪的姿势】

【题目链接】

想了一天多,感觉似乎只能是5^15。看了一下题解,发现还能这么玩...

神奇的题。

【iwtwiioi的题解】

/* Footprints In The Blood Soaked Snow */
#include <cstdio>

using namespace std;

typedef unsigned long long ULL;

const int maxn = 16, maxm = 7;
const unsigned int p = 1000000007;

int n, dp[maxn][maxn][maxn][maxn][maxn][maxm];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int dfs(int a, int b, int c, int d, int e, int last) {
	if(!(a | b | c | d | e)) return 1;
	if(dp[a][b][c][d][e][last]) return dp[a][b][c][d][e][last];

	int ans = 0;
	if(a) ans = (ans + (ULL)(a - (last == 2)) * dfs(a - 1, b, c, d, e, 1) % p) % p;
	if(b) ans = (ans + (ULL)(b - (last == 3)) * dfs(a + 1, b - 1, c, d, e, 2) % p) % p;
	if(c) ans = (ans + (ULL)(c - (last == 4)) * dfs(a, b + 1, c - 1, d, e, 3) % p) % p;
	if(d) ans = (ans + (ULL)(d - (last == 5)) * dfs(a, b, c + 1, d - 1, e, 4) % p) % p;
	if(e) ans = (ans + (ULL)(e - (last == 6)) * dfs(a, b, c, d + 1, e - 1, 5) % p) % p;
	return dp[a][b][c][d][e][last] = ans;
}

int cnt[maxn];

int main() {
	n = iread();
	for(int i = 1; i <= n; i++) cnt[iread()]++;
	printf("%d\n", dfs(cnt[1], cnt[2], cnt[3], cnt[4], cnt[5], 0));
	return 0;
}


你可能感兴趣的:(计数dp)