题解:P13858 [SWERC 2020] Rule 110
论文题。本文主要参考官方题解及 Gosper 于
一些废话
- Conway 于
1970 年发明生命游戏。 - Gosper 于
1984 年发表了快速计算生命游戏的\text{HashLife} 算法。也就是这篇文章所说的算法。 -
-
算法流程
由于论文描绘的是二维生命游戏,所以接下来的一些名词可能不太准确。下面出现的英文词汇大部分出现在论文中。
建树(Building Cells)
对于一个长度为
哈希化(Hash Mechanics)
可以发现,这种“线段树”中有很多相同的节点。在这个序列中,按照传统的方式,一共会建出
显然 micro-cell 只有两种。对于一个 macro-cell,我们至少要记录它的左右儿子(的标识),因此我们判定两个 macro-cell 相等的方式就是左右儿子的标识相等。
记忆化(Memoizing)
别抬杠,我没拼错
那建出这样一个树有什么用?我们可以利用它来计算整个序列在
在题解中的做法和论文中的做法稍有不同,个人倾向认为题解的更好理解。
首先要明确的一点是,假设当前知道了一个序列长度为
对于三个长度为
因此我们可以设计一个函数
这个算法优秀之处就在于,所询问的不同
那怎么求
为了求
以上就是理论基础。回到原题来。
可以发现,刚才所说的步骤都是往后跳
那这有个问题。我们输入了一个长度为
处理完
还有一点需要注意,我们计算 cell 数组
你可能问为什么没有复杂度分析。事实上是,题解没有,论文没有,我也不会证。论文里只提到使用哈希可以避免大量的重复运算。题解里只是提到了在极限数据下,最多有大概
事实上还跑的挺快。
#include <algorithm>
#include <iostream>
#include <cassert>
#include <vector>
#include <tuple>
#include <map>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tii = tuple<int, int, int>;
map<tii, int> rule;
map<pii, int> id;
struct Node
{
int depth; // 表示当前这个 cell 的长度为 2^{depth},其实没什么用,可以删去
int son[2];
ll oneCnt; // 1 的个数
};
vector<Node> nodes;
int merge(int u, int v) // 创建一个 macro-cell,两个子节点为 u, v
{
if (id.count({u, v}))
{
return id[{u, v}];
}
Node cur = {
nodes[u].depth + 1,
u, v,
nodes[u].oneCnt + nodes[v].oneCnt
};
nodes.push_back(cur);
return id[{u, v}] = nodes.size() - 1;
}
/**
* l, m, r are trees of the same size, i. e. 2^{depth}.
* after 2^{depth} steps, only information in m is meaningful
*/
int run(int l, int m, int r) // 题解里的 f(l, m, r)
{
if (rule.count({l, m, r}))
{
return rule[{l, m, r}];
}
// the depths are greater than 0, otherwise it will be stored in rule
assert(nodes[l].depth == nodes[m].depth);
assert(nodes[m].depth == nodes[r].depth);
assert(nodes[m].depth != 0);
auto [a, b] = nodes[l].son;
auto [c, d] = nodes[m].son;
auto [e, f] = nodes[r].son;
int g = run(a, b, c), h = run(b, c, d), i = run(c, d, e), j = run(d, e, f);
return rule[{l, m, r}] = merge(run(g, h, i), run(h, i, j));
}
vector<int> runList(vector<int> val)
{
vector<int> res;
for (int i = 0; i < val.size() - 2; i++)
{
res.push_back(run(val[i], val[i + 1], val[i + 2]));
}
return res;
}
int main()
{
rule[{1, 1, 1}] = 0; // 初始化规则
rule[{1, 1, 0}] = 1;
rule[{1, 0, 1}] = 1;
rule[{1, 0, 0}] = 0;
rule[{0, 1, 1}] = 1;
rule[{0, 1, 0}] = 1;
rule[{0, 0, 1}] = 1;
rule[{0, 0, 0}] = 0;
nodes.push_back({0, 0, 0, 0});
nodes.push_back({0, 1, 0, 1});
vector<int> cur;
for (int i = 0; i < 16; i++)
{
char ch;
cin >> ch;
cur.push_back(ch == '1');
}
ll n;
cin >> n;
int emp = 0; // emp 表示长度为 2^k 的空 cell。初始时是 0 的 micro-cell
while (n)
{
if (n & 1)
{
vector<int> nc{emp, emp}; // [emp, emp, cur..., emp, emp]
nc.resize(cur.size() + 2);
copy(cur.begin(), cur.end(), nc.begin() + 2);
nc.push_back(emp);
nc.push_back(emp);
cur = runList(nc);
}
if (cur.size() & 1) // 如果当前数组里只有奇数个 cell,就加入一个空 cell
{
cur.push_back(emp);
}
for (int i = 0; i < cur.size(); i += 2)
{
cur[i >> 1] = merge(cur[i], cur[i + 1]);
}
cur.resize(cur.size() >> 1);
emp = merge(emp, emp);
n >>= 1;
}
ll res = 0;
for (int i : cur)
{
res += nodes[i].oneCnt;
}
cout << res << '\n';
return 0;
}
我感觉官解的 python 写的很简洁啊,为什么我没放。
附录
一共有两个东西要补充。一种是将上述东西拓展到二维生命游戏,另一种是论文里对
四叉树(Quadtree)
拓展到二维情况中,我们仍然可以用类似的结构描绘。在合并中,我们把上下左右共
“结果”(\text{RESULT} )
在论文里是这样处理
合并也是容易的。
其实论文里还有对其他实现细节和常数的分析等等,这里就不展开赘述了。有兴趣可以自己读论文。