【传送门】

FFT第四题!

暑假的时候只会点分,然后合并是暴力合并的...水过去了...

其实两条路径长度的合并就是卷积的过程嘛,每次统计完路径就自卷积一下。

刚开始卷积固定了值域。T了。然后就不偷懒了,每次取最大权值乘二去找值域了。

#include <bits/stdc++.h>

const double pi = acos(-1.0);

struct Complex {
double r, i;
void clear() { r = i = 0.0; }
Complex(double r = , double i = ): r(r), i(i) {}
Complex operator + (const Complex &p) const { return Complex(r + p.r, i + p.i); }
Complex operator - (const Complex &p) const { return Complex(r - p.r, i - p.i); }
Complex operator * (const Complex &p) const { return Complex(r * p.r - i * p.i, r * p.i + i * p.r); }
}; void FFT(Complex *a, int n, int pd, int *r) {
for (int i = ; i < n; i++)
if (i < r[i])
std::swap(a[i], a[r[i]]);
for (int mid = ; mid < n; mid <<= ) {
Complex wn(cos(pi / mid), pd * sin(pi / mid));
for (int l = mid << , j = ; j < n; j += l) {
Complex w(1.0, 0.0);
for (int k = ; k < mid; k++, w = w * wn) {
Complex u = a[k + j], v = w * a[k + j + mid];
a[k + j] = u + v;
a[k + j + mid] = u - v;
}
}
}
if (pd < )
for (int i = ; i < n; i++)
a[i] = Complex(a[i].r / n, a[i].i / n);
} #define ll long long const int N = 2e5 + ;
int n, sz[N], maxsz[N], root, totsz;
std::vector<int> vec[N];
int prime[N], prin;
bool vis[N], is[N];
ll cnt[N], ccnt[N];
int dis[N], r[N];
Complex A[N];
int limit, l; void init() {
for (int i = ; i < N; i++) {
if (!is[i]) prime[++prin] = i;
for (int j = ; j <= prin && i * prime[j] < N; j++) {
is[i * prime[j]] = ;
if (i % prime[j] == ) break;
}
}
} inline bool chkmax(int &a, int b) { return a < b ? a = b, : ; } void getroot(int u, int fa) {
sz[u] = ; maxsz[u] = ;
for (int v : vec[u]) {
if (v == fa || vis[v]) continue;
getroot(v, u);
sz[u] += sz[v];
chkmax(maxsz[u], sz[v]);
}
chkmax(maxsz[u], totsz - sz[u]);
if (maxsz[u] < maxsz[root]) root = u;
} int f[N], tto, val; void getdis(int u, int fa) {
f[++tto] = dis[u];
val = std::max(val, f[tto]);
for (int v : vec[u]) {
if (vis[v] || v == fa) continue;
dis[v] = dis[u] + ;
getdis(v, u);
}
} void cal(int u, int d, int opt) {
tto = ;
dis[u] = d;
val = ;
getdis(u, );
for (int i = ; i <= tto; i++)
ccnt[f[i]]++;
limit = , l = ;
while (limit <= * val)
limit <<= , l++;
for (int i = ; i < limit; i++)
r[i] = r[i >> ] >> | ((i & ) << (l - ));
for (int i = ; i < limit; i++)
A[i] = Complex((double)ccnt[i], 0.0);
FFT(A, limit, , r);
for (int i = ; i < limit; i++)
A[i] = A[i] * A[i];
FFT(A, limit, -, r);
for (int i = ; i < limit; i++)
cnt[i] += opt * (ll)(A[i].r + 0.5);
for (int i = ; i <= tto; i++)
ccnt[f[i]]--;
} void solve(int u) {
vis[u] = ;
cal(u, , );
for (int v : vec[u]) {
if (vis[v]) continue;
cal(v, , -);
totsz = sz[v];
root = ;
getroot(v, );
solve(root);
}
} int main() {
init();
scanf("%d", &n);
for (int i = ; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
vec[u].push_back(v);
vec[v].push_back(u);
}
maxsz[root = ] = n;
totsz = n;
getroot(, );
solve(root);
ll ans = ;
for (int i = ; i <= prin; i++) {
ans += cnt[prime[i]];
}
ll sum = 1LL * n * (n - );
printf("%.7f\n", 1.0 * ans / sum);
return ;
}

