题目

如题。

算法

就是刚学习的插头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)\)

代码

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6. typedef long long i64;
  7. const int MaxN = 1000;
  8. const int MaxM = 6;
  9. const int MaxS = 103;
  10. const int MOD = (int) 1e9 + 7;
  11. int n, m;
  12. int A[MaxN][MaxM];
  13. #define getbit(s, i) ((s) >> ((i) << 1) & 3)
  14. #define clrbit(s, i) ((s) & ~(3 << ((i) << 1)))
  15. #define clrbit2(s, i, j) (clrbit( clrbit(s, i), j))
  16. #define cpbit(s, i, x) ((s) ^ ((x) << ((i) << 1)))
  17. #define revbit(s, i) ((s) ^ (1 << ((i) << 1)))
  18. template <class T>
  19. void addIt(T &a, const T &b)
  20. {
  21. a += b;
  22. if (a >= MOD) a -= MOD;
  23. }
  24. struct Hash
  25. {
  26. int n;
  27. pair<int, int> A[MaxS];
  28. struct Link
  29. {
  30. int to;
  31. Link *next;
  32. }pool[MaxS], *pool_cur, *info[MaxS];
  33. void sort()
  34. {
  35. std::sort(A, A + n);
  36. }
  37. int find(const int &x)
  38. {
  39. pair<int, int> *p = lower_bound(A, A + n, make_pair(x, -1));
  40. if (p->first == x) return p->second;
  41. return 0;
  42. }
  43. Hash()
  44. {
  45. pool_cur = pool;
  46. }
  47. void update()
  48. {
  49. int i = 0;
  50. while (i < n)
  51. {
  52. if (getbit(A[i].first, m))
  53. A[i] = A[-- n];
  54. else
  55. i ++;
  56. }
  57. }
  58. void push(const int &s, const int &x)
  59. {
  60. int hash = s % MaxS;
  61. for (Link *p = info[hash]; p; p = p->next)
  62. {
  63. if (A[p->to].first == s)
  64. {
  65. addIt(A[p->to].second, x);
  66. return;
  67. }
  68. }
  69. pool_cur->to = n;
  70. pool_cur->next = info[hash];
  71. info[hash] = pool_cur ++;
  72. A[n ++] = make_pair(s, x);
  73. }
  74. }dp[2][MaxN + 1][MaxM];
  75. int getbracket0(const int &s, const int &i)
  76. {
  77. int cnt = 1;
  78. for (int k = i + 1; k < m; k ++)
  79. {
  80. int t = getbit(s, k);
  81. if (t)
  82. {
  83. if (t & 1) cnt --;
  84. else cnt ++;
  85. }
  86. if (!cnt) return k;
  87. }
  88. return -1;
  89. }
  90. int getbracket1(const int &s, const int &i)
  91. {
  92. int cnt = -1;
  93. for (int k = i - 1; k >= 0; k --)
  94. {
  95. int t = getbit(s, k);
  96. if (t)
  97. {
  98. if (t & 1) cnt --;
  99. else cnt ++;
  100. }
  101. if (! cnt) return k;
  102. }
  103. return -1;
  104. }
  105. void Process(Hash (*dp)[MaxM])
  106. {
  107. dp[0][0].push(0, 1);
  108. for (int i = 0; i < n; i ++)
  109. {
  110. for (int j = 0; j < m; j ++)
  111. {
  112. Hash &cur = dp[i][j], &next = j == m - 1 ? dp[i + 1][0] : dp[i][j + 1];
  113. for (int k = 0; k < cur.n; k ++)
  114. {
  115. int s = cur.A[k].first;
  116. int x = cur.A[k].second;
  117. int L = getbit(s, m);
  118. int U = getbit(s, j);
  119. #define send(st) next.push(st, x)
  120. if (!A[i][j])
  121. {
  122. if (L && U)
  123. {
  124. L &= 1, U &= 1;
  125. if (!L && !U)
  126. send(revbit( clrbit2(s, j, m), getbracket0(s, j)));
  127. else if (L && !U)
  128. send(clrbit2(s, j, m));
  129. else if (L && U)
  130. send(revbit( clrbit2(s, j, m), getbracket1(s, j)));
  131. }
  132. else if (L)
  133. {
  134. send(s);
  135. send(clrbit( cpbit(s, j, L), m));
  136. }
  137. else if (U)
  138. {
  139. send(s);
  140. send(clrbit( cpbit(s, m, U), j));
  141. }
  142. else
  143. {
  144. send(s);
  145. send(cpbit( cpbit(s, j, 2), m, 3));
  146. }
  147. }
  148. else if (!L && !U)
  149. send(s);
  150. }
  151. cerr << next.n << endl;
  152. if (j == m - 1) next.update();
  153. }
  154. }
  155. }
  156. int P[729], Pn; // 3^m
  157. int Qn;
  158. pair<int, int> Q[729 * 729];
  159. void dfs(int i, int cnt, int s)
  160. {
  161. if (i == m)
  162. {
  163. if (! cnt && s)
  164. {
  165. P[Pn ++] = s;
  166. }
  167. }
  168. else
  169. {
  170. dfs(i + 1, cnt + 1, s ^ (2 << (i << 1)));
  171. if (cnt)
  172. dfs(i + 1, cnt - 1, s ^ (3 << (i << 1)));
  173. dfs(i + 1, cnt, s);
  174. }
  175. }
  176. int getbracket(const int &a, const int &j)
  177. {
  178. if (getbit(a, j) & 1)
  179. {
  180. int cnt = -1;
  181. for (int i = j - 1; i >= 0; i --)
  182. {
  183. int t = getbit(a, i);
  184. if (t)
  185. {
  186. if (t & 1) cnt --;
  187. else cnt ++;
  188. if (! cnt) return i;
  189. }
  190. }
  191. }
  192. else
  193. {
  194. int cnt = 1;
  195. for (int i = j + 1; i < m; i ++)
  196. {
  197. int t = getbit(a, i);
  198. if (t)
  199. {
  200. if (t & 1) cnt --;
  201. else cnt ++;
  202. if (! cnt) return i;
  203. }
  204. }
  205. }
  206. return -1;
  207. }
  208. bool eliminate(int &a, int at, int &b, const int &start)
  209. {
  210. if (getbit(a, at) == 0) return at == start;
  211. int other = getbracket(a, at);
  212. a = clrbit2(a, at, other);
  213. if (!eliminate(b, other, a, start)) return false;
  214. return true;
  215. }
  216. bool check(int a, int b)
  217. {
  218. for (int i = 0; i < n; i ++)
  219. if (getbit(a, i))
  220. {
  221. if (! eliminate(a, i, b, i)) return false;
  222. return a == 0 && b == 0;
  223. }
  224. return false;
  225. }
  226. int main()
  227. {
  228. int k;
  229. scanf("%d%d", &n, &m);
  230. scanf("%d", &k);
  231. while (k --)
  232. {
  233. int x, y;
  234. scanf("%d%d", &x, &y);
  235. x --, y --;
  236. A[x][y] = 1;
  237. }
  238. Process(dp[0]);
  239. for (int i = 0; i < n >> 1; i ++)
  240. {
  241. int j = n - i - 1;
  242. for (int k = 0; k < m; k ++)
  243. swap(A[i][k], A[j][k]);
  244. }
  245. Process(dp[1]);
  246. dfs(0, 0, 0);
  247. for (int i = 0; i < Pn; i ++)
  248. for (int j = 0; j < Pn; j ++)
  249. {
  250. if (check(P[i], P[j]))
  251. {
  252. Q[Qn ++] = make_pair(P[i], P[j]);
  253. }
  254. }
  255. scanf("%d", &k);
  256. for (int i = 0; i < n; i ++)
  257. {
  258. dp[0][i][0].sort();
  259. dp[1][i][0].sort();
  260. }
  261. while (k --)
  262. {
  263. int x, y;
  264. scanf("%d%d", &x, &y);
  265. x --, y --;
  266. int ans = 0;
  267. for (int i = 0; i < Qn; i ++)
  268. {
  269. if (getbit(Q[i].first, y))
  270. {
  271. addIt(ans, (int) ((i64) dp[0][x + 1][0].find(Q[i].first) *
  272. dp[1][n - x - 1][0].find(Q[i].second) % MOD));
  273. }
  274. }
  275. printf("%d\n", ans);
  276. }
  277. return 0;
  278. }

