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 ...
随机推荐
- python连接数据库及查询包含中文错误解决方法
使用MySQLdb库来连接数据库 import MySQLdb conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='', por ...
- 【算法】单调栈 & 单调队列学习笔记
1. 单调栈简介 1.1 前言 今天是 2023/1/15,一中寒假集训阶段性的结束了.集训的学习笔记可以在本人 blogs 的[算法]标签栏中找. 马上就要过年了,提前祝大家新年快乐! 1.2 什么 ...
- 解决pandas 读取csv文件报错
使用encoding参数: pd.read_csv(path,sep=",",encoding='utf-16') 注意:该参数之后的编码格式,并不是固定的,需要用记事本打开csv ...
- 明白error类型让你更快定位开发报错
在javascript中,开发遇到的项目报错,很多时候都是通过 Error 这个类来展示的,清楚Error的类型可以更好定位项目中的问题 Error的类型 Error Error是所有其它错误的父类, ...
- 一款开源免费、更符合现代用户需求的论坛系统:vanilla
对于个人建站来说,WordPress相信很多读者都知道了.但WordPress很多时候我们还是用来建立自主发布内容的站点为主,适用于个人博客.企业主站等.虽然有的主题可以把WordPress变为论坛, ...
- Azure Terraform(十四)Azure Key Vault 的机密管理
一,引言 最近有网友私信我,将 Terraform 部署到 Azure 是一种将基础结构作为代码进行管理的好方法,但是如何使用 Azure Key Vault 来存储我们的 Secret ?在这篇博文 ...
- IDA的使用2
IDA的使用2 string类型的选择 Rename 要注意如果再namelist和public name里面是不能重名 操作数 这个主要和开发结合精密, change sign-改变符号 bitwi ...
- 使用MD5算法和sha512sum校验和检验文件完整性
目录 一.前言 二.MD5算法简介 三.什么是校验和 四.使用MD5算法和sha512sum校验和检验文件完整性 五.总结 一.前言 在我们日常生活中,无论是下载文件.传输数据还是备份重要信息,如何确 ...
- 【NestJS系列】核心概念:Module模块
theme: fancy highlight: atelier-dune-dark 前言 模块指的是使用@Module装饰器修饰的类,每个应用程序至少有一个模块,即根模块.根模块是Nest用于构建应用 ...
- grinder使用入门
安装配置 自动安装脚本编写 移步我的github下载脚本,只要修改下配置信息,即可完成安装 环境变量设置: