Prime Distance On Tree(树上点分治+FFT)

描述

传送门:Codechef - PRIMEDST

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:

1
2
3
4
5
5
1 2
2 3
3 4
4 5

Output:

1
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.

思路

  • 树上路径统计问题的经典套路就是点分治,然后统计每个点的路径条数。
  • 对于每个结点的所有孩子计算到根的距离,用FFT求卷积,枚举素数统计答案即可。
  • 同时会计算来自同一儿子结点的子树中两个结点的路径,需要把这个情况的答案减去。
  • 具体实现看代码~

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <bits/stdc++.h>
using namespace std;

const int N = 1 << 16;
bool notprime[N] = {1, 1};
vector<int> prime;
void GetPrime()
{
for (int i = 2; i < N; i++)
if (!notprime[i] && i <= N / i)
for (int j = i * i; j < N; j += i)
notprime[j] = 1;
for (int i = 0; i < N; i++)
if (!notprime[i]) prime.push_back(i);
}

namespace fft
{
typedef double db;

struct cp
{
db x, y;
cp() { x = y = 0; }
cp(db x, db y) : x(x), y(y) {}
};

inline cp operator+(cp a, cp b) { return cp(a.x + b.x, a.y + b.y); }
inline cp operator-(cp a, cp b) { return cp(a.x - b.x, a.y - b.y); }
inline cp operator*(cp a, cp b) { return cp(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
inline cp conj(cp a) { return cp(a.x, -a.y); }

int base = 1;
vector<cp> roots = {{0, 0}, {1, 0}};
vector<int> rev = {0, 1};

const db PI = acosl(-1.0);

void ensure_base(int nbase)
{
if (nbase <= base) return;
rev.resize(static_cast<unsigned long>(1 << nbase));
for (int i = 0; i < (1 << nbase); i++)
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
roots.resize(static_cast<unsigned long>(1 << nbase));
while (base < nbase)
{
db angle = 2 * PI / (1 << (base + 1));
for (int i = 1 << (base - 1); i < (1 << base); i++)
{
roots[i << 1] = roots[i];
db angle_i = angle * (2 * i + 1 - (1 << base));
roots[(i << 1) + 1] = cp(cos(angle_i), sin(angle_i));
}
base++;
}
}

void fft(vector<cp>& a, int n = -1)
{
if (n == -1) n = a.size();
assert((n & (n - 1)) == 0);
int zeros = __builtin_ctz(n);
ensure_base(zeros);
int shift = base - zeros;
for (int i = 0; i < n; i++)
if (i < (rev[i] >> shift))
swap(a[i], a[rev[i] >> shift]);
for (int k = 1; k < n; k <<= 1)
for (int i = 0; i < n; i += 2 * k)
for (int j = 0; j < k; j++)
{
cp z = a[i + j + k] * roots[j + k];
a[i + j + k] = a[i + j] - z;
a[i + j] = a[i + j] + z;
}
}

vector<cp> fa, fb;

template <class T>
vector<T> multiply(vector<T>& a, vector<T>& b)
{
int need = a.size() + b.size() - 1;
int nbase = 0;
while ((1 << nbase) < need) nbase++;
ensure_base(nbase);
int sz = 1 << nbase;
if (sz > (int)fa.size())
fa.resize(static_cast<unsigned long>(sz));
for (int i = 0; i < sz; i++)
{
T x = (i < (int)a.size() ? a[i] : 0);
T y = (i < (int)b.size() ? b[i] : 0);
fa[i] = cp(x, y);
}
fft(fa, sz);
cp r(0, -0.25 / sz);
for (int i = 0; i <= (sz >> 1); i++)
{
int j = (sz - i) & (sz - 1);
cp z = (fa[j] * fa[j] - conj(fa[i] * fa[i])) * r;
if (i != j)
fa[j] = (fa[i] * fa[i] - conj(fa[j] * fa[j])) * r;
fa[i] = z;
}
fft(fa, sz);
vector<T> res(static_cast<unsigned long>(need));
for (int i = 0; i < need; i++) res[i] = fa[i].x + 0.5;
return res;
}
}; // namespace fft

namespace solution
{
vector<int> G[N];
bool vis[N];
int sz[N], mx[N], nn, rt;
void get_root(int u, int fa = 0)
{
sz[u] = 1, mx[u] = 0;
for (auto& v : G[u])
{
if (v == fa || vis[v]) continue;
get_root(v, u);
sz[u] += sz[v];
mx[u] = max(mx[u], sz[v]);
}
mx[u] = max(mx[u], nn - sz[u]);
if (mx[u] < mx[rt]) rt = u;
}

int dep[N], fa[N];
long long gao(int u, int d)
{
dep[u] = d, fa[u] = 0;
queue<int> q;
q.push(u);
vector<int> dis;
while (!q.empty())
{
int u = q.front();
q.pop();
dis.push_back(dep[u]);
for (auto& v : G[u])
{
if (v == fa[u] || vis[v]) continue;
fa[v] = u, dep[v] = dep[u] + 1;
q.push(v);
}
}
int mx = dis.back();
vector<long long> a(mx + 1);
for (auto& t : dis) ++a[t];
a = fft::multiply(a, a);
for (auto& t : dis) --a[t << 1];
for (auto& t : a) t >>= 1;
long long ret = 0;
for (auto& p : prime)
{
if (p > mx * 2) break;
ret += a[p];
}
return ret;
}

void solve(int u, long long& ans)
{
ans += gao(u, 0);
vis[u] = true;
for (auto& v : G[u])
{
if (vis[v]) continue;
ans -= gao(v, 1);
mx[rt = 0] = nn = sz[v];
get_root(v);
solve(rt, ans);
}
}

void solve()
{
int n;
scanf("%d", &n);
for (int i = 1, u, v; i < n; i++)
{
scanf("%d%d", &u, &v);
G[u].push_back(v), G[v].push_back(u);
}
mx[rt = 0] = nn = n;
long long ans = 0;
get_root(1);
solve(rt, ans);
printf("%.10f\n", ans * 2.0 / n / (n - 1));
}
}; // namespace solution

int main()
{
GetPrime();
solution::solve();
return 0;
}
捐助作者
0%