3
13
2015
5

Prime Distance On Tree

Problem description.

You are given a tree. If we select 2 distinct nodes uniformly at random, what's the probability that the distance between these 2 nodes is a prime number?

Input

The first line contains a number N: the number of nodes in this tree.
The following N-1 lines contain pairs a[i] and b[i], which means there is an edge with length 1 between a[i] and b[i].

Output

Output a real number denote the probability we want.
You'll get accept if the difference between your answer and standard answer is no more than 10^-6.

Constraints

2 ≤ N ≤ 50,000

The input must be a tree.

Example

Input:
5
1 2
2 3
3 4
4 5

Output:
0.5

Explanation

We have C(5, 2) = 10 choices, and these 5 of them have a prime distance:

1-3, 2-4, 3-5: 2

1-4, 2-5: 3

Note that 1 is not a prime number.

题意:

给你一棵树,求有多少对点之间的距离是质数。
2 ≤ N ≤ 50000 时限5s
第一次写点分。
点分+FFT
把点到root的距离卷一下得到答案
当然要减子树的答案。。
点分还要再写几题熟悉下=
#include <cstdio>
#include <algorithm>
#include <cmath>
#define maxn 100005
using namespace std;
struct node{double x,y;}a[maxn*4],b[maxn*4],w[2][maxn*4];
inline node operator +(node a,node b){return (node){a.x+b.x,a.y+b.y};}
inline node operator -(node a,node b){return (node){a.x-b.x,a.y-b.y};}
inline node operator *(node a,node b){return (node){a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};}
int c,e,f[maxn],g,h,i,j,k,l,m,n,N,All,root,Max,tot;
double ans;
int rev[maxn*4],way[maxn*2],next[maxn*2],last[maxn];
int data[maxn],d[maxn],can[maxn],size[maxn];
int F[maxn],p[maxn];

inline void make(int a,int b){
	way[++tot]=b;next[tot]=last[a];last[a]=tot;
	way[++tot]=a;next[tot]=last[b];last[b]=tot;
}

inline void pre(){
	for(int i=0;i<N;i++){
		int j=1,x=i,y=0;
		for(;j<N;j<<=1,x>>=1)(y<<=1)|=(x&1);
		rev[i]=y;
	}
	for(int i=0;i<N;i++){
		double e=2.0*acos(-1)*i/N;
		w[0][i]=(node){cos(e),sin(e)};
		w[1][i]=(node){cos(e),-sin(e)};
	}
}

inline void FFT(node *A,int f){
	for(int i=0;i<N;i++)if(rev[i]>i)swap(A[i],A[rev[i]]);
	for(int i=1;i<N;i<<=1)
	for(int j=0,t=N/(i<<1);j<N;j+=(i<<1))
	for(int k=l=0;k<i;k++,l+=t){
		node p=A[j+k+i]*w[f][l];
		node q=A[j+k];
		A[j+k]=q+p;
		A[j+k+i]=q-p;
	}
	if(f)for(int i=0;i<N;i++)A[i].x/=N;
}

void geta(int x,int fa){
	data[++data[0]]=d[x];
	Max=max(Max,d[x]);
	for(int i=last[x];i;i=next[i])if(way[i]!=fa&&!can[way[i]]){
		int s=way[i];
		d[s]=d[x]+1;
		geta(s,x);
	}
}

double calc(int x,int k){
	d[x]=k;data[0]=0;Max=0;geta(x,-1);
	for(N=1;N<=Max;N<<=1);N<<=1;
	for(int i=0;i<N;i++)a[i]=(node){0,0};
	for(int i=1;i<=data[0];i++)a[data[i]].x+=1.0;
	
	pre();FFT(a,0);FFT(b,0);
	for(int i=0;i<N;i++)a[i]=a[i]*a[i];
	FFT(a,1);
	double ans=0;
	for(int i=1;p[i]<=Max*2;i++)
	ans+=a[p[i]].x;
	return ans;
}

void getr(int x,int fa){
	size[x]=1;f[x]=0;
	for(int i=last[x];i;i=next[i])if(way[i]!=fa&&!can[way[i]]){
		int s=way[i];
		getr(s,x);
		size[x]+=size[s];
		if(size[s]>f[x])f[x]=size[s];
	}
	if(All-size[x]>f[x])f[x]=All-size[x];
	if(f[x]<f[root])root=x;
}

void work(int x){
	ans+=calc(x,0);can[x]=1;
	for(int i=last[x];i;i=next[i])
	if(!can[way[i]]){
		int s=way[i];
		ans-=calc(s,1);
		All=size[s];f[root=0]=n+1;
		getr(s,-1);
		work(root);
	}
}

void Pre(){
	for(i=2;i<=maxn;i++)if(!F[i]){
		p[++p[0]]=i;
		for(j=2*i;j<=maxn;j+=i)F[j]=1;
	}
}

int main(){
	scanf("%d",&n);
	for(i=1;i<n;i++){
		scanf("%d%d",&c,&e);
		make(c,e);
	}
	Pre();
	All=n;f[root=0]=n+1;
	getr(1,-1);
	work(root);
	double Ans=1.0*ans/n/(n-1);
	printf("%.9lf",Ans);
	return 0;
}

 

Category: BZOJ | Tags: | Read Count: 1885
Cooper Morwood 说:
2018年10月22日 20:07

Calculation of distance from one specific point to another one can save us from various kinds of efforts. Reviews are very helpful for us in hiring of best essay writing service that can provide qualified writers for our works.

AP SSC Evs Model Pap 说:
2022年9月11日 14:30

Every Telugu Medium, English Medium and Urdu Medium student of the State Board can download the AP 10th Class EVS Model Paper 2023 Pdf with answers for term-1 & term-2 exams of SA-1, SA-2 and other exams of the board. AP SSC Evs Model Paper Environmental Education is one of the most important subjects and it’s a part of Science. School Education Department and various leading private school teaching staff have designed and suggested the practice question paper for all Part-A, Part-B, Part-C, and Part-D questions of SA-1, SA-2, FA-1, FA-2, FA-3, FA-4 and Assignments. Advised to everyone can contact the class teacher to get important questions for all lessons and topics of EVS.

Alyssa 说:
2022年12月31日 23:21

In mathematics, the prime distance on a tree is the distance between two vertices of a tree that are connected by a path consisting entirely of CBD facts prime edges. The prime distance on a tree T is denoted pd(T). It is easy to see that the prime distance on a tree is always an odd number. Indeed, if the prime distance on a tree T is even, then there must be a path consisting of even number of edges between the two vertices, contradicting the fact that the prime distance is always a path consisting of prime edges.

boardmodelpaper.com 说:
2024年1月22日 15:05

The Board model paper" typically refers to a sample or model question paper that is designed by educational boards or institutions for various exams. These papers serve as practice material for students preparing for exams, providing them with an idea of the question format, difficulty level, and the type of content that may be covered in the actual examination. boardmodelpaper.com Model papers are usually created for specific subjects or courses. They cover a range of topics and chapters that students are expected to have studied during the academic term.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com