BZOJ 1079: [SCOI2008]着色方案 记忆化搜索

1079: [SCOI2008]着色方案

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


记忆化搜索,刚刚入门

本蒟蒻暂时不敢发题解


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;

typedef long long ll;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
void print(ll x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int mod=int(1e9)+7;

bool book[16][16][16][16][16][6];

ll f[16][16][16][16][16][6];

int buc[20];

ll dp(int a,int b,int c,int d,int e,int k)
{
	if(book[a][b][c][d][e][k])return f[a][b][c][d][e][k];
	if(a+b+c+d+e==0)return 1;
	ll res=0;
	if(a)res+=(a-(k==2))*dp(a-1,b,c,d,e,1);
	if(b)res+=(b-(k==3))*dp(a+1,b-1,c,d,e,2);
	if(c)res+=(c-(k==4))*dp(a,b+1,c-1,d,e,3);
	if(d)res+=(d-(k==5))*dp(a,b,c+1,d-1,e,4);
	if(e)res+=e*dp(a,b,c,d+1,e-1,5);
	book[a][b][c][d][e][k]=1;return f[a][b][c][d][e][k]=res%mod;;
}

int main()
{
	int n=read();
	for(int i=1;i<=n;++i)buc[read()]++;
	print(dp(buc[1],buc[2],buc[3],buc[4],buc[5],0));puts("");
	return 0;
}
/*
3
1 2 3

10
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值