【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 ...
随机推荐
- js获取键盘按键响应事件(兼容各浏览器)
<script type="text/javascript" language="JavaScript" charset="UTF-8" ...
- mvc的删除
有UserInfo一个实体类,db代表请求上下文 属性有ID,UserName,UserPass,Email var users=from c in db.UserInfo select c; var ...
- NativeExcel 读取文件
class function T_EShopDataBill.ImportData(const AFileName: String; AList: T_EShopDataModelList; var ...
- (转载)sql语句解决分页问题
<来源网址:http://www.delphifans.com/infoview/Article_353.html>sql语句解决分页问题日期:2005年1月17日 作者:treemon2 ...
- delphi 自带报告内存泄漏
//报告内存泄漏 ReportMemoryLeaksOnShutdown := true;
- A3992学习记录
ATmega64+A3992驱动步进电机 //ATmega 64a 电机驱动板程序//编译环境 AVR Studio 4.17/AVR GCC//系统外部时钟16M//作者:虞恺 //日期:2012. ...
- 一个基于python的即时通信程序
5月17日更新: 广播信息.用户列表.信息确认列表以及通信信息,从原来的用字符串存储改为使用字典来存储,使代码更清晰,更容易扩展,具体更改的格式如下: 广播信息(上线): { 'status': 信息 ...
- Linux命令执行顺序— ||和&&和;
command1 && command2: &&左边的command1执行成功(返回0表示成功)后,&&右边的command2才能被执行. comman ...
- 类的本质、description方法、SEL、NSLog输出增强
一.类的本质 1.类也是个对象 其实类也是一个对象,是Class类型的对象,简称“类对象” Class类型的定义 typedef struct objc_class *Class; 类名就代表着类对象 ...
- bnuoj 4187 GCC (数论)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=4187 [题意]:如题 [题解]:取n,m的最小值进行遍历就可以了: 注意 0 1 这组测试数据 [c ...