bjoi 2018 求和 唯一一道可能切的题一个数组还没开long long就成0分了

题目大意:

一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k次方和,而且每次的k可能是不同的

此处节点深度的定义是这个节点到根的路径上的边数

思路:

考试的时候随便写了个树剖

剖下来之后搞五十个次方的前缀和

然后一个数组乘起来没开long long

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define ll long long
#define MAXN 300100
#define inf 2139062143
#define MOD 998244353
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-;ch=getchar();}
while(isdigit(ch)) {x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,hsh[MAXN],HSH[MAXN],bl[MAXN],sz[MAXN],dep[MAXN],fa[MAXN];
int nxt[MAXN<<],fst[MAXN],to[MAXN<<],cnt;
ll sum[][MAXN],val[MAXN],res;
void add(int u,int v){nxt[++cnt]=fst[u],fst[u]=cnt,to[cnt]=v;}
void dfs(int x)
{
sz[x]=val[x]=;
for(int i=fst[x];i;i=nxt[i])
{
if(to[i]==fa[x]) continue;
dep[to[i]]=dep[x]+,fa[to[i]]=x;
dfs(to[i]);sz[x]+=sz[to[i]];
}
}
void DFS(int x,int anc)
{
int hvs=;hsh[x]=++cnt,HSH[cnt]=x,bl[x]=anc;
for(int i=fst[x];i;i=nxt[i])
if(to[i]!=fa[x]&&sz[to[i]]>sz[hvs]) hvs=to[i];
if(!hvs) return ;
DFS(hvs,anc);
for(int i=fst[x];i;i=nxt[i])
if(to[i]!=fa[x]&&to[i]!=hvs) DFS(to[i],to[i]);
}
int main()
{
n=read();int a,b,c;
for(int i=;i<n;i++) {a=read(),b=read();add(a,b);add(b,a);}
dfs();cnt=;
DFS(,);
for(int i=;i<=;i++)
for(int j=;j<=n;j++) (val[j]*=dep[HSH[j]])%=MOD,sum[i][j]=(sum[i][j-]+val[j])%MOD;
m=read();
while(m--)
{
a=read(),b=read(),c=read(),res=;
while(bl[a]!=bl[b])
{
if(dep[bl[a]]<dep[bl[b]]) swap(a,b);
(res+=sum[c][hsh[a]]-sum[c][hsh[bl[a]]-]+MOD)%=MOD;
a=fa[bl[a]];
}
if(dep[a]>dep[b]) swap(a,b);
(res+=sum[c][hsh[b]]-sum[c][hsh[a]-]+MOD)%=MOD;
printf("%lld\n",res);
}
}

luogu 4427 求和的更多相关文章

  1. Luogu 2671 求和 NOIP2015T3

    题目链接 题解 20pts $O(n^3)$枚举$x,y,z$,根据题目要求判断 40pts $O(n^2)$枚举$x,z$,需要满足$x,z$奇偶相同 20~40pts的代码我都没有写过...就不贴 ...

  2. Luogu P1625 求和

    题意 给定两个整数 \(n,m\),求 \[\sum\limits_{i=1}^{n}\frac{1}{\prod\limits_{j=i}^{i+m-1}j} \] \(\texttt{Data R ...

  3. luogu P1630 求和(枚举暴力)

    题意 题解 可以发现当a=10001时, 和1是等价的. 所以这题就水了. #include<iostream> #include<cstring> #include<c ...

  4. [Luogu] 余数求和

    question: $$\sum_{i=1}^{n} k \bmod i$$$$\sum_{i=1}^{n} k - \lfloor \frac{k}{i} \rfloor i$$$$\sum_{i= ...

  5. [Luogu 2261] CQOI2007 余数求和

    [Luogu 2261] CQOI2007 余数求和 这一定是我迄今为止见过最短小精悍的省选题了,核心代码 \(4\) 行,总代码 \(12\) 行,堪比小凯的疑惑啊. 这题一看暴力很好打,然而 \( ...

  6. BZOJ 4555 Luogu P4091 [HEOI2016/TJOI2016]求和 (第二类斯特林数)

    题目链接 (luogu) https://www.luogu.org/problem/P4091 (bzoj) https://www.lydsy.com/JudgeOnline/problem.ph ...

  7. [Luogu P2261] [CQOI2007]余数求和 (取模计算)

    题面 传送门:https://www.luogu.org/problemnew/show/P2261 Solution 这题显然有一个O(n)的直接计算法,60分到手. 接下来我们就可以拿出草稿纸推一 ...

  8. 【Bzoj4555】【Luogu P4091】求和(NTT)

    题面 Bzoj Luogu 题解 先来颓柿子 $$ \sum_{i=0}^n\sum_{j=0}^iS(i,j)2^jj! \\ =\sum_{j=0}^n2^jj!\sum_{i=0}^nS(i,j ...

  9. Luogu 4091 [HEOI2016/TJOI2016]求和

    BZOJ 4555 一道模板题. 第二类斯特林数有公式: $$S(n, m) = \frac{1}{m!}\sum_{i = 0}^{m}(-1)^i\binom{m}{i}(m - i)^n$$ 考 ...

随机推荐

  1. WinMain名词解析

    WinMain程序名词解析 int WINAPI WinMain(HINSTANCE hInstance ,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nS ...

  2. LeetCode 123. Best Time to Buy and Sell Stock III (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. STM32F407 跑马灯 库函数版 个人笔记

    原理图: MCU在开发板原理图的第二页,LED在开发板原理图的第三页 由图可知,PF9 ,PF10 若输出低电平则灯亮,高电平则灯灭 选推挽输出 代码步骤 使能IO口时钟. 调用函数RCC_AHB1P ...

  4. HDU 4941

    Magical Forest Problem Description There is a forest can be seen as N * M grid. In this forest, ther ...

  5. codeforces 369B

    #include<stdio.h>//题没读懂,没做出来 int main() {      int n,k,l,r,s,s1,m,a,i; while(scanf("%d%d% ...

  6. android listVIew实现button按钮监听程序

    1.重写simpleAdapter 方法@Override public HashMap<String,String> getItem(int position) { // TODO Au ...

  7. 2018/3/14 Hadoop学习笔记(一)

    首先,什么是Hadoop?为什么它是现在大数据处理最热门的框架呢?(正确来说,现在Hadoop是一个生态圈) Hadoop是apache下一套开源的服务框架,它主要的作用就是利用服务器集群,来对海量数 ...

  8. Ice Cave-CodeForces(广搜)

    链接:http://codeforces.com/problemset/problem/540/C You play a computer game. Your character stands on ...

  9. POJ1068 Parencodings 解题报告

    Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...

  10. JSP发送电子邮件

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/sending-email.html: 发送一个简单的电子邮件 给出一个简单的例子,从机器上发送一个简单的 ...