题目

如题。

算法

就是刚学习的插头DP。

从前往后从后往前分别进行一次DP。

要点

合法的括号序列只有103个

如何合并两次dp的信息

一开始犯傻了,以为当且仅当两个轮廓线的状态相同才是合法的方案。其实很容易举出反例。

如果直接枚举的话,每次询问的时间复杂度是\(O(103^2 m)\)。

为了加快速度,可以把所有合法的方案先列举出来(就是预处理),只有\(103^2\)个。每次询问的复杂度优化为\(O(103^2)\)。

时间复杂度

\(O(103 \cdot n \cdot m + 103^2 * m + 103^2 Q)\)

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long i64; const int MaxN = 1000;
const int MaxM = 6;
const int MaxS = 103;
const int MOD = (int) 1e9 + 7; int n, m;
int A[MaxN][MaxM]; #define getbit(s, i) ((s) >> ((i) << 1) & 3)
#define clrbit(s, i) ((s) & ~(3 << ((i) << 1)))
#define clrbit2(s, i, j) (clrbit( clrbit(s, i), j))
#define cpbit(s, i, x) ((s) ^ ((x) << ((i) << 1)))
#define revbit(s, i) ((s) ^ (1 << ((i) << 1))) template <class T>
void addIt(T &a, const T &b)
{
a += b;
if (a >= MOD) a -= MOD;
} struct Hash
{
int n;
pair<int, int> A[MaxS]; struct Link
{
int to;
Link *next;
}pool[MaxS], *pool_cur, *info[MaxS]; void sort()
{
std::sort(A, A + n);
} int find(const int &x)
{
pair<int, int> *p = lower_bound(A, A + n, make_pair(x, -1));
if (p->first == x) return p->second;
return 0;
} Hash()
{
pool_cur = pool;
} void update()
{
int i = 0;
while (i < n)
{
if (getbit(A[i].first, m))
A[i] = A[-- n];
else
i ++;
}
} void push(const int &s, const int &x)
{
int hash = s % MaxS;
for (Link *p = info[hash]; p; p = p->next)
{
if (A[p->to].first == s)
{
addIt(A[p->to].second, x);
return;
}
}
pool_cur->to = n;
pool_cur->next = info[hash];
info[hash] = pool_cur ++;
A[n ++] = make_pair(s, x);
}
}dp[2][MaxN + 1][MaxM]; int getbracket0(const int &s, const int &i)
{
int cnt = 1;
for (int k = i + 1; k < m; k ++)
{
int t = getbit(s, k);
if (t)
{
if (t & 1) cnt --;
else cnt ++;
}
if (!cnt) return k;
}
return -1;
} int getbracket1(const int &s, const int &i)
{
int cnt = -1;
for (int k = i - 1; k >= 0; k --)
{
int t = getbit(s, k);
if (t)
{
if (t & 1) cnt --;
else cnt ++;
}
if (! cnt) return k;
}
return -1;
} void Process(Hash (*dp)[MaxM])
{
dp[0][0].push(0, 1); for (int i = 0; i < n; i ++)
{
for (int j = 0; j < m; j ++)
{
Hash &cur = dp[i][j], &next = j == m - 1 ? dp[i + 1][0] : dp[i][j + 1];
for (int k = 0; k < cur.n; k ++)
{
int s = cur.A[k].first;
int x = cur.A[k].second;
int L = getbit(s, m);
int U = getbit(s, j); #define send(st) next.push(st, x) if (!A[i][j])
{
if (L && U)
{
L &= 1, U &= 1;
if (!L && !U)
send(revbit( clrbit2(s, j, m), getbracket0(s, j)));
else if (L && !U)
send(clrbit2(s, j, m));
else if (L && U)
send(revbit( clrbit2(s, j, m), getbracket1(s, j)));
}
else if (L)
{
send(s);
send(clrbit( cpbit(s, j, L), m));
}
else if (U)
{
send(s);
send(clrbit( cpbit(s, m, U), j));
}
else
{
send(s);
send(cpbit( cpbit(s, j, 2), m, 3));
}
}
else if (!L && !U)
send(s);
} cerr << next.n << endl;
if (j == m - 1) next.update();
}
}
} int P[729], Pn; // 3^m
int Qn;
pair<int, int> Q[729 * 729]; void dfs(int i, int cnt, int s)
{
if (i == m)
{
if (! cnt && s)
{
P[Pn ++] = s;
}
}
else
{
dfs(i + 1, cnt + 1, s ^ (2 << (i << 1)));
if (cnt)
dfs(i + 1, cnt - 1, s ^ (3 << (i << 1)));
dfs(i + 1, cnt, s);
}
} int getbracket(const int &a, const int &j)
{
if (getbit(a, j) & 1)
{
int cnt = -1;
for (int i = j - 1; i >= 0; i --)
{
int t = getbit(a, i);
if (t)
{
if (t & 1) cnt --;
else cnt ++; if (! cnt) return i;
}
}
}
else
{
int cnt = 1;
for (int i = j + 1; i < m; i ++)
{
int t = getbit(a, i);
if (t)
{
if (t & 1) cnt --;
else cnt ++;
if (! cnt) return i;
}
}
}
return -1;
} bool eliminate(int &a, int at, int &b, const int &start)
{
if (getbit(a, at) == 0) return at == start;
int other = getbracket(a, at);
a = clrbit2(a, at, other);
if (!eliminate(b, other, a, start)) return false;
return true;
} bool check(int a, int b)
{
for (int i = 0; i < n; i ++)
if (getbit(a, i))
{
if (! eliminate(a, i, b, i)) return false;
return a == 0 && b == 0;
}
return false;
} int main()
{
int k;
scanf("%d%d", &n, &m);
scanf("%d", &k); while (k --)
{
int x, y;
scanf("%d%d", &x, &y);
x --, y --;
A[x][y] = 1;
} Process(dp[0]);
for (int i = 0; i < n >> 1; i ++)
{
int j = n - i - 1;
for (int k = 0; k < m; k ++)
swap(A[i][k], A[j][k]);
}
Process(dp[1]); dfs(0, 0, 0);
for (int i = 0; i < Pn; i ++)
for (int j = 0; j < Pn; j ++)
{
if (check(P[i], P[j]))
{
Q[Qn ++] = make_pair(P[i], P[j]);
}
} scanf("%d", &k); for (int i = 0; i < n; i ++)
{
dp[0][i][0].sort();
dp[1][i][0].sort();
}
while (k --)
{
int x, y;
scanf("%d%d", &x, &y);
x --, y --;
int ans = 0;
for (int i = 0; i < Qn; i ++)
{
if (getbit(Q[i].first, y))
{
addIt(ans, (int) ((i64) dp[0][x + 1][0].find(Q[i].first) *
dp[1][n - x - 1][0].find(Q[i].second) % MOD));
}
}
printf("%d\n", ans);
} return 0;
}

