【CodeChef】Prime Distance On Tree
给定一棵边长都是\(1\)的树,求有多少条路径长度为质数
树上路径自然是点分治去搞,但是发现要求是长度为质数,总不能对每一个质数都判断一遍吧
自然是不行的,这个东西显然是一个卷积,我们合并的时候显然可以直接大力\(NTT\)
但是需要注意的是我们访问子树的顺序必须是先访问深度小的子树,否则轻松被菊花加长链卡掉
但是\(CodeChef\)数据水啊,就这样直接搞过去了
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#define re register
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
const int maxn=1e5+5;
const int inf=1e9;
inline int read() {
char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
struct E{int v,nxt;}e[maxn];
const LL mod=998244353;
const LL G[2]={3,332748118};
int sum[maxn],mx[maxn],vis[maxn],st[maxn];
int now,S,rt,top,n,num,head[maxn],tax[maxn];
int f[maxn],p[maxn>>1];
int rev[262145],len,L;
LL A[262145],B[262145],ans;
inline void add(int x,int y) {e[++num].v=y;e[num].nxt=head[x];head[x]=num;}
inline LL ksm(LL a,int b) {
LL S=1;
while(b) {if(b&1) S=S*a%mod;b>>=1;a=a*a%mod;}
return S;
}
inline void NTT(LL *f,int o) {
for(re int i=0;i<len;i++)
if(i<rev[i]) std::swap(f[i],f[rev[i]]);
for(re int i=2;i<=len;i<<=1) {
int ln=i>>1;LL og1=ksm(G[o],(mod-1)/i);
for(re int l=0;l<len;l+=i) {
LL t,og=1;
for(re int x=l;x<l+ln;++x) {
t=(og*f[ln+x])%mod;
f[ln+x]=(f[x]-t+mod)%mod;
f[x]=(f[x]+t)%mod;
og=(og*og1)%mod;
}
}
}
if(!o) return;
LL inv=ksm(len,mod-2);
for(re int i=0;i<len;i++) f[i]=(f[i]*inv)%mod;
}
inline void mul() {
int m=0;
for(re int i=1;i<=top;i++) m=max(m,st[i]);
if(!L) {L=m;return;}
for(re int i=1;i<=top;i++) B[st[i]]++;
len=1;while(len<m+L+2) len<<=1;
for(re int i=0;i<len;i++) rev[i]=rev[i>>1]>>1|((i&1)?len>>1:0);
for(re int i=1;i<=L;i++) A[i]=tax[i];
NTT(A,0),NTT(B,0);
for(re int i=0;i<len;i++) A[i]=(A[i]*B[i])%mod;
NTT(A,1);
for(re int i=1;i<=p[0]&&p[i]<=L+m;i++) ans+=A[p[i]];
L=max(L,m);
for(re int i=0;i<len;i++) A[i]=B[i]=0;
}
void getroot(int x,int fa) {
sum[x]=1;mx[x]=0;
for(re int i=head[x];i;i=e[i].nxt) {
if(vis[e[i].v]||e[i].v==fa) continue;
getroot(e[i].v,x);
sum[x]+=sum[e[i].v];
mx[x]=max(mx[x],sum[e[i].v]);
}
mx[x]=max(mx[x],S-sum[x]);
if(mx[x]<now) now=mx[x],rt=x;
}
void getdis(int x,int fa,int L) {
st[++top]=L;
for(re int i=head[x];i;i=e[i].nxt) {
if(vis[e[i].v]||e[i].v==fa) continue;
getdis(e[i].v,x,L+1);
}
}
void dfs(int x) {
vis[x]=1;
for(re int i=head[x];i;i=e[i].nxt) {
if(vis[e[i].v]) continue;
top=0;getdis(e[i].v,x,1);
for(re int j=1;j<=top;j++)
if(!f[st[j]]) ans++;
mul();
for(re int j=1;j<=top;j++) tax[st[j]]++;
}
for(re int i=1;i<=L;i++) tax[i]=0;
L=0;
for(re int i=head[x];i;i=e[i].nxt) {
if(vis[e[i].v]) continue;
S=sum[e[i].v];now=inf;
getroot(e[i].v,x);dfs(rt);
}
}
int main() {
n=read();
for(re int x,y,i=1;i<n;i++)
x=read(),y=read(),add(x,y),add(y,x);
f[1]=1;
for(re int i=2;i<=n;i++) {
if(!f[i]) p[++p[0]]=i;
for(re int j=1;j<=p[0]&&p[j]*i<=n;j++) {
f[p[j]*i]=1;
if(i%p[j]==0) break;
}
}
S=n,now=inf;getroot(1,0);dfs(rt);
LL C=(LL)n*(LL)(n-1)/2ll;
printf("%.6lf\n",(double)ans/(double)C);
return 0;
}
【CodeChef】Prime Distance On Tree的更多相关文章
- CodeChef - PRIMEDST Prime Distance On Tree 树分治 + FFT
Prime Distance On Tree Problem description. You are given a tree. If we select 2 distinct nodes unif ...
- 【UVA10140】Prime Distance
题目大意:求出一个给定区间 [l, r] 内相邻素数之间的最大距离和最小距离. 题解:由于 l, r 的范围太大,没法直接用筛法得出区间的素数.考虑筛出区间的素数等价于筛掉区间内的所有和数, 根据算术 ...
- 一本通1619【例 1】Prime Distance
1619: [例 1]Prime Distance 题目描述 原题来自:Waterloo local,题面详见 POJ 2689 给定两个整数 L,R,求闭区间 [L,R] 中相邻两个质数差值最小的数 ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【BZOJ2959】长跑(Link-Cut Tree,并查集)
[BZOJ2959]长跑(Link-Cut Tree,并查集) 题面 BZOJ 题解 如果保证不出现环的话 妥妥的\(LCT\)傻逼题 现在可能会出现环 环有什么影响? 那就可以沿着环把所有点全部走一 ...
- 【BZOJ2588】Count On a Tree(主席树)
[BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...
- 【BZOJ2816】【ZJOI2012】网络(Link-Cut Tree)
[BZOJ2816][ZJOI2012]网络(Link-Cut Tree) 题面 题目描述 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相 ...
- 【CodeChef】Querying on a Grid(分治,最短路)
[CodeChef]Querying on a Grid(分治,最短路) 题面 Vjudge CodeChef 题解 考虑分治处理这个问题,每次取一个\(mid\),对于\(mid\)上的三个点构建最 ...
- 【CF434E】Furukawa Nagisa's Tree 点分治
[CF434E]Furukawa Nagisa's Tree 题意:一棵n个点的树,点有点权.定义$G(a,b)$表示:我们将树上从a走到b经过的点都拿出来,设这些点的点权分别为$z_0,z_1... ...
随机推荐
- 【原】Redis实现生成自增流水号
场景: 公司内部有个业务场景是后台审核之后需要生成一个流水号,规则是: 201807280001,201807280002,201807280003,后面四位依次递增,前面年月日取当前时间并且转换成y ...
- 【转】ArrayBlockingQueue浅析
ArrayBlockingQueue是常用的线程集合,在线程池中也常常被当做任务队列来使用.使用频率特别高.他是维护的是一个循环队列(基于数组实现),循环结构在数据结构中比较常见,但是在源码实现中还是 ...
- MongoDB2.x升级到3.x解决方案
MongoDB2.x版本Maven配置 <!-- mongodb --> <dependency> <groupId>org.springframework.dat ...
- 微软正式开源Blazor,将.NET带回到浏览器
微软 ASP.NET 团队近日正式开源了Blazor,这是一个Web UI框架,可通过WebAssembly在任意浏览器中运行 .Net. Blazor旨在简化快速的单页面 .Net 浏览器应用的构建 ...
- Ubuntu双系统环境下隐藏掉其他开机启动项
系统环境:ubuntu16.04需求:PC装的双系统(ubuntu+win10),为了应对某些需求,需要将win10系统给从电脑上消失,让你看不见也进不去它.做法:当然不可能真的删除掉win10系统, ...
- js-用于检测类数组对象的函数
//判定o是否是一个类数组对象 //字符串和函数有length属性,但是它们 //可以用typeof检测将其排除.在客户端JavaScript中,DOM文本节点 //也有length属性,需要用额外判 ...
- 关于java.lang.NoClassDefFoundError: org/apache/commons/collections/FastHashMap的错误解决办法
在JavaEE开发中,在把配置文件中的数据或用户表单提交上来的数据,封装在相应JavaBean的对象的对应属性中时:在实际开发中,使用第三方法工具包BeanUtils(commons-beanutil ...
- Scala + Thrift+ Zookeeper+Flume+Kafka配置笔记
1. 开发环境 1.1. 软件包下载 1.1.1. JDK下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downl ...
- hadoop HA集群搭建步骤
NameNode DataNode Zookeeper ZKFC JournalNode ResourceManager NodeManager node1 √ √ √ √ node2 ...
- Android MVP Plugin,一键完成MVP结构代码编写
推荐一个Gradle的学习系列,Gradle相关的知识一直很匮乏,难得发现一个不错的系列: http://www.cnblogs.com/davenkin/p/gradle-learning-1.ht ...