传送门

fftfftfft一眼题(其实最先想到的是nttnttntt,wawawa了几次之后发现模数不够大果断弃疗写fftfftfft)

我们点分治统计答案的个数。

考虑现在已经统计出了到当前点的所有距离如何更新答案。

显然如果两个距离能够凑出一个质数的话就对答案有1的贡献。

所以相当于计算出每一个距离的个数。

然后可以推一个式子:

tota=∑i=0maxdiscnti∗cnta−itot_a=\sum_{i=0}^{maxdis}cnt_i*cnt_{a-i}tota​=∑i=0maxdis​cnti​∗cnta−i​

这不就是距离数组跟自己卷积吗?

果断fftfftfft优化。

然而fftfftfft的写法也wawawa了。

找dzyodzyodzyo拍了半天没错?

然后全局longlonglong longlonglong了一波终于过了233.

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
	int ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ans;
}
typedef long long ll;
const double pi=acos(-1.0);
const int N=2e5+5;
int n,pri[N],tot=0,msiz[N],siz[N],vis[N],rt,all,tmp[N],lim=1,tim=0,pos[N],maxn;
bool isp[N];
vector<int>e[N>>2];
ll ans=0;
inline void init(){
	isp[1]=1;
	for(register int i=2;i<=n;++i){
		if(!isp[i])pri[++tot]=i;
		for(register int j=1;j<=tot&&i*pri[j]<=n;++j){
			isp[i*pri[j]]=1;
			if(i%pri[j]==0)break;
		}
	}
}
struct Complex{
	double x,y;
	friend inline Complex operator+(const Complex&a,const Complex&b){return (Complex){a.x+b.x,a.y+b.y};}
	friend inline Complex operator-(const Complex&a,const Complex&b){return (Complex){a.x-b.x,a.y-b.y};}
	friend inline Complex operator*(const Complex&a,const Complex&b){return (Complex){a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};}
	friend inline Complex operator/(const Complex&a,const double&b){return (Complex){a.x/b,a.y/b};}
}cnt[N];
inline void fft(Complex a[],int type){
	for(register int i=0;i<lim;++i)if(i<pos[i])swap(a[i],a[pos[i]]);
	for(register int mid=1;mid<lim;mid<<=1){
		Complex wn=(Complex){cos(pi/mid),sin(type*pi/mid)};
		for(register int j=0,len=mid<<1;j<lim;j+=len){
			Complex w=(Complex){1,0};
			for(register int k=0;k<mid;++k,w=w*wn){
				Complex a0=a[j+k],a1=a[j+k+mid]*w;
				a[j+k]=a0+a1,a[j+k+mid]=a0-a1;
			}
		}
	}
	if(type==-1)for(register int i=0;i<lim;++i)a[i]=a[i]/(double)lim;
}
void getroot(int p,int fa){
	msiz[p]=siz[p]=1;
	for(register int i=0;i<e[p].size();++i){
		int v=e[p][i];
		if(v==fa||vis[v])continue;
		getroot(v,p),siz[p]+=siz[v],msiz[p]=max(msiz[p],siz[v]);
	}
	msiz[p]=max(msiz[p],all-siz[p]);
	if(msiz[p]<msiz[rt])rt=p;
}
void getdis(int p,int fa,int delt){
	cnt[delt].x+=1;
	maxn=max(maxn,delt);
	for(register int i=0;i<e[p].size();++i){
		int v=e[p][i];
		if(vis[v]||v==fa)continue;
		getdis(v,p,delt+1);
	}
}
inline void calc(int p,int delt,int type){
	maxn=0,getdis(p,0,delt),lim=1,tim=0;
	while(lim<=maxn*2)++tim,lim<<=1;
	for(register int i=0;i<lim;++i)pos[i]=(pos[i>>1]>>1)|((i&1)<<(tim-1));
	int sum=(int)-cnt[1].x;
	fft(cnt,1);
	for(register int i=0;i<lim;++i)cnt[i]=cnt[i]*cnt[i];
	fft(cnt,-1);
	for(register int i=1;i<=tot;++i){
		if(pri[i]>lim)break;
		sum+=(int)(cnt[pri[i]].x+0.5);
	}
	ans+=(ll)sum/2*type;
	for(register int i=0;i<lim;++i)cnt[i].x=cnt[i].y=0;
}
void solve(int p){
	calc(p,0,1),vis[p]=1;
	for(register int i=0;i<e[p].size();++i){
		int v=e[p][i];
		if(vis[v])continue;
		all=siz[v],rt=0,calc(v,1,-1),getroot(v,0),solve(rt);
	}
}
signed main(){
	freopen("lx.in","r",stdin);
	n=read(),init();
	for(register int i=1,u,v;i<n;++i)u=read(),v=read(),e[u].push_back(v),e[v].push_back(u);
	all=msiz[rt=0]=n,getroot(1,0),solve(rt);
	printf("%.8lf",(double)ans*2.0/(double)((double)n*(double)(n-1)));
	return 0;
}

