zoukankan      html  css  js  c++  java
  • BZOJ 1079: [SCOI2008]着色方案

    1079: [SCOI2008]着色方案

    Time Limit: 10 Sec  Memory Limit: 162 MB

    Submit: 2290  Solved: 1387

    [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

    题解

    因为数量相等的油漆涂的方案是一样的,而且数量不超过5,所以设f[a][b][c][d][e][f]为a种数量为1的油漆,b种数量为2的油漆,c种数量为3的油漆,d种数量为4的油漆,e种数量为5的油漆,上一次涂的数量为f的种类的油漆的方案数。

    然后直接记忆化搜索。

    代码

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<iostream>
    #define LL long long
    using namespace std;
    const int K=16,N=6,mod=1000000007;
    int k;
    int c[K],a[N],vis[K][K][K][K][K][N],f[K][K][K][K][K][N];
    int dfs(int a,int b,int c,int d,int e,int last){
    	if(vis[a][b][c][d][e][last])return f[a][b][c][d][e][last];
    	if(a+b+c+d+e==0)return 1;
    	int temp=0;
    	if(a)temp=(temp+(LL)(a-(last==2))*dfs(a-1,b,c,d,e,1))%mod;
    	if(b)temp=(temp+(LL)(b-(last==3))*dfs(a+1,b-1,c,d,e,2))%mod;
    	if(c)temp=(temp+(LL)(c-(last==4))*dfs(a,b+1,c-1,d,e,3))%mod;
    	if(d)temp=(temp+(LL)(d-(last==5))*dfs(a,b,c+1,d-1,e,4))%mod;
    	if(e)temp=(temp+(LL)e*dfs(a,b,c,d+1,e-1,5))%mod;
    	vis[a][b][c][d][e][last]=1;
    	return f[a][b][c][d][e][last]=(temp%mod);
    }
    int main(){
    	scanf("%d",&k);
    	for(int i=1;i<=k;i++){
    		scanf("%d",&c[i]);
    		a[c[i]]++;
    	}
    	printf("%d
    ",dfs(a[1],a[2],a[3],a[4],a[5],0));
    	return 0;
    }
  • 相关阅读:
    关于ThreadLocal的理解
    常用Linux软件安装
    Spring事务注解@Transactional失效的问题
    使用jackson转换xml格式数据进行响应
    创建简单web程序了解servlet
    JDBC
    StringBuild类
    Canlendar 日期类
    Java Date 时间类的使用
    QWeb
  • 原文地址:https://www.cnblogs.com/chezhongyang/p/7763853.html
Copyright © 2011-2022 走看看