1. #include <algorithm>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <complex>
  7. #include <string>
  8. #include <vector>
  9. #include <cstdio>
  10. #include <cmath>
  11. //#include <ctime>
  12. #include <map>
  13. using namespace std;
  14.  
  15. // FFT on complex filed. (You can also use FFT on finite field.)
  16. complex <double> x[1<<20];
  17. complex <double> y[1<<20];
  18. complex <double> r[1<<20];
  19.  
  20. namespace Fourier
  21. {
  22. // n must be 2^k
  23. complex <double> t[1<<20];
  24. void FFT(complex <double> *array, int n, double pi = M_PI)
  25. {
  26. for(int step = n>>1; step >= 1; step >>= 1)
  27. {
  28. complex <double> w(cos(2 * pi * step / n), sin(2 * pi * step / n));
  29. for(int start = 0; start < step; start ++)
  30. {
  31. complex <double> factor(1, 0);
  32. int z = 0;
  33. for(int i = start; i < n; i += step << 1)
  34. {
  35. array[i+step] *= factor;
  36. array[i] += array[i+step];
  37. array[i+step] = array[i] - array[i+step] - array[i+step];
  38. factor *= w;
  39. t[z] = array[i];
  40. t[z+n/step/2] = array[i+step];
  41. z ++;
  42. }
  43. z = 0;
  44. for(int i = start; i < n; i += step)
  45. array[i] = t[z++];
  46. }
  47. }
  48. for(int i = 0; i < n; i++)
  49. array[i] *= complex <double> (1/sqrt(n), 0);
  50. }
  51.  
  52. void IFFT(complex <double> *array, int n)
  53. {
  54. FFT(array, n, -M_PI);
  55. }
  56.  
  57. void cyclic_convolution(complex <double> *A, complex <double> *B, complex <double> *result, int n)
  58. {
  59. FFT(A, n);
  60. FFT(B, n);
  61. for(int i = 0; i < n; i++)
  62. result[i] = A[i] * B[i];
  63. IFFT(result, n);
  64. for(int i = 0; i < n; i++)
  65. result[i] *= complex <double> (sqrt(n), 0);
  66. }
  67. }
  68.  
  69. int n;
  70. const int MAXN = 200000;
  71. const int MAXDEPTH = 20;
  72. vector <int> e[MAXN];
  73. int cntNodes[MAXN];
  74. int minValue, center;
  75.  
  76. vector <int> cache[MAXDEPTH][4];
  77. int cache_ans[MAXDEPTH][4][MAXN];
  78. int ans[MAXN];
  79. int temp1[MAXN];
  80. int temp2[MAXN];
  81. int temp3[MAXN];
  82. int temp[MAXN];
  83. int saveN[MAXDEPTH];
  84.  
  85. // find the minimal n such that 2**n > x
  86. int minimalPow2(int x)
  87. {
  88. int ret = 0;
  89. int t = 1;
  90. while(t < x)
  91. {
  92. ret ++;
  93. t = t + t;
  94. }
  95. return ret;
  96. }
  97.  
  98. // use dfs to find the center of a tree
  99. int dfs(int cur, int from, int d, int n, bool special = false, int depth = -1)
  100. {
  101. if(special)
  102. cache_ans[depth][0][d] ++;
  103. cntNodes[cur] = 1;
  104. int maxSubTree = 0, cnt = 0;
  105. for(int i = 0; i < e[cur].size(); i++)
  106. if(e[cur][i] != from)
  107. {
  108.  
  109. int t = dfs(e[cur][i], cur, d+1, n, special, depth);
  110. cntNodes[cur] += cntNodes[e[cur][i]];
  111. cnt += t;
  112. maxSubTree = max(maxSubTree, t);
  113. }
  114. maxSubTree = max(maxSubTree, n-1-cnt);
  115. if(n > 0 && maxSubTree < minValue)
  116. {
  117. center = cur;
  118. minValue = maxSubTree;
  119. }
  120. return cnt + 1;
  121. }
  122.  
  123. int totNodes;
  124.  
  125. int findCenter(int root, int depth)
  126. {
  127. int n = dfs(root, -1, 0, 0, true, depth);
  128. minValue = 100000000;
  129. totNodes = dfs(root, -1, 0, n, false, depth);
  130. dfs(center, -1, 0, n, false, depth);
  131. return center;
  132. }
  133.  
  134. // A[i]: number of nodes have a distance i from subtree A
  135. // B[i]: number of nodes have a distance i from subtree B
  136. // Update all pathes between subtree A and subtree B
  137. void calcPair(int *A, int *B, int N)
  138. {
  139. for(int i = 0; i < (1<<N); i++)
  140. {
  141. x[i] = complex <double> (A[i], 0);
  142. y[i] = complex <double> (B[i], 0);
  143. }
  144. Fourier :: cyclic_convolution(x, y, r, 1<<N);
  145.  
  146. for(int i = 1; i < (1<<N); i++)
  147. ans[i] += (long long)(r[i].real() + 0.5);
  148. }
  149.  
  150. void solve(int root, int depth, int reqN)
  151. {
  152. for(int i = 0; i < (1<<reqN); i++)
  153. {
  154. cache_ans[depth][0][i] = 0;
  155. cache_ans[depth][1][i] = 0;
  156. cache_ans[depth][2][i] = 0;
  157. cache_ans[depth][3][i] = 0;
  158. }
  159. int center = findCenter(root, depth);
  160. int totalNodes = cntNodes[center];
  161.  
  162. cache_ans[depth][0][0] = 1;
  163. if(totalNodes <= 2)
  164. {
  165. if(totalNodes == 2)
  166. {
  167. cache_ans[depth][0][1] = 1;
  168. ans[1] ++;
  169. }
  170. return;
  171. }
  172. cache[depth][0] = e[center];
  173. cache[depth][1].clear(), cache[depth][2].clear(), cache[depth][3].clear();
  174. int cnt = 1;
  175.  
  176. // The subtree will be grouped into 3 parts, each one has a size no more than N/2.
  177. for(int i = 0; i < e[center].size(); i++)
  178. {
  179. int before = cnt;
  180. int after = cnt + cntNodes[e[center][i]];
  181. cnt = after;
  182. if(after <= totalNodes / 2)
  183. cache[depth][1].push_back(e[center][i]);
  184. else if(before <= totalNodes / 2)
  185. cache[depth][2].push_back(e[center][i]);
  186. else
  187. cache[depth][3].push_back(e[center][i]);
  188. }
  189. int N = minimalPow2(totalNodes);
  190. N = max(N, 2);
  191.  
  192. e[center] = cache[depth][1];
  193. solve(center, depth + 1, N);
  194. for(int i = 1; i < (1<<N); i++)
  195. cache_ans[depth][1][i] = cache_ans[depth+1][0][i];
  196. e[center] = cache[depth][2];
  197. solve(center, depth + 1, N);
  198. for(int i = 1; i < (1<<N); i++)
  199. cache_ans[depth][2][i] = cache_ans[depth+1][0][i];
  200. e[center] = cache[depth][3];
  201. solve(center, depth + 1, N);
  202. for(int i = 1; i < (1<<N); i++)
  203. cache_ans[depth][3][i] = cache_ans[depth+1][0][i];
  204. e[center] = cache[depth][0];
  205.  
  206. for(int i = 0; i < (1<<reqN); i++)
  207. {
  208. temp1[i] = cache_ans[depth][1][i];
  209. temp2[i] = cache_ans[depth][2][i];
  210. temp3[i] = cache_ans[depth][3][i];
  211. }
  212. calcPair(temp1, temp2, reqN);
  213. calcPair(temp1, temp3, reqN);
  214. calcPair(temp2, temp3, reqN);
  215. }
  216.  
  217. bool isPrime[1000001];
  218.  
  219.  
  220. int MAIN()
  221. {
  222. memset(isPrime, true, sizeof(isPrime));
  223. isPrime[0] = isPrime[1] = false;
  224. for(int i = 2; i <= 1000000; i++)
  225. if(isPrime[i])
  226. for(int j = i+i; j <= 1000000; j+=i)
  227. isPrime[j] = false;
  228.  
  229. memset(ans, 0, sizeof(ans));
  230. memset(cache_ans, 0, sizeof(cache_ans));
  231. cin >> n;
  232. for(int i = 1; i < n; i++)
  233. {
  234. int a, b;
  235. cin >> a >> b;
  236. e[a].push_back(b);
  237. e[b].push_back(a);
  238. }
  239. solve(1, 0, minimalPow2(n));
  240. double total = 0, sum = 0;
  241. for(int i = 1; i <= n; i++)
  242. {
  243. total += ans[i];
  244. if(isPrime[i])
  245. sum += ans[i];
  246. }
  247. cout << sum / total << endl;
  248. return 0;
  249. }
  250.  
  251.  
  252. int main()
  253. {
  254. //srand((unsigned)time(NULL));
  255. #ifdef LOCAL_TEST
  256. freopen("in.txt", "r", stdin);
  257. freopen("out.txt", "w", stdout);
  258. #endif
  259. ios :: sync_with_stdio(false);
  260. cout << fixed << setprecision(16);
  261. return MAIN();
  262. }