Educational Codeforces Round 127 (Rated for Div. 2) E. Preorder
设\(f[v]\)是以结点\(v\)为根的方案数,设左子树的根为\(x\),右子树的根为\(y\),那么如果左右子树完全相同,那么我们交换左右子树对方案没有任何影响,都是:
\]
如果左右子树不相同,那么则多出\(f[x] * f[y]\)的贡献,所以方案数为\(f[v] = f[x] * f[y] * 2\)。
最重要的就是如何判断两个子树是不是完全相同的,这里用到树哈希,参考大佬们的哈希函数:
\]
其中\(P_0\),\(P_1\)是两个不同的质数,因为对哈希基本没有研究,所以我直接取了131,1331,幸运没有被卡。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = (1 << 18) + 10, Mod = 998244353;
ull P1 = 131, P0 = 13131;
ull Hash[N];
ll n;
ll f[N];
ull pre1[25], pre0[25];
string s;
void dfs(ll u, ll depth) {
if (u * 2 >= ((1 << n) - 1)) { //到了叶子结点
f[u] = 1;
Hash[u] = ((s[u] - 'A') ? pre1[depth] : pre0[depth]);
return;
}
dfs(u * 2ll, depth + 1);
dfs(u * 2ll + 1, depth + 1);
Hash[u] = Hash[u << 1] * Hash[u << 1 | 1] + ((s[u] - 'A') ? pre1[depth] : pre0[depth]);
f[u] = f[u << 1] * f[u << 1 | 1] % Mod;
if (Hash[u << 1] != Hash[u << 1 | 1]) {
f[u] = f[u << 1] * f[u << 1 | 1] % Mod * 2 % Mod;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
pre1[1] = P1;
for (int i = 2; i <= 20; i++) {
pre1[i] = pre1[i - 1] * P1;
}
pre0[1] = P0;
for (int i = 2; i <= 20; i++) {
pre0[i] = pre0[i - 1] * P0;
}
cin >> n;
cin >> s;
s = "0" + s;
dfs(1, 1);
//cout << Hash[4] << " " << Hash[5] << "\n";
cout << f[1] << "\n";
return 0;
}
Educational Codeforces Round 127 (Rated for Div. 2) E. Preorder的更多相关文章
- Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题
Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题 [Problem Description] 总共两次询 ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
随机推荐
- 西门子HMI切换页面时的指示功能
怎么样才能做到像这样按下切换界面的按钮,切换过去之后对应的切换按钮还进行高亮指示呢? 首先我们要先新建模板,把我们的画面拖拽到模板里就会形成按钮 在画面的"属性"中 在属性中选上我 ...
- ENVI、ERDAS计算Landsat 7地表温度:单窗算法实现
本文介绍基于ENVI与ERDAS软件,对Landsat 7遥感影像数据加以单窗算法的地表温度(LST)反演操作. 目录 1 原理部分与前期操作准备 1.1 图像预处理 1.2 植被指数反演 1.3 单 ...
- cdn 引入的资源需要通过 externals 排除打包哦~
cdn 指的是通过相互连接的网络系统,使用最靠近用户的服务器将音乐.图片等资源以高效率和低成本的方式将内容传递给用户. 在 webpack 中,我们可能会将引入的第三方资源会编译成单独的文件,作为静态 ...
- c++中unique_ptr 的使用和理解
unique_ptr 的使用 std::unique_ptr是c++11起引入的智能指针,为什么必须要在c++11起才有该特性,主要还是c++11增加了move语义,否则无法对对象的所有权进行传递. ...
- 从MybatisPlus回归Mybatis
从MybatisPlus回归Mybatis 之前写项目一直习惯使用MyBatisPlus,单表查询很方便:两张表也很方便,直接业务层处理两张表的逻辑.但什么都图方便只会害了你. 但连接的表比较复杂的时 ...
- JavaScript的Map和WeakMap
熟悉JavaScript的Map和WeakMap Map Map的键/值可以是任何类型 基本API 初始化映射: //使用new关键字和Map构造函数进行初始化 const m1 = new Map( ...
- 关于 Llama 2 的一切资源,我们都帮你整理好了
Llama 2 是一个由 Meta 开发的大型语言模型,是 LLaMA 1 的继任者.Llama 2 可通过 AWS.Hugging Face 获取,并可以自由用于研究和商业用途.Llama 2 预训 ...
- 完美解决Content type ‘multipart/form-data;boundary=----------0467042;charset=UTF-8‘ not supported问题
一.前言 今天在做文件上传功能出现了该问题,该接口如下: @PostMapping("/upload") public Boolean upload(@RequestParam ...
- textarea自动适应高度
textarea自动适应高度,兼容IE/Firefox.chrome 代码:<textarea name="textarea" id="textarea" ...
- 解密Linux中的通用块层:加速存储系统,提升系统性能
通用块层 通用块层是Linux中的一个重要组件,用于管理不同块设备的统一接口,减少不同块设备的差异带来的影响.它位于文件系统和磁盘驱动之间,类似于Java中的适配器模式,让我们无需关注底层实现,只需提 ...