【CF434E】Furukawa Nagisa's Tree

题意:一棵n个点的树,点有点权。定义$G(a,b)$表示:我们将树上从a走到b经过的点都拿出来,设这些点的点权分别为$z_0,z_1...z_{l-1}$,则$G(a,b)=z_0+z_1k^1+z_2k^2+...+z_{l-1}k^{l-1}$。如果$G(a,b)=X \mod Y$(保证Y是质数),则我们称(a,b)是好的,否则是坏的。现在想知道,有多少个三元组(a,b,c),满足(a,b),(b,c),(a,c)都是好的或者都是坏的?

$n\le 10^5,Y\le 10^9$

题解:由于一个点对要么是好的要么是坏的,所以我们可以枚举一下所有符合条件的3元组的情况。不过符合条件需要3条边都相同,那我们可以反过来,统计不合法的3元组的情况(一共$2^3-2$种情况)。经过观察我们发现,我们可以在 同时连接两种颜色的边 的那个点处统计贡献,即把三元组的贡献放到了点上。我们设$in_0(),in_1(i),out_0(i),out_1(i)$表示i有多少个好(坏)边连入(出),则一个点对答案的贡献就变成:

$2in_0(i)in_1(i)+2out_0(i)out_1(i)+in_0(i)out_1(i)+in_1(i)out_0(i)$

最后将答案/2即可。

所以现在我们只需要求:对于每个点,有多少好边连入(连出)。这个用点分治可以搞定,因为我们容易计算两个多项式连接起来的结果。本题我采用的是容斥式的点分治。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100010;
typedef long long ll;
int n,cnt,tot,mn,rt;
ll X,Y,K,Ki,ans;
ll pw[maxn],pi[maxn],v[maxn],in1[maxn],in0[maxn],out1[maxn],out0[maxn];
int to[maxn<<1],nxt[maxn<<1],head[maxn],vis[maxn],siz[maxn];
struct node
{
ll x;
int y;
node() {}
node(ll a,int b) {x=a,y=b;}
bool operator < (const node &a) const {return x<a.x;}
}p[maxn],q[maxn];
inline int rd()
{
char gc=getchar(); int ret=0;
while(gc<'0'||gc>'9') gc=getchar();
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret;
}
inline void add(int a,int b)
{
to[cnt]=b,nxt[cnt]=head[a],head[a]=cnt++;
}
inline ll pm(ll x,ll y)
{
ll z=1;
while(y)
{
if(y&1) z=z*x%Y;
x=x*x%Y,y>>=1;
}
return z;
}
void getrt(int x,int fa)
{
int i,tmp=0;
siz[x]=1;
for(i=head[x];i!=-1;i=nxt[i]) if(!vis[to[i]]&&to[i]!=fa) getrt(to[i],x),siz[x]+=siz[to[i]],tmp=max(tmp,siz[to[i]]);
tmp=max(tmp,n-siz[x]);
if(tmp<mn) mn=tmp,rt=x;
}
void getp(int x,int fa,int dep,ll s1,ll s2)
{
s1=(s1*K+v[x])%Y,s2=(s2+v[x]*((!dep)?0:pw[dep-1]))%Y,dep++;
p[++tot]=node((X-s1+Y)*pi[dep]%Y,x),q[tot]=node(s2,x);
for(int i=head[x];i!=-1;i=nxt[i]) if(!vis[to[i]]&&to[i]!=fa)
getp(to[i],x,dep,s1,s2);
}
void calc(int x,int flag,int dep,ll s1,ll s2)
{
int i,j,cnt;
tot=0;
s1=(s1*K+v[x])%Y,s2=(s2+v[x]*((!dep)?0:pw[dep-1]))%Y,dep++;
p[++tot]=node((X-s1+Y)*pi[dep]%Y,x),q[tot]=node(s2,x);
for(i=head[x];i!=-1;i=nxt[i]) if(!vis[to[i]]) getp(to[i],x,dep,s1,s2);
sort(p+1,p+tot+1),sort(q+1,q+tot+1);
for(cnt=0,i=j=1;i<=tot;i++)
{
for(;j<=tot&&q[j].x<=p[i].x;j++)
{
if(j==1||q[j].x!=q[j-1].x) cnt=0;
cnt++;
}
if(j!=1&&q[j-1].x==p[i].x) out1[p[i].y]+=cnt*flag;
}
for(cnt=0,i=j=1;i<=tot;i++)
{
for(;j<=tot&&p[j].x<=q[i].x;j++)
{
if(j==1||p[j].x!=p[j-1].x) cnt=0;
cnt++;
}
if(j!=1&&p[j-1].x==q[i].x) in1[q[i].y]+=cnt*flag;
}
}
void dfs(int x)
{
vis[x]=1;
int i;
calc(x,1,0,0,0);
for(i=head[x];i!=-1;i=nxt[i]) if(!vis[to[i]])
{
calc(to[i],-1,1,v[x],0);
tot=siz[to[i]],mn=1<<30,getrt(to[i],x),dfs(rt);
}
}
int main()
{
//freopen("cf434E.in","r",stdin); n=rd(),Y=rd(),K=rd(),X=rd(),Ki=pm(K,Y-2);
int i,a,b;
memset(head,-1,sizeof(head));
for(i=1;i<=n;i++) v[i]=rd();
for(i=pw[0]=pi[0]=1;i<=n;i++) pw[i]=pw[i-1]*K%Y,pi[i]=pi[i-1]*Ki%Y;
for(i=1;i<n;i++) a=rd(),b=rd(),add(a,b),add(b,a);
tot=n,mn=1<<30,getrt(1,0),dfs(rt);
for(i=1;i<=n;i++)
{
in0[i]=n-in1[i],out0[i]=n-out1[i];
ans+=2*in1[i]*in0[i]+2*out1[i]*out0[i]+in0[i]*out1[i]+in1[i]*out0[i];
}
printf("%lld",1ll*n*n*n-ans/2);
return 0;
}

