zoukankan      html  css  js  c++  java
  • BZOJ 1079 [SCOI2008]着色方案 【记忆化搜索】

    BZOJ 1079 [SCOI2008]着色方案

    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

    题解:

    直接集合(状压)dp肯定是不行的,空间爆!!!考虑两种油漆如果当前都能涂x次,那么他们本质是一样的。

    上记忆化搜索(%hzwer)

    f[a][b][c][d][e][pre] 表示 只能涂一次的油漆有 a 个,只能涂2次的油漆有 b 个, …… 前一种颜色为 pre。

    然后通过乘法原理弄粗结果。。。注意要特判相邻的两个颜色!!!

    代码:

     1 #include<bits/stdc++.h>
     2 #define ll long long
     3 const int mo=1e9+7;
     4 using namespace std;
     5 ll f[16][16][16][16][16][6];
     6 int x[6],n;
     7 bool vis[16][16][16][16][16][6];
     8 inline ll doing(int a,int b,int c,int d,int e,int pre)
     9 {
    10     ll tot=0;
    11     if (vis[a][b][c][d][e][pre]) return f[a][b][c][d][e][pre];
    12     if (a+b+c+d+e==0) return 1;
    13     if (a) tot+=(a-(pre==2))*doing(a-1,b,c,d,e,1);
    14     if (b) tot+=(b-(pre==3))*doing(a+1,b-1,c,d,e,2);
    15     if (c) tot+=(c-(pre==4))*doing(a,b+1,c-1,d,e,3);
    16     if (d) tot+=(d-(pre==5))*doing(a,b,c+1,d-1,e,4);
    17     if (e) tot+=e*doing(a,b,c,d+1,e-1,5);
    18     vis[a][b][c][d][e][pre]=1;
    19     return f[a][b][c][d][e][pre]=(tot%mo);
    20 }
    21 int main()
    22 {
    23     scanf("%d",&n);
    24     for (int i=1; i<=n; i++)
    25     {
    26         int y;
    27         scanf("%d",&y);
    28         x[y]++;
    29     }
    30     printf("%lld",doing(x[1],x[2],x[3],x[4],x[5],0));
    31     return 0;
    32 }
    View Code

    加油加油加油!!!fighting fighting fighting!!!

  • 相关阅读:
    Windows Server 2003 R2 IIS服务的命令行方式重启命令
    DWZ学习记录--关闭loading效果
    td顶部对齐
    struts2 iterator 迭代标签只显示前五条记录
    struts2 select 默认选中
    struts2 select标签
    s:select 标签中list存放map对象的使用
    struts2中<s:select>标签的使用
    eclipse在Windows7 64 位下出现Unhandled event loop exception No more handles
    Jquery焦点图/幻灯片效果 插件 KinSlideshow
  • 原文地址:https://www.cnblogs.com/Frank-King/p/9435123.html
Copyright © 2011-2022 走看看