洛谷 P4427 求和
传送门啦
思路:
开始不肿么容易想到用倍增,但是想到需要求 $ Lca $ ,倍增这种常数小而且快的方法就很方便了。求 $ Lca $ 就是一个最普通的板子。那现在考虑怎么求题目中的结果。
树上差分可能听起来很高大上,但是前缀和并不陌生,树上差分就理解成树上前缀和就好了:
$ sum[u] + sum[v] - sum[lca(u , v)] ; $
树上差分之前要先预处理出 $ dis $ 数组, $ dis[i][j] $ 表示从 $ i $ 出发到根节点(本题中的1号节点)的 $ j $ 次方。
for(re long long j = 1 ; j <= 50 ; ++ j)
dis[x][j] = dis[fa][j] + quick_power(dep[x] , j) ;
这就是预处理的代码了, $ dep $ 表示深度 , $ quick - power $ 为快速幂。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define re register
using namespace std ;
const long long maxn = 300005 ;
const long long mod = 998244353 ;
inline long long read () {
long long f = 1 , x = 0 ;
char ch = getchar () ;
while(ch > '9' || ch < '0') {if(ch == '-') f = -1 ; ch = getchar () ;}
while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0' ; ch = getchar () ;}
return x * f ;
}
inline void print (long long x){
if(x < 0) {putchar('-') ; x = -x ;}
if(x > 9) print(x / 10) ;
putchar(x % 10 + '0') ;
}
long long n , x , y , m , a , b , c ;
long long head[maxn] , tot ;
long long ans ;
struct Edge {
long long from , to , next ;
}edge[maxn << 1] ;
inline void add (long long u , long long v) {
edge[++tot].from = u ;
edge[tot].to = v ;
edge[tot].next = head[u] ;
head[u] = tot ;
}
long long quick_power (long long a , long long b) {
long long res = a , ans = 1 ;
while(b) {
if(b & 1) ans = ans * res % mod ;
res = res * res % mod ;
b >>= 1 ;
}
return ans % mod ;
}
long long dep[maxn] , f[maxn][21] , dis[maxn][51];
inline void dfs (long long x , long long fa) {
dep[x] = dep[fa] + 1 ;
f[x][0] = fa ;
for(re long long j = 1 ; j <= 50 ; ++ j)
dis[x][j] = dis[fa][j] + quick_power(dep[x] , j) ;
for(re long long i = 1 ; (1 << i) <= dep[x] ; ++ i) {
f[x][i] = f[f[x][i - 1]][i - 1] ;
}
for(re long long i = head[x] ; i ; i = edge[i].next) {
long long v = edge[i].to ;
if(v != fa) dfs(v , x) ;
}
}
inline long long lca (long long a , long long b) {
if(dep[a] < dep[b]) swap(a , b) ;
for(re long long i = 20 ; i >= 0 ; -- i) {
if((1 << i) <= (dep[a] - dep[b]) ) {
a = f[a][i] ;
}
}
if(a == b) return a ;
for(re long long i = 20 ; i >= 0 ; -- i) {
if((1 << i) <= dep[a] && (f[a][i] != f[b][i])) {
a = f[a][i] ;
b = f[b][i] ;
}
}
return f[a][0] ;
}
int main () {
n = read () ;
for(re long long i = 1 ; i <= n - 1 ; ++ i) {
x = read () ; y = read () ;
add(x , y) ;
add(y , x) ;
}
dep[1] = -1 ;
dfs(1 , 1) ;
m = read () ;
for(re long long i = 1 ; i <= m ; ++ i) {
a = read () ; b = read () ; c = read () ;
long long root = lca(a , b) ;
ans = (dis[a][c] - dis[root][c] + dis[b][c] - dis[f[root][0]][c]) % mod ;
//printf("%d %d %d %d\n" , dis[a][c] , dis[b][c] , dis[root][c] , quick_power(dep[root] , c) % mod ) ;
print(ans) ;
printf("\n") ;
}
return 0 ;
}
洛谷 P4427 求和的更多相关文章
- 洛谷 P4427
传送门 洛谷P4427 题意: 给你一个数,然后让你求这两个数之间的点的深度的k次方和. #思路: 很容易想到lca.因为lca可以说是求树上两个点的距离的好方法.而且lca还能遍历每一个点. 然后我 ...
- 洛谷P4427 [BJOI2018]求和
\(\Large\textbf{Description: } \large{一颗n个节点的树,m次询问,每次查询点i到点j的路径上所有节点点深度的k次方的和并对998244353取模(1\leq n, ...
- 洛谷 P2671 求和 解题报告
P2671 求和 题目描述 一条狭长的纸带被均匀划分出了\(n\)个格子,格子编号从\(1\)到\(n\) .每个格子上都染了一种颜色\(color_i\)用\([1,m]\)当中的一个整数表示),并 ...
- 洛谷P2671 求和 [数论]
题目传送门 求和 格式难调,题面就不放了. 分析: $ZYYS$的一道题. 很显然是大力推公式.我们分析一下题目,实际上限制条件就是:下标同奇偶且颜色相同的数,那么我们先拿这个公式$(x+z)*(nu ...
- 洛谷——P1630 求和
P1630 求和 题目描述 求1^b+2^b+……+a^b的和除以10000的余数. 输入输出格式 输入格式: 第一行包含一个正整数N,表示共有N组测试数据: 接下来N行,每行包含两个正整数a和b. ...
- 洛谷 P2671 求和
题目描述 一条狭长的纸带被均匀划分出了nn个格子,格子编号从11到nn.每个格子上都染了一种颜色color\_icolor_i用[1,m][1,m]当中的一个整数表示),并且写了一个数字number\ ...
- [洛谷2671]求和<前缀和&模拟>
题目链接:https://www.luogu.org/problemnew/show/P2671 这是noip2015普及组的第三题,谁说的普及组的题就一定水的不行,这道题就比较有意思的 这道题的暴力 ...
- NOIP2015 普及组 洛谷P2671 求和 (数学)
一道数学题...... 采用分组的思想,我们要统计答案的数对满足两个条件:同奇偶,同颜色.所以可以按这两个要求分组. 然后就是分组处理了,对于每组(有k个数),这里面的任意两对数都是满足条件的,可推出 ...
- 整除分块学习笔记+[CQOI2007]余数求和(洛谷P2261,BZOJ1257)
上模板题例题: [CQOI2007]余数求和 洛谷 BZOJ 题目大意:求 $\sum^n_{i=1}k\ mod\ i$ 的值. 等等……这题就学了三天C++的都会吧? $1\leq n,k\leq ...
随机推荐
- Codeforces Round #510 (Div. 2)(A)
传送门:Problem A https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 公园里有n个沙滩,a[i]表示第i个沙滩初始人数,现有m个人 ...
- MySQL5.5登录密码忘记了,怎嘛办?
1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysqld --skip-grant- tables回车.如果没有出现提示信息,那就对了. 4.再开一 ...
- 【Maven】eclipse中使用Maven、生命周期
1.在eclipse中创建maven工程 >>在eclipse中配置maven: 配置maven版本:Eclips自带了一个maven,一般不用自带的这个,而选择我们安装的那个maven ...
- raise TemplateDoesNotExist
raise TemplateDoesNotExist(template_name, chain=chain)django.template.exceptions.TemplateDoesNotExis ...
- java日期相关
JAVA中获得一个月最大天数的方法 参考博客:http://www.cnblogs.com/relucent/p/4566582.html Calendar 类是一个抽象类,为日历字段之间的转换提供了 ...
- JAVA记录-SpringMVC+Mybatis几个核心注意的地方
1.DispatcherServlet -- 前置控制器 DispatcherServlet是一个Servlet,所以可以配置多个DispatcherServlet. DispatcherServ ...
- .NET Framework自带的文件内存映射类
最近一直为文件内存映射发愁,整个两周一直折腾这个东西.在64位系统和32位系统还要针对内存的高低位进行计算.好麻烦..还是没搞定 偶然从MSDN上发现.NET 4.0把内存文件映射加到了.NET类库中 ...
- 通过 LPeg 介绍解析表达式语法(Parsing Expression Grammars)
通过 LPeg 介绍解析表达式语法(Parsing Expression Grammars) 译者: FreeBlues 修订版本: 1.00 最新链接: http://www.cnblogs.com ...
- ubuntu配置lua环境,并进行c与lua的相互调用
1.安装lua环境 先查看一下apt可获取的lua版本 我们选择lua5.1版本进行安装 sudo apt install lua5.1 安装完之后测试一下是否安装成功,如果可以正常使用,则lua环境 ...
- JS中的call()方法和apply()方法用法总结
原文引自:https://blog.csdn.net/ganyingxie123456/article/details/70855586 最近又遇到了JacvaScript中的call()方法和app ...