欢迎访问~原文出处——博客园-zhouzhendong

去博客园看该题解


题目传送门 - BZOJ1079


题目概括

  有n个木块排成一行,从左到右依次编号为1~n。你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块。所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n。相邻两个木块涂相同色显得很难看,所以你希望统计任意两个相邻木块颜色不同的着色方案。


题解

  一开始想状压dp,压每种颜色的剩余数。

  发现要超时。

  访问了hzwer大佬的博客,立刻恍然大悟。

  我们可以压每种剩余数的颜色个数!

  具体:

  dp[a][b][c][d][e][t]表示剩余1的颜色有a个,剩余2的颜色有b个,剩余3的颜色有c个,剩余4的颜色有d个,剩余5的颜色有e个,之前选择的那种颜色现在还剩t的方案总数。

  那么复杂度为165×6≈6500000,应该不会超时了。

  记忆化dfs比较好写,所以写了记忆化dfs。


代码

#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef long long LL;
const LL mod=;
int k,tot[];
LL dp[][][][][][];
LL DP(int a,int b,int c,int d,int e,int t){
if (dp[a][b][c][d][e][t]!=-)
return dp[a][b][c][d][e][t];
if (a+b+c+d+e==)
return dp[a][b][c][d][e][t]=;
int A=a-(t==),B=b-(t==),C=c-(t==),D=d-(t==),E=e;
LL &res=dp[a][b][c][d][e][t];
res=;
if (a)
res+=A*DP(a-,b,c,d,e,);
if (b)
res+=B*DP(a+,b-,c,d,e,);
if (c)
res+=C*DP(a,b+,c-,d,e,);
if (d)
res+=D*DP(a,b,c+,d-,e,);
if (e)
res+=E*DP(a,b,c,d+,e-,);
return res%=mod;
}
int main(){
memset(dp,-,sizeof dp);
memset(tot,,sizeof tot);
scanf("%d",&k);
for (int i=,a;i<=k;i++){
scanf("%d",&a);
tot[a]++;
}
printf("%lld",DP(tot[],tot[],tot[],tot[],tot[],));
return ;
}

BZOJ1079 [SCOI2008]着色方案 动态规划的更多相关文章

  1. [luogu2476][bzoj1079][SCOI2008]着色方案【动态规划】

    题目描述 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+-+ck=n.相邻两个木块涂相同色显得很难 ...

  2. BZOJ1079:[SCOI2008]着色方案(DP)

    Description 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块. 所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个 ...

  3. BZOJ1079 [SCOI2008]着色方案 【dp记忆化搜索】

    题目 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块. 所有油漆刚好足够涂满所有木块,即c1+c2+-+ck=n.相邻两个木块涂相同色显得很难看 ...

  4. BZOJ1079: [SCOI2008]着色方案 (记忆化搜索)

    题意:有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块. 所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得很 ...

  5. 2018.10.20 bzoj1079: [SCOI2008]着色方案(多维dp)

    传送门 dp妙题. f[a][b][c][d][e][last]f[a][b][c][d][e][last]f[a][b][c][d][e][last]表示还剩下aaa个可以用一次的,还剩下bbb个可 ...

  6. BZOJ1079 [SCOI2008]着色方案[组合计数DP]

    $有a_{1}个1,a_{2}个2,...,a_{n}个n(n<=15,a_{n}<=5),求排成一列相邻位不相同的方案数.$ 关于这题的教训记录: 学会对于复杂的影响分开计,善于发现整体 ...

  7. bzoj1079: [SCOI2008]着色方案

    dp.以上次染色时用的颜色的数量和每种数量所含有的颜色作状态. #include<cstdio> #include<algorithm> #include<cstring ...

  8. 【记忆化搜索】bzoj1079 [SCOI2008]着色方案

    #include<cstring> #include<cstdio> using namespace std; #define MOD 1000000007 typedef l ...

  9. bzoj1079: [SCOI2008]着色方案

    ci<=5直接想到的就是5维dp了...dp方程YY起来很好玩...写成记忆化搜索比较容易 #include<cstdio> #include<cstring> #inc ...

随机推荐

  1. Entry point (0x08000000) points to a Thumb instruction but is not a valid Thumb code pointer.

    1.菜单 project-options-linker-misc controls加入 --entry Reset_Handler --first __Vectors 2.导入startup_stm3 ...

  2. C# CEF 封装UserControl

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...

  3. Linux配置Tomcat步骤mv apache-tomcat-7.0.82 tomcat

    (一)安装JAVA1.检查java环境 java -version,不存在安装.2.yum -y list java* Loaded plugins: fastestmirror, langpacks ...

  4. 树的dfs序.欧拉序

    dfs序 ==先序,连续一段区间就是子树

  5. Linux 重启网卡失败 Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.

    linux下重启网卡使用命令 : service network restart 时报错: [root@slave01 hadoop]# service network restart Startin ...

  6. 20155228 2016-2017-2 《Java程序设计》第7周学习总结

    20155228 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 Lambda 方法参考的特性,在重用现有的API上扮演了重要的角色.重用现有方法操作,可以避 ...

  7. LeetCode -Reverse Pairs

    my solution: class Solution { public: int reversePairs(vector<int>& nums) { int length=num ...

  8. js 原生 ajax

    //js ajax function Ajax(url,type,data,comFun,sucFun,errFun) { //1.创建XMLHttpRequest对象 var xmlHttpRequ ...

  9. Java SE之字符串常量池

    Reference Document: 什么是字符串常量池?   http://www.importnew.com/10756.html[Recommend] Java常量池理解与总结   http: ...

  10. c# 匿名函数

    int t(){    Func<int> m=()=>3;    return m()+m();}