题目

如题。

算法

就是刚学习的插头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. [译]Stairway to Integration Services Level 13 - SSIS 变量回顾

    介绍 在前一篇中我们组合了已经学过的事件冒泡 event bubbling, 日志记录 logging, 和父子模型 Parent-Child pattern 建立了自定义的SSIS包日志记录. 本文 ...

  2. 用ToggleButton和ImageView实现不同状态下显示的切换

    靠,写的时候第一次因为把implements OnCheckedChangeListener这里实现的接口写错了,搞了很久, 后来发现又少了这两句错了 btn = (ToggleButton) fin ...

  3. 总线接口与计算机通信(四)USB外部总线(初级认识)

    USB简介   USB是英文Universal Serial BUS(通用串行总线)的缩写,是一个外部总线标准,用于规范电脑与外部设备的连接和通讯,是应用在PC领域的接口技术.USB接口支持设备的即插 ...

  4. jz2440不能成功地启动文件系统, Failed to execute /linuxrc.

    文件系统加载失败,错误信息提示:    VFS: Mounted root (nfs filesystem).    Freeing init memory: 140K    Failed to ex ...

  5. BZOJ 1820: [JSOI2010]Express Service 快递服务( dp )

    dp(i,j,k)表示在处理第i个业务, 另外2个在j,k处. 第一维可以滚动... --------------------------------------------------------- ...

  6. BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理( 二分答案 )

    二分一下答案就好了... --------------------------------------------------------------------------------------- ...

  7. 转:30分钟掌握STL

    三十分钟掌握STL 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把它翻译出来.我没有对翻译出来的内容校验过.如果你没法在三十分钟内觉得有 ...

  8. CreateThread与_beginthread, _beginthreadex创建线程的基本概念和区别(1)

    这三个函数都可以创建新的线程,但都是如何创建的呢?当然MSDN文档最权威: Creates a thread to execute within the virtual address space o ...

  9. ftp上传错误

    明明设置好了权限,但是在上传的时候提示如下错误,但在使用的过程当中,发现有的时候是可以上传的,很奇怪的问题. baidu 了一下,发现是下面的这个设置导致的.改过来后,果然正常. 这个设置只是一个字符 ...

  10. A. Case of the Zeros and Ones----解题报告

    A. Case of the Zeros and Ones Description Andrewid the Android is a galaxy-famous detective. In his ...