2018.11.17 codechef PRIMEDST(点分治+fft)的更多相关文章

  1. International Programming Retreat Day(2018.11.17)

    时间:2018.11.17地点:北京国华投资大厦

  2. 2018.11.17 bzoj4259: 残缺的字符串(fft)

    传送门 fftfftfft套路题. 我们把aaa ~ zzz映射成111 ~ 262626,然后把∗*∗映射成000. 考虑对于两个长度都为nnn的字符串A,BA,BA,B. 我们定义一个差异函数di ...

  3. IOS面试题2018/11/17

    1.设计模式是什么?你知道哪些设计模式? 设计模式是一种编码经验,就是一种成熟的逻辑去处理某一种类型的事情. 1.MVC模式:model view controller,把模型,视图,控制器 层进行解 ...

  4. 2018.11.17 hdu5829Rikka with Subset(ntt)

    传送门 nttnttntt基础题. 考虑计算每一个数在排名为kkk时被统计了多少次来更新答案. 这样的话,设anskans_kansk​表示所有数的值乘上排名为kkk的子集数的总和. 则ansk=∑i ...

  5. Django学习笔记-2018.11.17

    URL配置: 项目下的urls.py配置的为URL总路径,在使用第二种方法在app下配置的urls.py是在总路径下的分路径 Templates: DTL初步使用 render()函数支持dict类型 ...

  6. 2018.11.17 Struts2框架入门

    Struts2 框架学习 一.struts2是什么? (1)概念 (2)struts2使用优势 自动封装参数 参数校验 结果的处理(转发|重定向) 国际化 显示等待页面 表单的防止重复提交 (3)st ...

  7. 2017 3 11 分治FFT

    考试一道题的递推式为$$f[i]=\sum_{j=1}^{i} j^k \times (i-1)! \times \frac{f[i-j]}{(i-j)!}$$这显然是一个卷积的形式,但$f$需要由自 ...

  8. hdu 5730 Shell Necklace [分治fft | 多项式求逆]

    hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...

  9. 洛谷 P4721 【模板】分治 FFT 解题报告

    P4721 [模板]分治 FFT 题目背景 也可用多项式求逆解决. 题目描述 给定长度为 \(n−1\) 的数组 \(g[1],g[2],\dots,g[n-1]\),求 \(f[0],f[1],\d ...

随机推荐

  1. Java 对象 引用,equal == string

    以前确实一直没注意这个概念,这次看了帖子才知道. 转载于:https://zwmf.iteye.com/blog/1738574 Java对象及其引用 关于对象与引用之间的一些基本概念. 初学Java ...

  2. 微信小程序模板中使用循环

    原创文章 本篇介绍如何在模板里使用循环. 方法一: template.wxml,该文件内容为: <template name="msgItem1"> <block ...

  3. 本地推送UILocalNotification的一些简单方法

    1.添加本地推送,需要在app添加推送.可以根据通知的userInfo的不同的键值对来区分不同的通知 UILocalNotification *notification = [[UILocalNoti ...

  4. Android 异步加载数据 AsyncTask异步更新界面

    官方文档:     AsyncTask enables proper and easy use of the UI thread. This class allows to perform backg ...

  5. c# mac地址 和http://xx.xx.xx/ 正则表达式匹配

    Mac  :^([0-9a-fA-F]{2})(([/\s:][0-9a-fA-F]{2}){5})$ C# 书写方式 一下是允许mac中间间隔符是“:”或者“-”两种输入方式 并且我把上边的正则表达 ...

  6. string+和stringbuffer的速度比较

    public class Main{ public static void main(String[] args){ /* 1 */ String string = "a" + & ...

  7. C++11并发编程实战 免费书籍

    C++11 博客http://www.cnblogs.com/haippy/p/3284540.html 网上推荐的C++多线程基本都是C++ Concurrency in Action 英文版的,中 ...

  8. (转)wcf项目程序调试

    由于使用分布式开发,因此在调试时,要分为客户端调试和服务端调试两种情况,下面就对这两种情况的调试步骤分别加以详细说明  调试客户端的页面代码 当仅仅需要调试客户端代码时,按照以下步骤进行操作: 1. ...

  9. Triangle2D类(Java)

    定义Triangle2D类,包含: 三个名为p1.p2和p3的MyPoint型数据域,这三个数据域都带有get和set方法.MyPoint在练习题10.4中定义. 一个无参构造方法,该方法创建三个坐标 ...

  10. Nginx 的 docker 部署

    1.输入命令 docker pull nginx:1.15 拉取 nginx 的镜像: 2.使用 docker images 查看拉取到的镜像信息: 3.在主机上创建用于映射的目录 mkdir -p ...