2018.11.17 codechef PRIMEDST(点分治+fft)
传送门
fftfftfft一眼题(其实最先想到的是nttnttntt,wawawa了几次之后发现模数不够大果断弃疗写fftfftfft)
我们点分治统计答案的个数。
考虑现在已经统计出了到当前点的所有距离如何更新答案。
显然如果两个距离能够凑出一个质数的话就对答案有1的贡献。
所以相当于计算出每一个距离的个数。
然后可以推一个式子:
tota=∑i=0maxdiscnti∗cnta−itot_a=\sum_{i=0}^{maxdis}cnt_i*cnt_{a-i}tota=∑i=0maxdiscnti∗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)的更多相关文章
- International Programming Retreat Day(2018.11.17)
时间:2018.11.17地点:北京国华投资大厦
- 2018.11.17 bzoj4259: 残缺的字符串(fft)
传送门 fftfftfft套路题. 我们把aaa ~ zzz映射成111 ~ 262626,然后把∗*∗映射成000. 考虑对于两个长度都为nnn的字符串A,BA,BA,B. 我们定义一个差异函数di ...
- IOS面试题2018/11/17
1.设计模式是什么?你知道哪些设计模式? 设计模式是一种编码经验,就是一种成熟的逻辑去处理某一种类型的事情. 1.MVC模式:model view controller,把模型,视图,控制器 层进行解 ...
- 2018.11.17 hdu5829Rikka with Subset(ntt)
传送门 nttnttntt基础题. 考虑计算每一个数在排名为kkk时被统计了多少次来更新答案. 这样的话,设anskans_kansk表示所有数的值乘上排名为kkk的子集数的总和. 则ansk=∑i ...
- Django学习笔记-2018.11.17
URL配置: 项目下的urls.py配置的为URL总路径,在使用第二种方法在app下配置的urls.py是在总路径下的分路径 Templates: DTL初步使用 render()函数支持dict类型 ...
- 2018.11.17 Struts2框架入门
Struts2 框架学习 一.struts2是什么? (1)概念 (2)struts2使用优势 自动封装参数 参数校验 结果的处理(转发|重定向) 国际化 显示等待页面 表单的防止重复提交 (3)st ...
- 2017 3 11 分治FFT
考试一道题的递推式为$$f[i]=\sum_{j=1}^{i} j^k \times (i-1)! \times \frac{f[i-j]}{(i-j)!}$$这显然是一个卷积的形式,但$f$需要由自 ...
- hdu 5730 Shell Necklace [分治fft | 多项式求逆]
hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...
- 洛谷 P4721 【模板】分治 FFT 解题报告
P4721 [模板]分治 FFT 题目背景 也可用多项式求逆解决. 题目描述 给定长度为 \(n−1\) 的数组 \(g[1],g[2],\dots,g[n-1]\),求 \(f[0],f[1],\d ...
随机推荐
- 导入Unity插件时出现Failed to import package with error: Couldn't decompress package
导入Unity插件时出现Failed to import package with error: Couldn't decompress package 一开始以为压缩包本身有问题,坏了 后来发现在父 ...
- 初识Netty
我们已经了解了Socket通信/IO/NIO/AIO编程,对于通信模型已经有了一个初步的认识,其实我们之前所学习的仅仅是一个模型,如果想把这些真正的用于实际工作中去,其实我们之前所学习的仅仅是一个模型 ...
- [leetcode]127. Word Ladder单词接龙
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- Exception starting filter encodingFilter
1. maven运行tomcat6出现错误Exception starting filter encodingFilter: 严重: Exception starting filter encodin ...
- ES6中的Promise使用方法与总结
在javascript中,代码是单线程执行的,对于一些比较耗时的IO操作,都是通过异步回调函数来实现的. 但是这样会存在一个问题,当下一个的操作需要上一个操作的结果时,我们只能把代码嵌到上一个操作的回 ...
- Sqlite文件在ubunut的查看
1. How to list the tables in a SQLite database file that was opened with ATTACH? The .tables, and .s ...
- UI移动设备屏幕知识
今天同学给我了一个设计UI的软件,其实我是一头雾水的,有点孤陋寡闻了,设计UI还有这样的软件.百度了下,看见这篇文章还是很有收获的,原来UI还是一门如此高深的学问.把这篇文章和大家一起分享吧,希望你们 ...
- SSI服务端包含技术
1.页面拆出来怎么样通过web服务浏览呢? 使用web服务(例如nginx)的SSI技术,将多个子页面合并渲染输出. 2.SSI是什么? 3. ssi包含类似于jsp页面中的incluce指令,ssi ...
- Pycharm使⽤用秘笈v0.3PyCharm使⽤用秘籍
Pycharm使⽤用秘笈v0.3PyCharm使⽤用秘籍 1. PyCharm的基本使⽤用 在PyCharm下为你的Python项⽬目配置Python解释器器 1. Project:当前项⽬目名> ...
- jQuery的鼠标悬停时放大图片的效果
这是一个基于jQuery的效果,当鼠标在小图片上悬停时,会弹出一个大图,该大图会跟随鼠标的移动而移动.这个效果最初源于小敏同志的一个想法,刚开始做的时候只能实现弹出的图片是固定的,不能随鼠标移动,最后 ...