清华集训2014 day2 task1 简单回路的更多相关文章

  1. 清华集训2014 day1 task1 玛里苟斯

    题目 这可算是描述很简单的一道题了!但是不简单. \(S\)是一个可重集合,\(S = \{a_1, a_2, \dots, a_n \}\). 等概率随机取\(S\)的一个子集\(A = \{a_{ ...

  2. 清华集训2014 day2 task3 矩阵变换

    题目 算法 稳定婚姻系统(其实就是贪心) 一个方案不合法,当且仅当下面这种情况: 设第\(i\)行选了数字\(x\),如果第\(j\)行有一个\(x\)在第\(i\)行的\(x\)后面,并且第\(j\ ...

  3. uoj 41 【清华集训2014】矩阵变换 婚姻稳定问题

    [清华集训2014]矩阵变换 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/41 Description 给出 ...

  4. AC日记——【清华集训2014】奇数国 uoj 38

    #38. [清华集训2014]奇数国 思路: 题目中的number与product不想冲: 即为number与product互素: 所以,求phi(product)即可: 除一个数等同于在模的意义下乘 ...

  5. 【BZOJ3814】【清华集训2014】简单回路 状压DP

    题目描述 给你一个\(n\times m\)的网格图和\(k\)个障碍,有\(q\)个询问,每次问你有多少个不同的不经过任何一个障碍点且经过\((x,y)\)与\((x+1,y)\)之间的简单回路 \ ...

  6. 清华集训2014 sum

    清华集训2014sum 求\[∑_{i=1}^{n}(-1)^{⌊i√r⌋}\] 多组询问,\(n\leq 10^9,t\leq 10^4, r\leq 10^4\). 吼题解啊 具体已经讲得很详细了 ...

  7. UOJ#46. 【清华集训2014】玄学

    传送门 分析 清华集训真的不是人做的啊嘤嘤嘤 我们可以考虑按操作时间把每个操作存进线段树里 如果现在点x正好使一个整块区间的右端点则更新代表这个区间的点 我们不难发现一个区间会因为不同的操作被分成若干 ...

  8. 清华集训2014 day1 task3 奇数国

    题目 题目看起来好像很难的样子!其实不然,这是最简单的一道题. 算法 首先要注意的是: \(number \cdot x + product \cdot y = 1\) ,那么我们称\(number\ ...

  9. 【UOJ#37】 [清华集训2014] 主旋律

    题目链接 题目描述 给定一张强联通图,求有多少种边的存在情况满足图依然强联通. \(n\leq15\) Sol 首先正难则反,考虑用总数减去不强联通的. 考虑一张不强联通的图,缩点后一定是一个 DAG ...

随机推荐

  1. JAVA知识的相关积累--用于自己以后查找

    基础: Properties类操作文件,主要是对配置文件的操作.java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容 ...

  2. POJ 2250 Compromise(LCS)

    POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...

  3. 模仿jquery的一些实现 第二版

    具体如下: //w作为window的形参,就表示window (function(w) { // 定义一个全局的window.wyl变量,就类似于jquery里的$,Jquery对象 w.wyl; / ...

  4. 转:requirejs:让人迷惑的路径解析(~~不错)

    接触过requirejs的童鞋可能都知道,无论是通过define来定义模块,还是通过require来加载模块,模块依赖声明都是很重要的一步.而其中涉及到的模块路径解析,对于新手来说,有的时候会让人觉得 ...

  5. MFC不使用对话框资源模版创建对话框

    在MFC程序中使用对话框时首先在资源模版里创建对话框资源,然后DoModal()或者CReate显示出模式对话框或者非模式对话框,这样创建出的对话框移植性差,从一个工程移动到另一个工程比较麻烦. 在M ...

  6. BZOJ 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐

    1609: [Usaco2008 Feb]Eating Together麻烦的聚餐 Description 为了避免餐厅过分拥挤,FJ要求奶牛们分3批就餐.每天晚饭前,奶牛们都会在餐厅前排队入内,按F ...

  7. STL 源代码剖析 算法 stl_algo.h -- partition

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partition ------------------------------------ ...

  8. LeetCode——Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  9. onmouseover和onmouseout的烦恼

    一个DIV层,当鼠标移进的时候会触发onmouseover,移出的时候会触发onmouseout.   非常easy的逻辑,这也是我们想要的!但随之烦恼也就来了:onmouseover并不会仅仅在移进 ...

  10. QQ邮箱添加公司邮箱步骤

    经领导提示,发现QQ邮箱可以添加公司邮箱.这样,在有同事出差在外的时候,可以通过QQWEB邮箱,即可收发公司邮箱,不必安装邮箱客户端软件. 仅供各位同事参考使用. 1.打开QQ邮箱 2.点击其他邮箱, ...