CodeChef PrimeDST【点分治】【FFT】

原创 2015年07月09日 08:57:32
/* I will wait for you */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <string>
#define make(a,b) make_pair(a,b)
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef map<int, int> mii;

const int maxn = 200010;
const int maxm = 1010;
const int maxs = 26;
const int inf = 0x3f3f3f3f;
const int P = 1000000007;
const double error = 1e-9;
const double Pi = 3.1415926535897932;

inline ll read()
{
	ll x = 0, f = 1; char ch = getchar();
	while (ch > '9' || ch < '0' )
		f = (ch == '-' ? -1 : 1), ch = getchar();
	while (ch <= '9' && ch >= '0')
		x = x * 10 + ch - '0', ch = getchar();
	return f * x;
}

struct complex
{
	double re, im;
} _x[maxn], w[2][maxn];

complex operator + (complex a, complex b)
{
	complex c;
	c.re = a.re + b.re;
	c.im = a.im + b.im;
	return c;
}

complex operator - (complex a, complex b)
{
	complex c;
	c.re = a.re - b.re;
	c.im = a.im - b.im;
	return c;
}

complex operator * (complex a, complex b)
{
	complex c;
	c.re = a.re * b.re - a.im * b.im;
	c.im = a.re * b.im + a.im * b.re;
	return c;
}

struct edge
{
	int v, next;
} e[maxn];

int n, root, sum, _maxdeep, maxdeep, head[maxn], 
    pri[maxn], _max[maxn], size[maxn], cnt, g[maxn], 
    f[maxn], deep[maxn], del[maxn], rev[maxn];
ll ans;

void insert(int u, int v)
{
	e[cnt] = (edge) {v, head[u]}, head[u] = cnt++;
}	

void _deep(int u, int p)
{
	g[deep[u]] += 1, maxdeep = max(maxdeep, deep[u]);
	for (int i = head[u]; ~i; i = e[i].next) {
		int v = e[i].v;
		if (v != p && !del[v])
			deep[v] = deep[u] + 1, _deep(v, u);
	}
}

void _size(int u, int p)
{
	size[u] = 1;
	for (int i = head[u]; ~i; i = e[i].next) {
		int v = e[i].v;
		if (v != p && !del[v])
			_size(v, u), size[u] += size[v];
	}
}

void _find(int u, int p)
{
	size[u] = 1, _max[u] = 0;
	for (int i = head[u]; ~i; i = e[i].next) {
		int v = e[i].v;
		if (v != p && !del[v]) {
			_find(v, u), size[u] += size[v];
			_max[u] = max(_max[u], size[v]);
		}
	}
	_max[u] = max(_max[u], sum - size[u]);
	if (_max[u] < _max[root]) root = u;
}

void FFT(complex *a, int n, int f)
{
	for (int i = 0; i < n; i++) {
		rev[i] = 0;
		for (int j = i, k = 1; k < n; k <<= 1, j >>= 1)
			(rev[i] <<= 1) |= (j & 1);
		if (rev[i] > i) swap(a[i], a[rev[i]]);
	}

	for (int i = 0; i < n; i++) {
		w[0][i].re = cos(2 * Pi * i / n);
		w[0][i].im = sin(2 * Pi * i / n);
		w[1][i].re = cos(2 * Pi * i / n);
		w[1][i].im = -sin(2 * Pi * i / n);
	}

	for (int i = 1; i < n; i <<= 1)
		for (int j = 0, l = n / (i << 1); j < n; j += (i << 1))
			for (int k = 0, t = 0; k < i; k += 1, t += l) {
				complex x = a[j + k], y = w[f][t] * a[i + j + k];
				a[j + k] = x + y, a[i + j + k] = x - y;
			}
	for (int i = 0; f && i < n; i++) a[i].re /= n;
}

void _solve(int *a, int n, int f)
{
	int len = 1; 
	while (1 << len < n << 1) len += 1;

	for (int i = 0; i < 1 << len; i++)
		_x[i].re = _x[i].im = 0;
	for (int i = 0; i < n; i++)
		_x[i].re = a[i];

	FFT(_x, 1 << len, 0);
	for (int i = 0; i < 1 << len; i++)
		_x[i] = _x[i] * _x[i];
	FFT(_x, 1 << len, 1);

	for (int i = 0; i < 1 << len; i++) {
		if (!pri[i] && f == 1)
			ans += (ll) (_x[i].re + 0.5);
		if (!pri[i] && f == -1)
			ans -= (ll) (_x[i].re + 0.5);
	}
}

