洛谷P3338 [ZJOI2014]力(FFT)

题意:洛谷P3338

分析:

题目给了我们这样一大坨的式子, F j = ∑ i < j q i q j ( i − j ) 2 − ∑ i > j q i q j ( i − j ) 2 F_j=\sum_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum_{i>j}\frac{q_iq_j}{(i-j)^2} Fj=i<j(ij)2qiqji>j(ij)2qiqj
然后让我们求 E j = F j q j E_j=\frac{F_j}{q_j} Ej=qjFj
首先我们化简一下这个式子,让 F i F_i Fi q i q_i qi得到 E j = ∑ i < j q i ( i − j ) 2 − ∑ i > j q i ( i − j ) 2 E_j=\sum_{i<j}\frac{q_i}{(i-j)^2}-\sum_{i>j}\frac{q_i}{(i-j)^2} Ej=i<j(ij)2qii>j(ij)2qi,即
E j = ∑ i = 0 j − 1 q i ( i − j ) 2 − ∑ i = j + 1 n q i ( i − j ) 2 E_j=\sum_{i=0}^{j-1}\frac{q_i}{(i-j)^2}-\sum_{i=j+1}^{n}\frac{q_i}{(i-j)^2} Ej=i=0j1(ij)2qii=j+1n(ij)2qi
这个式子暴力计算是 O ( n 2 ) O(n^2) O(n2)的,无法接受,考虑 F F T FFT FFT优化。

p ( i ) = 1 i 2 p(i)=\frac{1}{i^2} p(i)=i21,则上式化为 F j = ∑ i = 0 j − 1 q i ∗ p ( i − j ) − ∑ i = j + 1 n q i ∗ p ( i − j ) F_j=\sum_{i=0}^{j-1}q_i*p(i-j)-\sum_{i=j+1}^{n}q_i*p(i-j) Fj=i=0j1qip(ij)i=j+1nqip(ij)

q i ∗ q^*_i qi表示 q i q_i qi的逆置,则 ∑ i = j + 1 n q i ∗ p ( i − j ) \sum_{i=j+1}^{n}q_i*p(i-j) i=j+1nqip(ij)可以转化为 ∑ i = 0 j − 1 q i ∗ ∗ p ( i − j ) \sum_{i=0}^{j-1}q^*_i*p(i-j) i=0j1qip(ij),
逆置有疑问的看文章末尾我画的丑图。

最后因为 i − j i-j ij会是负数,而 p ( i ) p(i) p(i)是偶函数,所以最后式子转化为
E j = ∑ i = 0 j − 1 q i ∗ p ( j − i ) − ∑ i = 0 j − 1 q i ∗ ∗ p ( j − i ) E_j=\sum_{i=0}^{j-1}q_i*p(j-i)-\sum_{i=0}^{j-1}q^*_i*p(j-i) Ej=i=0j1qip(ji)i=0j1qip(ji)
等号两端都是卷积直接 F F T FFT FFT求即可

#include 
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int maxn  = 100000 + 5;
const int maxm  = 100 + 5;
const int inf   = 0x3f3f3f3f;
const LL  mod   = 1e9 + 7;//19260817
const double pi = acos(-1.0);

int n, L, len, rev[maxn << 2];

struct Complex{
	double x, y;
	Complex(double _x = 0, double _y = 0) : x(_x), y(_y){}
	Complex operator + (const Complex &a)const{
		return Complex(x + a.x, y + a.y);
	}
	Complex operator - (const Complex &a)const{
		return Complex(x - a.x, y - a.y);
	}
	Complex operator * (const Complex &a)const{
		return Complex(x * a.x - y * a.y, x * a.y + y * a.x);
	}
}a[maxn << 2], b[maxn << 2], c[maxn << 2];

void FFT(Complex a[], int len, int f){
	Complex x, y, w, wn;
	for(int i = 0; i < len; i++) if(i < rev[i]) swap(a[i], a[rev[i]]);
	for(int i = 1; i < len; i <<= 1){
		wn = Complex(cos(pi / i), f * sin(pi / i));
		for(int j = 0, p = i << 1; j < len; j += p){
			w = Complex(1, 0);
			for(int k = 0; k < i; k++, w = w * wn){
				x = a[j + k], y = w * a[j + k + i];
				a[j + k] = x + y, a[j + k + i] = x - y;
			}
		}
	}
	if(!~f) for(int i = 0; i <= len; i++) a[i].x /= 1.0 * len;
}

int main(){ 
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%lf", &a[i].x);
		b[n - i + 1].x = a[i].x;
		c[i].x = 1.0 / i / i;
	}
	for(len = 1; len <= n + n; len <<= 1) ++L;
	for(int i = 0; i < len; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (L - 1));
	FFT(a, len, 1);
	FFT(b, len, 1);
	FFT(c, len, 1);
	for(int i = 1; i <= len; i++){
		a[i] = a[i] * c[i];
		b[i] = b[i] * c[i];
	}
	FFT(a, len, -1);
	FFT(b, len, -1);
	for(int i = 1; i <= n; i++) printf("%.3lf\n", a[i].x - b[n - i + 1].x);
    return 0; 
}

洛谷P3338 [ZJOI2014]力(FFT)_第1张图片
F F T FFT FFT入门。

你可能感兴趣的:(FFT)