\(\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]求和的更多相关文章

  1. P4427 [BJOI2018]求和

    P4427 [BJOI2018]求和 同[TJOI2018]教科书般的扭曲虚空 懒得写了(雾 #include<bits/stdc++.h> #define il inline #defi ...

  2. 洛谷 P4427

    传送门 洛谷P4427 题意: 给你一个数,然后让你求这两个数之间的点的深度的k次方和. #思路: 很容易想到lca.因为lca可以说是求树上两个点的距离的好方法.而且lca还能遍历每一个点. 然后我 ...

  3. Bzoj5294/洛谷P4428 [Bjoi2018]二进制(线段树)

    题面 Bzoj 洛谷 题解 考虑一个什么样的区间满足重组之后可以变成\(3\)的倍数.不妨设\(tot\)为一个区间内\(1\)的个数.如果\(tot\)是个偶数,则这个区间一定是\(3\)的倍数,接 ...

  4. 洛谷 P4427 求和

    传送门啦 思路: 开始不肿么容易想到用倍增,但是想到需要求 $ Lca $ ,倍增这种常数小而且快的方法就很方便了.求 $ Lca $ 就是一个最普通的板子.那现在考虑怎么求题目中的结果. 树上差分可 ...

  5. 【桶哥的问题——吃桶-简化版】【洛谷p2671】求和

    求和=>[链接] 题目相较起_rqy出的要简单很多,来自noip普及组2015 化简这个式子:x+z=2y,故x与z mod 2同余,因此和桶哥的问题——吃桶一样的思路就可以做出来啦qwq: # ...

  6. 洛谷P2261 余数求和

    整除分块的小应用. 考虑到 k % x = k - (k / x) * x 所以把 x = 1...n 加起来就是 k * n - (k / i) * i i = 1...k(注意这里是k) 对于这个 ...

  7. Luogu P4427 [BJOI2018]求和

    这是一道巨狗题,我已无力吐槽为什么我怎么写都不过 我们对于这种无修改的边权题目有一个经典的树上差分套路: \(ans=sum_x+sum_y-2\cdot sum_{LCA(x,y)}\) 这里的\( ...

  8. 洛谷P2261余数求和

    传送门啦 再一次见证了分块的神奇用法,在数论里用分块思想. 我们要求 $ ans = \sum\limits ^{n} _{i=1} (k % i) $ ,如果我没看错,这个题的暴力有 $ 60 $ ...

  9. 洛谷 P2415 集合求和【数学公式/模拟】

    给定一个集合s(集合元素数量<=30),求出此集合所有子集元素之和. 输入输出格式 输入格式: 集合中的元素(元素<=1000) 输出格式: 和 输入输出样例 输入样例#1: 2 3 输出 ...

随机推荐

  1. windows系统下hosts文件的改写(为了测试nginx内网的证书代理,需要做域名解析)

    1. win加R     C:\WINDOWS\system32\drivers\etc 2.打开hosts文件  加入一行  IP为客户机要访问的IP地址  域名也是在nginx中定义好的 3.ct ...

  2. 为什么需要NAT,目前家庭的计算机器如何上网?(原创)

    .什么是NAT?     字面翻译网络地址转换. 2.产生的背景    解决公网IP不足的问题.    官方规定,将IP地址资源进行分类,分为ABCDE,常用ABC三类,在每类中划分出了一些私有IP供 ...

  3. Codeforces #617 (Div. 3) D. Fight with Monsters(贪心,排序)

    There are nn monsters standing in a row numbered from 11 to nn . The ii -th monster has hihi health ...

  4. hdfs dfs ls /列出了本地根目录下文件夹和文件Warning: fs.defaultFS is not set when running "ls" command

    [root@node01 customShells]# hdfs dfs -ls /Warning: fs.defaultFS is not set when running "ls&quo ...

  5. HTML 5 <em> <strong> <dfn> <code> <samp> <kbd> <var> <cite> 标签

    <em> 呈现为被强调的文本. <strong> 定义重要的文本. <dfn> 定义一个定义项目. <code> 定义计算机代码文本. <samp ...

  6. mysql时出现:is not allowed to connect to this MySQL serverConnection closed by foreign host问题的解决

    这个原因是因为索要链接的mysql数据库只允许其所在的服务器连接,需要在mysql服务器上设置一下允许的ip权限,如下: 1.连接mysql mysql -u root -p 1 如图: 2.授权 g ...

  7. python对文件中光标的操作迭代器

    seek()    默认从文件开头开始.seek(10) seek(10,1)   需要以b的模式读取文件,从相对位置进行移动光标 seek(-3,2)  倒着移动光标的模式 例如: f= open( ...

  8. Pandas 用法汇总

    一.生成数据表 1.首先导入pandas 库,一般会用到 numpy 库,所以我们先导入备用: import numpy as np import pandas as pd 2.生成 CSV 或者 x ...

  9. SpringMVC的@ControllerAdvice注解

    @ControllerAdvice顾名思义,他是一个Controller的增强,是一个异常处理类.常用于实现下面三个方面的功能: 1.处理全局异常,结合方法型注解@ExceptionHandler,用 ...

  10. jQuery EasyUI window窗口实例

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>j ...