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 ...
随机推荐
- Power AutoMate: 运行脚本程序
运行脚本文件 操作步骤 配置脚本 点击脚本文件菜单,选中运行python脚本.在其中输入需要徐行的脚本点击保存 之后界面会如下所示: 运行程式 可以看到程式正常运行
- jdk安装自动化
写个在linux环境安装Java的脚本(install_java.sh),只需将jdk上传至/opt目录下,执行脚本即可. #!/bin/bash #author:zhangyl #本安装使用jdk版 ...
- python列表的增删
list = [1, 2, 3, 4]# 打印后两位print(list[-2:])# 打印前2位print(list[:2])# 修改列表元素list[0] = 5print(list)# 添加元素 ...
- 【路由器】OpenWrt 手动编译 ipk
目录 .ipk 文件 编译准备 编译 .ipk 文件 更新 feeds 配置平台 获取交叉编译链 添加需要编译的第三方软件包 参考资料 .ipk 文件 .ipk 文件是可以通过 OpenWrt 的包管 ...
- 细聊C# AsyncLocal如何在异步间进行数据流转
前言 在异步编程中,处理异步操作之间的数据流转是一个比较常用的操作.C#异步编程提供了一个强大的工具来解决这个问题,那就是AsyncLocal.它是一个线程本地存储的机制,可以在异步操作之间传递数据. ...
- Redis系列22:Redis 的Pub/Sub能力
Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) Redis系列5: ...
- QA|conftest使用了fixture但是没生效的原因|Pytest
conftest.py中使用了fixture但是没生效,后面发现是因为autouse默认False导致,修改后代码如下 # conftest.py @pytest.fixture(scope='ses ...
- 利用RATF框架实现web状态的监控
之前,我们已经说明了如何实现一个我们的接口测试框架RATF,当然这个框架不止可以用于管理我们的接口测试代码,我们还可以用他来对我们的web进行简单粗暴的监控. 原理: 1. 通过使用配置文件,对要监控 ...
- 使用HTML一键打包EXE工具打包KRPANO全景项目
HTML一键打包EXE工具(HTML封装EXE, HTML转EXE)能把任意HTML项目(网址)一键打包为单个EXE文件,可以脱离浏览器和服务器,直接双击即可运行. 打包工具群:429338543 最 ...
- 「codeforces - 1633F」Perfect Matching
link. 首先所有的 activated nodes 组合成了一棵以 \(1\) 为根的有根树.询问即求由 activated nodes 组成的树的最大匹配.对于树上最大匹配有一个贪心策略:自底向 ...