清华集训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. Spring注解配置

    配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  2. Intellij IDEA 常用 设置 及 快捷键 (持续更新)

    Transparent native-to-ascii conversion以下设置都可以通过 设置中的搜索框 进行关键字搜索 0, 打开Project 设置 Command + ; 1, 打开Mod ...

  3. jQuery + svg/vml

    流程设计器jQuery + svg/vml(Demo7 - 设计器与引擎及表单一起应用例子)   去年就完成了流程设计器及流程引擎的开发,本想着把流程设计器好好整理一下,形成一个一步一步的开发案例,结 ...

  4. android获取系统wifi状态等

    WIFI 获取WIFI状态 WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); ...

  5. perl学习(5) 输入和输出

    1.1. 从标准输入设备输入 <STDIN> 行输入操作在到达文件的结尾时将返回undef,在while循环的条件中不能使用chomp: while (defined($line = &l ...

  6. Hdu 3371 Connect the Cities(最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...

  7. Palindrome(最长回文串manacher算法)O(n)

     Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  8. ListView下拉刷新及上拉更多两种状态

    一.前言: 很多应用都会用到ListView,当然如果是iOS就会用UITableViewController,这两个控件在不同的OS上,功能是一样的,只是有些细微的不同(iOS的UITableVie ...

  9. 使用Intel编译器获得一致的浮点数值计算结果

    使用Intel编译器获得一致的浮点数值计算结果大多数十进制的浮点数, 用二进制表示时不是完全一致的; 与此同时, 大多数与浮点数值相关的计算结果, 存在着固有的不确定性.通常, 编写浮点计算应用软件希 ...

  10. BZOJ 2157: 旅游( 树链剖分 )

    树链剖分.. 样例太大了根本没法调...顺便把数据生成器放上来 -------------------------------------------------------------------- ...