洛谷P4427 [BJOI2018]求和
\(\Large\textbf{Description: } \large{一颗n个节点的树,m次询问,每次查询点i到点j的路径上所有节点点深度的k次方的和并对998244353取模(1\leq n,m \leq 300000,1\leq k\leq 50)。}\\\)
\(\Large\textbf{Solution: } \large{一开始看到这道题并没有思路,但是注意到k很小,所以我们可以预处理出每个节点到根节点1的路径上点的1到50次方的和,然后每次O(1)查询即可。\\}\)
\(\Large\textbf{Code: }\\\)
#include <cstdio>
#include <algorithm>
#define LL long long
#define gc() getchar()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
const int N = 3e5 + 5;
const int p = 998244353;
int n, m, cnt, head[N], son[N], dep[N], size[N], top[N], fa[N];
LL dis[N][52];
struct Edge {
int to, next;
}e[N];
inline int read() {
char ch = gc();
int ans = 0;
while (ch > '9' || ch < '0') ch = gc();
while (ch >= '0' && ch <= '9') ans = (ans << 1) + (ans << 3) + ch - '0', ch = gc();
return ans;
}
inline void add(int x, int y) {
e[++cnt].to = y;
e[cnt].next = head[x];
head[x] = cnt;
}
inline void dfs1(int x, int y) {
int Max = 0;
LL now = 0;
size[x] = 1;
fa[x] = y;
dep[x] = dep[y] + 1;
now = dep[x];
rep(i, 1, 50) dis[x][i] = (dis[y][i] + now) % p, now = (now * dep[x]) % p;
for (int i = head[x]; i ; i = e[i].next) {
int u = e[i].to;
dfs1(u, x);
size[x] += size[u];
if (size[u] > Max) son[x] = u, Max = size[u];
}
}
inline void dfs2(int x, int tp) {
top[x] = tp;
if (!son[x]) return;
dfs2(son[x], tp);
for (int i = head[x]; i ; i = e[i].next) {
int u = e[i].to;
if (u == son[x]) continue;
dfs2(u, u);
}
}
inline int lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
int main() {
n = read();
int x, y, k;
rep(i, 2, n) { x = read(), y = read(); if (x > y) swap(x, y); add(x, y); }
dep[0] = -1;
dfs1(1, 0);
dfs2(1, 1);
m = read();
while (m--) {
x = read(), y = read(), k = read();
int l = lca(x, y);
printf("%lld\n", (dis[x][k] + 2 * p - dis[l][k] - dis[fa[l]][k] + dis[y][k]) % p);
}
return 0;
}
洛谷P4427 [BJOI2018]求和的更多相关文章
- P4427 [BJOI2018]求和
P4427 [BJOI2018]求和 同[TJOI2018]教科书般的扭曲虚空 懒得写了(雾 #include<bits/stdc++.h> #define il inline #defi ...
- 洛谷 P4427
传送门 洛谷P4427 题意: 给你一个数,然后让你求这两个数之间的点的深度的k次方和. #思路: 很容易想到lca.因为lca可以说是求树上两个点的距离的好方法.而且lca还能遍历每一个点. 然后我 ...
- Bzoj5294/洛谷P4428 [Bjoi2018]二进制(线段树)
题面 Bzoj 洛谷 题解 考虑一个什么样的区间满足重组之后可以变成\(3\)的倍数.不妨设\(tot\)为一个区间内\(1\)的个数.如果\(tot\)是个偶数,则这个区间一定是\(3\)的倍数,接 ...
- 洛谷 P4427 求和
传送门啦 思路: 开始不肿么容易想到用倍增,但是想到需要求 $ Lca $ ,倍增这种常数小而且快的方法就很方便了.求 $ Lca $ 就是一个最普通的板子.那现在考虑怎么求题目中的结果. 树上差分可 ...
- 【桶哥的问题——吃桶-简化版】【洛谷p2671】求和
求和=>[链接] 题目相较起_rqy出的要简单很多,来自noip普及组2015 化简这个式子:x+z=2y,故x与z mod 2同余,因此和桶哥的问题——吃桶一样的思路就可以做出来啦qwq: # ...
- 洛谷P2261 余数求和
整除分块的小应用. 考虑到 k % x = k - (k / x) * x 所以把 x = 1...n 加起来就是 k * n - (k / i) * i i = 1...k(注意这里是k) 对于这个 ...
- Luogu P4427 [BJOI2018]求和
这是一道巨狗题,我已无力吐槽为什么我怎么写都不过 我们对于这种无修改的边权题目有一个经典的树上差分套路: \(ans=sum_x+sum_y-2\cdot sum_{LCA(x,y)}\) 这里的\( ...
- 洛谷P2261余数求和
传送门啦 再一次见证了分块的神奇用法,在数论里用分块思想. 我们要求 $ ans = \sum\limits ^{n} _{i=1} (k % i) $ ,如果我没看错,这个题的暴力有 $ 60 $ ...
- 洛谷 P2415 集合求和【数学公式/模拟】
给定一个集合s(集合元素数量<=30),求出此集合所有子集元素之和. 输入输出格式 输入格式: 集合中的元素(元素<=1000) 输出格式: 和 输入输出样例 输入样例#1: 2 3 输出 ...
随机推荐
- 0X01应用程序黑客技术
前言 该文章主要是讲解了常见的应用程序黑客技术基本概念,包括消息钩取,API钩取,DLL注入,代码注入 天象独行 0X01:消息钩取 原理:在我们通过键盘,鼠标等输入信息过程中,Windows会通过钩 ...
- DataTable和实体类之间的转换
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...
- 【转】CGI 和 FastCGI 协议的运行原理
介绍 深入CGI协议 CGI的运行原理 CGI协议的缺陷 深入FastCGI协议 FastCGI协议运行原理 为什么是 FastCGI 而非 CGI 协议 CGI 与 FastCGI 架构 再看 Fa ...
- day21-Python运维开发基础(单个字符匹配 / 多字符匹配)
1. 正则表达式(单个字符匹配) # ### 正则表达式 => 单个字符匹配 import re """ lst = re.findall(正则表达式,字符串) & ...
- C++11 — lambda表达式(匿名函数)
C++11中lambda表达式的基本语法格式为: [capture](parameters) -> return_type { /* ... */ } 其中 [] 内为外部变量的传递方式: [] ...
- 浏览器的HTML页面展示
当你输入一个url链接发生了什么? 下面的图是上篇文章的改造版本 电脑浏览器输入百度 打开台式电脑或者笔记本中的浏览器(IE,谷歌,360等),访问百度http://www.baidu.com,此时发 ...
- leetcode295 Find Median from Data Stream
""" Median is the middle value in an ordered integer list. If the size of the list is ...
- 还在用逆向工程?太Low了,试试通用Mapper吧!
什么是通用Mapper? 通用mapper 可以极大的方便开发人员进行ORM,提供极其方便的单表增删改查. 什么是通用mapper,一句话简单说,它就是个辅助mybatis极简单表开发的组件.它不是为 ...
- VirtualBox安装Debian
1.下载Debian的dvd1,按照http://www.jb51.net/os/85858.html网上教程安装Debian 1.1.我创建了20G的虚拟磁盘,分区的时候我分了3个区,2G交换空间, ...
- django的404,500错误自定义页面的配置
django404,500错误自定义页面: 1.设置settings文件 DEBUG = False ALLOWED_HOSTS = ['127.0.0.1', 'localhost']或者ALLOW ...