【CF434E】Furukawa Nagisa's Tree 点分治的更多相关文章

  1. Codeforces 434E - Furukawa Nagisa's Tree(三元环+点分治)

    Codeforces 题面传送门 & 洛谷题面传送门 场号 hopping,刚好是我的学号(指 round 的编号) 注:下文中分别用 \(X,Y,K\) 代替题目中的 \(x,y,k\) 注 ...

  2. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  3. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  4. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  5. [bzoj 1468][poj 1741]Tree [点分治]

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  6. POJ 1741 Tree(点分治点对<=k)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  7. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  8. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  9. CF1039D You Are Given a Tree 根号分治,贪心

    CF1039D You Are Given a Tree LG传送门 根号分治好题. 这题可以整体二分,但我太菜了,不会. 根号分治怎么考虑呢?先想想\(n^2\)暴力吧.对于每一个要求的\(k\), ...

随机推荐

  1. 【转】java日志组件介绍(common-logging,log4j,slf4j,logback )

    common-logging common-logging是apache提供的一个通用的日志接口.用户可以自由选择第三方的日志组件作为具体实现,像log4j,或者jdk自带的logging, comm ...

  2. Mybatis : "less than" issue in Select annotations

    I am using java7, spring 3 and mybatis Pom.xml <org.mybatis-version>3.2.8</org.mybatis-vers ...

  3. SharePoint 读取内容的插件之SharepointPlus

    前言 最近,一直在前端和SharePoint进行交互,然后,发现一个好用的插件,分享给大家. 首先,需要添加一个引用,如下图: 当然,我这里只是举个例子,亲们一定要去下载这个库,然后传到服务器或者文档 ...

  4. FileUpload1.PostedFile.FileName 获取的文件名

    例如文件叫做 c:\web\a.jpg 如果用IE,则返回   c:\web\a.jpg 含路径 如果用Chrome,则返回 a.jpg 不含路径.

  5. 分布式环境中,模块数据交互协议分析 (百度brpc)

    1. 背景 之前听到同事说,要为自己的模块考虑写个数据协议.今天有空想了一下.写出来,方便后续使用. 开源代码brpc中可以支持多种协议,nshead.redis.mongo等20多种协议. 2. 什 ...

  6. one-stage object detectors(1)

    2019/04/08 强烈推荐:深入理解one-stage目标检测算法 yolo系列 one-stage object detectors(YOLO and SSD) 在不专一的模型中,每个检测器应该 ...

  7. solr集群构建的基本流程介绍

    先从第一台solr服务器说起:1. 它首先启动一个嵌入式的Zookeeper服务器,作为集群状态信息的管理者,2. 将自己这个节点注册到/node_states/目录下3. 同时将自己注册到/live ...

  8. [android警告]AndroidManifest.xml警告 Not targeting the latest versions of Android

    警告:Not targeting the latest versions of Android; compatibility modes apply.Consider testing and upda ...

  9. Java WebSocket实现网络聊天室(群聊+私聊)

    1.简单说明 在网上看到一份比较nice的基于webSocket网页聊天项目,准备看看学习学习,如是有了这篇文章!原博主博客:http://blog.csdn.net/Amayadream/artic ...

  10. PHP类中self和$this的区别

    1.self代表类,$this代表对象2.能用$this的地方一定使用self,能用self的地方不一定能用$this静态的方法中不能使用$this,静态方法给类访问的. 今天在使用静态方法的时候,使 ...