1079: [SCOI2008]着色方案

1079: [SCOI2008]着色方案

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

[ Submit][ Status][ Discuss]

f[a][b][c][d][e][last]:还剩下a瓶可以涂一个格子的颜料,b瓶可以涂两个格子的颜料...上一次是用了一瓶可以涂    last个格子的颜料,方案数

转移显然,可以用记忆化搜索

然后,记得用vis数组来判断有没有访问过,貌似有些f数模1E9+7刚刚好是0
T了好几发。。GG
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

typedef long long LL;
const LL mo = 1000000007;

int n,TOT,tot[6],Max[6],f[17][17][17][17][17][6];
bool vis[17][17][17][17][17][6];

int dfs(int a,int b,int c,int d,int e,int last)
{
	if (a < 0 || b < 0 || c < 0 || d < 0 || e < 0) return 0;
	if (a + b + c + d + e > TOT) return 0;
	if (a > Max[1] || b > Max[2] || c > Max[3] || d > Max[4] || e > Max[5]) return 0;
	if (vis[a][b][c][d][e][last]) return f[a][b][c][d][e][last];
	LL F = 0; vis[a][b][c][d][e][last] = 1;
	if (last == 1) {
		F += 1ll*(a+1)*dfs(a+1,b,c,d,e,0); //F %= mo;
		F += 1LL*(a+1)*dfs(a+1,b,c,d,e,1); //F %= mo;
		F += 1LL*a*dfs(a+1,b,c,d,e,2); //F %= mo;
		F += 1LL*(a+1)*dfs(a+1,b,c,d,e,3); //F %= mo;
		F += 1LL*(a+1)*dfs(a+1,b,c,d,e,4); //F %= mo;
		F += 1LL*(a+1)*dfs(a+1,b,c,d,e,5); F %= mo;
	}
	else if (last == 2) {
		F += 1ll*(b+1)*dfs(a-1,b+1,c,d,e,0); //F %= mo;
		F += 1LL*(b+1)*dfs(a-1,b+1,c,d,e,1); //F %= mo;
		F += 1LL*(b+1)*dfs(a-1,b+1,c,d,e,2); //F %= mo;
		F += 1LL*b*dfs(a-1,b+1,c,d,e,3); //F %= mo;
		F += 1LL*(b+1)*dfs(a-1,b+1,c,d,e,4); //F %= mo;
		F += 1LL*(b+1)*dfs(a-1,b+1,c,d,e,5); F %= mo;
	}
	else if (last == 3) {
		F += 1ll*(c+1)*dfs(a,b-1,c+1,d,e,0); //F %= mo;
		F += 1LL*(c+1)*dfs(a,b-1,c+1,d,e,1); //F %= mo;
		F += 1LL*(c+1)*dfs(a,b-1,c+1,d,e,2); //F %= mo;
		F += 1LL*(c+1)*dfs(a,b-1,c+1,d,e,3); //F %= mo;
		F += 1LL*c*dfs(a,b-1,c+1,d,e,4); //F %= mo;
		F += 1LL*(c+1)*dfs(a,b-1,c+1,d,e,5); F %= mo;
	}
	else if (last == 4) {
		F += 1ll*(d+1)*dfs(a,b,c-1,d+1,e,0); //F %= mo;
		F += 1LL*(d+1)*dfs(a,b,c-1,d+1,e,1); //F %= mo;
		F += 1LL*(d+1)*dfs(a,b,c-1,d+1,e,2); //F %= mo;
		F += 1LL*(d+1)*dfs(a,b,c-1,d+1,e,3); //F %= mo;
		F += 1LL*(d+1)*dfs(a,b,c-1,d+1,e,4); //F %= mo;
		F += 1LL*d*dfs(a,b,c-1,d+1,e,5); F %= mo;
		
	}
	else if (last == 5) {
		F += 1ll*(e+1)*dfs(a,b,c,d-1,e+1,0); //F %= mo;
		F += 1LL*(e+1)*dfs(a,b,c,d-1,e+1,1); //F %= mo;
		F += 1LL*(e+1)*dfs(a,b,c,d-1,e+1,2); //F %= mo;
		F += 1LL*(e+1)*dfs(a,b,c,d-1,e+1,3); //F %= mo;
		F += 1LL*(e+1)*dfs(a,b,c,d-1,e+1,4); //F %= mo;
		F += 1LL*(e+1)*dfs(a,b,c,d-1,e+1,5); F %= mo;
		
	}
	return f[a][b][c][d][e][last] = F;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int x; scanf("%d",&x);
		++tot[x];
	}
	for (int i = 1; i <= 5; i++) {
		for (int j = i; j <= 5; j++)
			Max[i] += tot[j];
		TOT += tot[i];
	}
	f[tot[1]][tot[2]][tot[3]][tot[4]][tot[5]][0] = 1;
	vis[tot[1]][tot[2]][tot[3]][tot[4]][tot[5]][0] = 1;
	cout << dfs(0,0,0,0,0,1);
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值