void solve(int u)
{
	del[u] = 1, _size(u, 0);
	f[0] = 1, _maxdeep = 0;
	for (int i = 1; i <= size[u]; i++)
		f[i] = 0;

	for (int i = head[u]; ~i; i = e[i].next) {
		int v = e[i].v;
		if (!del[v]) {
			maxdeep = 0;
			for (int i = 0; i <= size[v]; i++)
				g[i] = 0;

			deep[v] = 1, _deep(v, u);
			_solve(g, maxdeep + 1, -1);

			_maxdeep = max(_maxdeep, maxdeep);
			for (int i = 0; i <= size[v]; i++)
				f[i] += g[i];
		}
	}
	_solve(f, _maxdeep + 1, 1);
	
	for (int i = head[u]; ~i; i = e[i].next) {
		int v = e[i].v;
		if (!del[v]) {
			sum = size[v], root = 0;
			_find(v, u), solve(root);
		}
	}

}

void init()
{
	pri[0] = pri[1] = 1;
	for (int i = 2; i < n; i++)
		if (!pri[i]) 
			for (int j = 2 * i; j < n; j += i)
				pri[j] = 1;
}

int main()
{
	n = read(), init();

	memset(head, -1, sizeof head);	
	for (int i = 1; i < n; i++) {
		int u = read(), v = read();
		insert(u, v), insert(v, u);
	}

	sum = _max[0] = n, root = 0;
	_find(1, 0), solve(root);

	printf("%.6f\n", 1.0 * ans / n / (n - 1));
	
	return 0;
}

HDU6195 cable cable cable 签到

题目链接:点击打开链接 水题,凑数。 AC代码: /* 2017年9月10日20:54:06 HDU 6195 签到 */ #include #include #include #in...

PAT (Advanced Level) Practise 1131 Subway Map (30)

1131. Subway Map (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者...

Codechef Prime Distance On Tree(点分治+FFT)

题外话最近做题发现自己非常SB,总是查一个SB错误查N久,简直绝望啊。。。弱逼为何而战这次是忘记加long long查了N久。。蛋碎无比不过好歹是又做出一道cc hard的题了呢,感人肺腑Descri...

CodeChef COUNTARI Arithmetic Progressions FFT + 分块

题目大意: 就是现在给出一个数列A[1~n], 每个数都是不超过30000的正整数, 现在求有多少个三元组(i, j, k)满足 1 大致思路: 这个题刚开始想到判断A[i] + A[k] ==...

Codechef Arithmetic Progressions ,分块FFT

题解网上到处都是,就不说了。 一开始块内暴力时忘了把桶清零,并且没有随下标移动更新桶内的值 然后FFT部分我一开始是这么写的ans+=(ll)(h[a[j]...
  • zxin__
  • zxin__
  • 2017年07月05日 15:55
  • 148

Bzoj3509:[CodeChef] COUNTARI:分块+FFT

题目链接:3509:[CodeChef] COUNTARI 题目要求求出(i,j,k)的对数满足i 式子变形得到a[j]*2=a[i]+a[k]; 考虑对于每个a[j]求出左边的i和右边的k 发现这个...

bzoj 3509: [CodeChef] COUNTARI (FFT+分块)

题目描述传送门题目大意:给定一个长度为N的数组A[],求有多少对i, j, k(1

codechef Prime Distance On Tree FFT

先树分治,对于每个点为根处理一下,然后对这个点的子树构成的多项式平方一下求一下素数个数即可。 #include #include #include #include #include #includ...

解题报告:Prime Distance On Tree 点分治 + FFT

题意: 给定一个树(结点数,问树上任意选两点,它们之间的点距为素数的概率。 思路: 很容易想到点分治统计答案,然后只需要统计经过根结点的,这个步骤很容易想到两两儿子之间进行fft,很明显会超时,那...

[n点无向图个数 分治FFT || 多项式求逆] BZOJ 3456 城市规划

然后就分治FFT了 #include #include #include using namespace std; typedef long long ll; inline char nc(){...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CodeChef PrimeDST【点分治】【FFT】
举报原因:
原因补充:

(最多只允许输入30个字)