codechef Prime Distance On Tree FFT

161人阅读 评论(0)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=100005;
const double eps(1e-8);
typedef long long LL;
const double PI = acos(-1.0);
int isprim[maxn<<1];
void init(void){
for(int i=2;i<2*maxn;i++){
for(int j=2*i;j<2*maxn;j+=i){
isprim[j]=1;
}
}
}
struct Complex
{
double real, image;
Complex(double _real, double _image)
{
real = _real;
image = _image;
}
Complex(){}
};

Complex operator + (const Complex &c1, const Complex &c2)
{
return Complex(c1.real + c2.real, c1.image + c2.image);
}

Complex operator - (const Complex &c1, const Complex &c2)
{
return Complex(c1.real - c2.real, c1.image - c2.image);
}

Complex operator * (const Complex &c1, const Complex &c2)
{
return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real);
}

int rev(int id, int len)
{
int ret = 0;
for(int i = 0; (1 << i) < len; i++)
{
ret <<= 1;
if(id & (1 << i)) ret |= 1;
}
return ret;
}

Complex A[maxn<<1];
void FFT(Complex* a, int len, int DFT)
{
for(int i = 0; i < len; i++)
A[rev(i, len)] = a[i];
for(int s = 1; (1 << s) <= len; s++)
{
int m = (1 << s);
Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
for(int k = 0; k < len; k += m)
{
Complex w = Complex(1, 0);
for(int j = 0; j < (m >> 1); j++)
{
Complex t = w*A[k + j + (m >> 1)];
Complex u = A[k + j];
A[k + j] = u + t;
A[k + j + (m >> 1)] = u - t;
w = w*wm;
}
}
}
if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
for(int i = 0; i < len; i++) a[i] = A[i];
return;
}
Complex a[maxn<<1];
int size[maxn];
int vis[maxn];
vector<int>g[maxn];
vector<int>gg,gg1;
int dp[maxn];
void dfs(int u,int pa){
size[u]=1;
dp[u]=0;
gg.push_back(u);
for(int v:g[u]){
if(!vis[v]&&v!=pa){
dfs(v,u);
dp[u]=max(dp[u],size[v]);
size[u]+=size[v];
}
}
}
void dfs2(int u,int pa,int d){
gg.push_back(d);
gg1.push_back(d);
for(int v:g[u]){
if(!vis[v]&&v!=pa){
dfs2(v,u,d+1);
}
}
}
int num[maxn<<1];
LL xishu[maxn<<1];
LL get(vector<int>g2){
int len=1,s=0;
for(int q:g2){
s=max(s,q);
}
while(len<=s) len=len<<1;
len<<=1;
for(int i=0;i<len;i++){
num[i]=0;
}
for(int q:g2){
num[q]++;
}
for(int i=0;i<len;i++){
a[i]=Complex(num[i],0);
}
FFT(a,len,1);
for(int i=0;i<len;i++){
a[i]=a[i]*a[i];
}
FFT(a,len,-1);
for(int i=0;i<len;i++){
xishu[i]=(LL)(a[i].real+0.5);
}
for(int i=0;i<len/2;i++){
xishu[i<<1]-=num[i];
}
LL ans=0;
for(int i=2;i<len;i++){
xishu[i]/=2;
if(isprim[i]==0){
ans+=xishu[i];
}
}
return ans;
}
LL ans;
void solve(int u){
gg.clear();
for(int v:g[u]){
if(!vis[v]){
gg1.clear();
dfs2(v,u,1);
ans-=get(gg1);
}
}
gg.push_back(0);
ans+=get(gg);
}
void dfs1(int u){
gg.clear();
dfs(u,-1);
int root=1,ans=100000000;
for(int v:gg){
dp[v]=max(dp[v],dp[u]-dp[v]);
if(ans>dp[v]){
ans=dp[v];
root=v;
}
}
vis[root]=1;
solve(root);
for(int v:g[root]){
if(!vis[v]){
dfs1(v);
}
}
}
int main()
{
int n;
init();
cin>>n;
for(int i=0;i<n-1;i++){
int x,y;
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs1(1);
double ans1=(1.0)*ans/((LL)n*(n-1)/2);
printf("%.6lf\n",ans1);
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：87345次
• 积分：3100
• 等级：
• 排名：第11555名
• 原创：230篇
• 转载：0篇
• 译文：0篇
• 评论：14条
评论排行