洛谷 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 ...
随机推荐
- dedecms获取顶级栏目名称、二级栏目名称实现方法 转
织梦DEDECMS文章.栏目页获取当前页面顶级栏目名称的方法 在用织梦做一些项目时,时常会碰到需要在当前页面调用顶级栏目名称的时候,织梦默认{dede:field name='typename' /} ...
- ElasticSearch搜索介绍四
ElasticSearch搜索 最基础的搜索: curl -XGET http://localhost:9200/_search 返回的结果为: { "took": 2, &quo ...
- springmvc 中@Controller和@RestController的区别
@Controller和@RestController的区别? 官方文档:@RestController is a stereotype annotation that combines @Respo ...
- 使用ajax实现form表单的submit事件
需求:如题,需要在登录页面使用ajax提交请求,并在本页面返回请求信息. 主要部分jS如下: //提交表单$("#loginForm").submit(function(){ va ...
- go for range
func main() { var str = "hellow worda" for _, val := range str { fmt.Printf("%q" ...
- Java编程思想 学习笔记1
一.对象导论 1.抽象过程 Alan Kay曾经总结了第一个成功的面向对象语言.同时也是Java所基于的语言之一的Smalltalk的五个基本特性,这些特性表现了纯粹的面向对象程序设计方式 1)万物皆 ...
- JS在Html中使用JavaScript
一.三种方式 1)<script>元素 2)外部文件 3)文档模式 二.<script>元素 是向HTML页面插入JavaScript的主要方法:HTML 4.01为<s ...
- Linux cache 缓存过大
linux cache 缓存过大 : 除重启服务 之外:直接释放内存方式之一: 修改配置释放cached内存: echo > /proc/sys/vm/drop_caches
- PyCharm的调试
1.设置断点 2.debug模式运行 F8 下一行代码 查看当前位置所有局部变量:print(locals()) 查看全局变量: print( ...
- luogu P3978 [TJOI2015]概率论
看着就是要打表找规律 使用以下代码 for(int i=3;i<=20;i++) { int a1=0,a2=0; for(int j=1;j<i;j++) { for(int k=0;k ...