【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. securecrt通过ssh连接板子: 密钥交换失败,没有兼容的加密程序

    在用securecrt连接板子时遇到如下问题: 需要修改板子上的/etc/ssh/ssh_config和/etc/ssh/sshd_config. 修改/etc/ssh/ssh_config,取消下面 ...

  2. 【不做标题党,只做纯干货】HashMap在jdk1.7和1.8中的实现

     同步首发:http://www.yuanrengu.com/index.php/20181106.html Java集合类的源码是深入学习Java非常好的素材,源码里很多优雅的写法和思路,会让人叹为 ...

  3. 解决 docker 报错: Error starting daemon: error initializing graphdriver: backing file system is unsupported for this graph driver

    CentOS 7.5 x64下 sudo yum install docker -y systemctl enable docker systemctl start docker 发现启动失败 jou ...

  4. 对图片进行透明化处理-使用java程序

    因需要将一张白色背景图片处理为透明色,因此上网上搜了搜处理方案,可以通过ps,和美图秀秀,但是我电脑上并没有这两个软件,下载安装太耗时.从网上搜了搜发现原来可以使用java代码进行处理,代码如下: i ...

  5. SpringBoot企业级框架

    Zebra 微服务框架 springBoot GitHub地址:https://github.com/ae6623/Zebra OSCGit地址:http://git.oschina.net/ae66 ...

  6. 关于安装 Microsoft Office

    安装 Microsoft Office 道路可谓漫漫…… 学校信息网络中心提供的“正版Office”安装了好几次都没成功…… [关于安装包和安装] 看了这篇经验,还不错:  office2018官方下 ...

  7. 树莓派进阶之路 (037) - 设置树莓派3 B+的静态IP

    修改/etc/dhcpcd.conf 文件 sudo vim /etc/dhcpcd.conf interface eth0 static ip_address= static routers=192 ...

  8. SNF框架及机器人2018年1-9月份升级内容

    1月 增加评星控件.年月选择控件 完善表格弹框的封装,增加多选弹框 的封装 增加表格 单元格合并.列头必填与可填写的标识 4月 关于分页查询和排序的各种修改(扶额) 导入excel优化 bs计算合计的 ...

  9. Delphi如何创建并绘制EMF图形文件

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  10. grokking deep learning

    https://www.manning.com/books/grokking-deep-learning?a_aid=grokkingdl&a_bid=32715258