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 ...
随机推荐
- React: React-Router嵌套路由 exact问题
说明 当使用嵌套路由时,不能在父路由中添加exact,因为要先匹配父路由才能匹配子路由 父路由 子路由 效果如下所示 参考链接 https://www.jianshu.com/p/8bc3251079 ...
- ubuntu 安装pip3 报错
运行命令 sudo apt install python3-pip 报错: 有几个软件包无法下载,要不运行apt-get update 或加上--fix-missing试试 输入命令: sudo ap ...
- MySQL5.5+配置主从同步并结合ThinkPHP5设置分布式数据库
前言: 本文章是在同处局域网内的两台windows电脑,且MySQL是5.5以上版本下进行的一主多从同步配置,并且使用的是集成环境工具PHPStudy为例.最后就是ThinkPHP5的分布式的连接,读 ...
- 在不修改代码情况下解决 Chrome 浏览器跨域
前言: 在前后台分离的项目,跨域是经常遇到的问题了.是实际解决方案中,大部分采用服务端配置,而如果只是调试,可以通过配置 Chrome 浏览器实现跨域,以下以 NodeJs 服务为例. 开始: 1. ...
- [minio]简介与安装
简介 MinIO是一款高性能的分布式对象存储系统. 官网地址 特性 轻便 高性能 跨平台 高扩展性 云原生支持 兼容Amazon S3 基本概念 s3:simple storage service,简 ...
- pyqt5学习日记
前提需要pip安装PyQt5与PyQt5-tools 安装后会有qtdesigner.exe和pyuic5.exe,用everything直接可以搜索到 qtdesigner.exe是来设计ui的 p ...
- C#利用Refit实现JWT自动续期
前言 笔者之前开发过一套C/S架构的桌面应用,采用了JWT作为用户的登录认证和授权.遇到的唯一问题就是JWT过期了该怎么办?设想当一个用户正在进行业务操作,突然因为Token过期失效,莫名其妙地跳转到 ...
- 6、Mybatis之高级查询
6.1.创建接口.映射文件和测试类 ++++++++++++++++++++++++++分割线++++++++++++++++++++++++++ 注意namespace属性值为对应接口的全限定类名 ...
- Programming abstractions in C阅读笔记:p132-p137
<Programming Abstractions In C>学习第53天,p132-p137,3.2小节"strings"总结如下: 一.技术总结 3.2小节介绍了字 ...
- 原来你是这样的JAVA[06]-反射
1.JVM为每个加载的class及interface创建了对应的Class实例来保存class及interface的所有信息: 获取一个class对应的Class实例后,就可以获取该class的所有信 ...