Codechef Prime Distance On Tree的更多相关文章

  1. codechef Prime Distance On Tree(树分治+FFT)

    题目链接:http://www.codechef.com/problems/PRIMEDST/ 题意:给出一棵树,边长度都是1.每次任意取出两个点(u,v),他们之间的长度为素数的概率为多大? 树分治 ...

  2. CodeChef - PRIMEDST Prime Distance On Tree 树分治 + FFT

    Prime Distance On Tree Problem description. You are given a tree. If we select 2 distinct nodes unif ...

  3. 【CodeChef】Prime Distance On Tree

    vjudge 给定一棵边长都是\(1\)的树,求有多少条路径长度为质数 树上路径自然是点分治去搞,但是发现要求是长度为质数,总不能对每一个质数都判断一遍吧 自然是不行的,这个东西显然是一个卷积,我们合 ...

  4. 数论 - 素数的运用 --- poj 2689 : Prime Distance

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 D ...

  5. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  6. BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )

    树链剖分+可持久化线段树....这个一眼可以看出来, 因为可持久化所以写了标记永久化(否则就是区间修改的线段树的持久化..不会), 结果就写挂了, T得飞起...和管理员拿数据调后才发现= = 做法: ...

  7. UVA 10140 - Prime Distance(数论)

    10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...

  8. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  9. [POJ268] Prime Distance(素数筛)

    /* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...

随机推荐

  1. Manthan, Codefest 18 (rated, Div. 1 + Div. 2) E bfs + 离线处理

    https://codeforces.com/contest/1037/problem/E 题意 有n个人,m天,在第i天早上,x和y会成为朋友,每天晚上大家都要上车,假如一个人要上车那么他得有至少k ...

  2. 谈谈vue.js中methods watch和compute的区别和联系

    methods,watch和computed都是以函数为基础的,但各自却都不同: 1.watch和computed都是以Vue的依赖追踪机制为基础的,它们都试图处理这样一件事情:当某一个数据(称它为依 ...

  3. [MFC]_在vs2019中使用MFC快速构建简单windows窗口程序

    微软基础类库(英语: Classes,简称MFC)是微软公司提供的一个类库(class libraries),以C++类的形式封装了Windows API,并且包含一个应用程序框架,以减少应用程序开发 ...

  4. python每日学习2018/1/14(python之禅)

    The Zen of Python, by Tim Peters   Beautiful is better than ugly. Explicit is better than implicit. ...

  5. Could not find resource——mybatis 找不到映射器xml文件

    今天用IDEA写Mybatis的时候,测试报了如图所示的错,恶心死我了,后来解决了,总结一下,防止下回跳坑,当然,也是做一个分享,如果有朋友遇到这个错,希望有所帮助 Error parsing SQL ...

  6. ASP.NET MVC 实现简单的登录

    1.创建一个控制器   如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  7. Xamarin移动开发备忘

    vs2017下: 1.debug用于本地生成和调试,release用于发布.区别主要在于: 安卓项目的生成选项属性中,开发者模式release是不勾的,而且高级里的cpu不同(debug是x86,re ...

  8. C# - VS2019调用ZXing.NET实现条码、二维码和带有Logo的二维码生成

    前言 C# WinFrm程序调用ZXing.NET实现条码.二维码和带有Logo的二维码生成. ZXing.NET导入 GitHub开源库 ZXing.NET开源库githib下载地址:https:/ ...

  9. Microsoft.Office.Interop.Excel 读取 excel 中的 checkbox 和 radio

    using Excel = Microsoft.Office.Interop.Excel; Excel.Application excelapp = new Excel.Application(); ...

  10. Python requests库的使用(一)

    requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.python-requests.org/z ...