题目

如题。

算法

就是刚学习的插头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. eclipse修改java代码后报错: java.lang.OutOfMemoryError: PermGen space

    由于在eclipse中运行项目后,我们又重新修改了某个java类,导致tomcat会重新加载这个项目所有的class.jar,多次加载后由于分配的存储空间有限,就导致了:java.lang.OutOf ...

  2. js 字符串为空

    content.replace(/(^\s)|(\s$)/g, "")

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

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

  4. 【xcode】错误之Could not launch "" failed to get the task for process

    http://blog.csdn.net/teng_ontheway/article/details/8467932 在Xcode下编译工程正常,在模拟器下运行正常,最后在真机上运行的时候出现了如下错 ...

  5. 用JS判断用户使用的是手机端还是pc端访问

    最近项目中用到一个应用,当访问同一个网站地址的时候,例如:www.xxx.com的时候,如果当前客户端是pc则跳转到专注于pc的部分,如果当前客户机是手机,则跳转到专注于手机的部分,秉承一贯的习惯,b ...

  6. nginx自动切割访问日志

    Web 访问日志 (access_log) 记录了所有外部客户端对Web服务器的访问行为,包含了客户端IP,访问日期,访问的URL资源,服务器返回的HTTP状态码等重要信息. 一条典型的Web访问日志 ...

  7. perl 自动发产品

    use Net::SMTP; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Headers; use HTTP::Response; use Enc ...

  8. SWIFT学习笔记01

    1.Swift.用来推断option是不是nil,相当于OC的 if(option) if let name = option{ greeting = "if=====" }els ...

  9. js php xmlrequest 上传图片

    本来想用插件上传图片的,后来自己写了一个简单的js实现异步的图片上传,不多说上代码很easy upload.php <?php if(isset($_FILES["myfile&quo ...

  10. stm32之GPIO库函数开发

    关于GPIO库函数的重点函数:P122 GPIO_Init() :根据GPIO_InitStruct中指定的参数初始化外设GPIOx寄存器: GPIO_ReadInputDataBit():读取指定端 ...