【HDOJ】4363 Draw and paint
看题解解的。将着色方案映射为40*40*5*5*5*5*2个状态,40*40表示n*m,5*5*5*5表示上下左右相邻块的颜色,0表示未着色。
2表示横切或者竖切。
基本思路是记忆化搜索然后去重,关键点是可能未切前当前块已经着色了。
/* 4363 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int mod = 1e9+;
int dp[][][][][][][]; int calc(int x, int y, int u, int d, int l, int r, int dir) {
if (dp[x][y][u][d][l][r][dir] >= )
return dp[x][y][u][d][l][r][dir]; int& ret = dp[x][y][u][d][l][r][dir]; ret = ;
if ((x==&&dir==) || (y==&&dir==)) {
rep(i, , )
if (i!=u && i!=d && i!=l && i!=r)
++ret;
return ret;
} if (dir) {
rep(i, , y) {
rep(j, , ) {
if (j!=u && j!=d && j!=l) {
ret = (ret + calc(x, y-i, u, d, j, r, )) % mod;
}
if (j!=u && j!=d && j!=r) {
ret = (ret + calc(x, i, u, d, l, j, )) % mod;
}
}
} int tmp = ;
rep(i, , ) {
if (i!=u && i!=d && i!=l) {
rep(j, , ) {
if (j!=u && j!=d && j!=r && j!=i)
++tmp;
}
}
} ret = (ret + mod - tmp*(y-)) % mod;
rep(i, , )
if (i!=u && i!=l && i!=r && i!=d)
++ret; ret %= mod;
} else {
rep(i, , x) {
rep(j, , ) {
if (j!=u && j!=l && j!=r) {
ret = (ret + calc(x-i, y, j, d, l, r, )) % mod;
}
if (j!=d && j!=l && j!=r) {
ret = (ret + calc(i, y, u, j, l, r, )) % mod;
}
}
} int tmp = ;
rep(i, , ) {
if (i!=u && i!=l && i!=r) {
rep(j, , ) {
if (j!=d && j!=l && j!=r && j!=i)
++tmp;
}
}
} ret = (ret + mod - tmp*(x-)) % mod;
rep(i, , )
if (i!=u && i!=l && i!=r && i!=d)
++ret; ret %= mod;
} return ret;
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t;
int n, m;
int ans; memset(dp, -, sizeof(dp));
scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &m);
ans = calc(n, m, , , , , );
printf("%d\n", ans);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】4363 Draw and paint的更多相关文章
- 【HDOJ】4056 Draw a Mess
这题用线段树就MLE.思路是逆向思维,然后每染色一段就利用并查集将该段移除,均摊复杂度为O(n*m). /* 4056 */ #include <iostream> #include &l ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
随机推荐
- DirectSound学习(三)--类、方法、属性翻译
DirectSound.Device :Contains methods and properties used to create buffer objects, manage devices, a ...
- Android sqlite
转载 http://blog.csdn.net/s874154731/article/details/7086238 import android.content.Context; import an ...
- APACHE支持.htaccess以及 No input file specified解决方案
在你的Apache安装文件夹conf里找到httpd.conf文件 搜索LoadModule rewrite_module modules/mod_rewrite.so 如果前面有注释符号#,请去掉. ...
- CentOS6.5 MySQL 配置设置总结笔记
三.登录MySQL 登录MySQL的命令是mysql, mysql 的使用语法如下: mysql [-u username] [-h host] [-p[password]] [dbname] u ...
- linux设备驱动模型(kobject与kset)
Linux设备模型的目的:为内核建立一个统一的设备模型,从而又一个对系统结构的一般性抽象描述.换句话说,Linux设备模型提取了设备操作的共同属性,进行抽象,并将这部分共同的属性在内核中实现,而为需要 ...
- Asp.net MVC 如何向webform一样在IIS里添加虚拟目录
相信很多用webform的程序猿都习惯性的使用虚拟目录的形式来对一个程序添加新的功能,那么在mvc下该如何来弄呢? 首先得有一个项目基层的项目,然后我们在这个项目的基础上新增一个功能模块,例如信息发布 ...
- EXTJS 4.2 资料 控件之combo 联动
写两个数据源: 1.IM_ST_Module.js { success:true, data:[ { ModuleId: '1', ModuleName: '资讯' } , { ModuleId: ' ...
- SNMP的工作原理&软件开发
SNMP(Simple Network Management Protocol,简单网络管理协议)首先是由IETF的研究小组为了解决Internet上的路由器管理问题而提出的.SNMP的设计原则是简单 ...
- NGUI自适应分辨率,黑边自动填充, 无黑边,等比例缩放
原地址:http://game.ceeger.com/forum/read.php?tid=16571 1,给背景添加一个UIstretch, .将style选择最后一个FitInternalKeep ...
- fiddler插件开发step by step 1
Fiddler 是优秀的抓包工具,有着众多的优秀插件.Fiddler 软件是由C#语言开发的,运行在.net Framework 框架之上,所以我们也可以使用vs来开发自己的Fiddler插件,下面就 ...