P3723 [AH2017/HNOI2017]禮物(FFT)

_lifehappy_發表於2020-10-13

P3723 [AH2017/HNOI2017]禮物

式子化簡

∑ i = 1 n ( x i − y j ) 2 \sum_{i = 1} ^{n} (x_i- y_j) ^2\\ i=1n(xiyj)2

我們對第一個手環 + c +c +c,相當於 ( x i + c − y i ) 2 (x_i + c - y_i) ^ 2 (xi+cyi)2,對第二個手環 + c +c +c相當於 ( x i − y i − c ) 2 (x_i - y_i - c) ^2 (xiyic)2

也就是 ( x i − y j + c ) 2 (x_i - y_j + c) ^2 (xiyj+c)2, c ∈ [ − m , m ] c \in[-m, m] c[m,m]

接下來我們就是要求
∑ i = 1 n ( x i − y i + c ) 2 ∑ i = 1 n ( x i 2 + y i 2 + 2 c ( x i − y i ) + c 2 − 2 x i y i ) \sum_{i = 1} ^{n} (x_i - y_i + c) ^2\\ \sum_{i = 1} ^{n} (x_i ^ 2 + y_i ^ 2 + 2c(x_i - y_i) + c ^ 2 - 2x_iy_i)\\ i=1n(xiyi+c)2i=1n(xi2+yi2+2c(xiyi)+c22xiyi)
除去最後一項,前面的都是定值,無非就是列舉 c c c,check一下 ∑ i = 1 n 2 c ( x i − y i ) + c 2 \sum\limits_{i = 1} ^{n} 2c(x_i - y_i) + c ^ 2 i=1n2c(xiyi)+c2的最小值嘛,

但是不難發現這是一個開口向上得二次函式,所以最小值可以直接通過對稱軸 O ( 1 ) O(1) O(1)求得,

所以我們要讓 ∑ i = 1 n x i y i \sum\limits_{i = 1} ^{n}x_i y_i i=1nxiyi最大,也就是 ∑ i = 1 n x i y i + t \sum\limits_{i = 1} ^{n}x_iy_{i + t} i=1nxiyi+t最大

我們翻轉 x x x,得到 ∑ i = 1 n x n − i + 1 y i + t \sum\limits_{i = 1} ^{n} x_{n - i + 1} y_{i + t} i=1nxni+1yi+t最大,這就是一個多項式卷積的形式了

程式碼

/*
  Author : lifehappy
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const double pi = acos(-1.0);

const int N = 1e6 + 10;

struct Complex {
    double r, i;

    Complex(double _r = 0, double _i = 0) : r(_r), i(_i) {}

}a[N], b[N];

Complex operator + (const Complex & a, const Complex & b) {
    return Complex(a.r + b.r, a.i + b.i);
}

Complex operator - (const Complex & a, const Complex & b) {
    return Complex(a.r - b.r, a.i - b.i);
}

Complex operator * (const Complex & a, const Complex & b) {
    return Complex(a.r * b.r - a.i * b.i, a.r * b.i + a.i * b.r);
}

int r[N];

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

void get_r(int lim) {
    for(int i = 0; i < lim; ++i) {
        r[i] = (i & 1) * (lim >> 1) + (r[i >> 1] >> 1);
    }
}

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    ll ans = 0, res = 0;
    int n, m, lim = 1;
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++) {
    	scanf("%lf", &a[n - i + 1].r); 
	}
	for(int i = 1; i <= n; i++) {
		scanf("%lf", &b[i].r);
		b[i + n]= b[i];
	}
	for(int i = 1; i <= n; i++) {
		ans += a[i].r * a[i].r + b[i].r * b[i].r;
		res += a[i].r - b[i].r;
	}
	ll c1 = floor(res * 1.0 / n), c2 = ceil(res * 1.0 / n);
	ans += min(n * c1 * c1 - 2 * c1 * res, n * c2 * c2 - 2 * c2 * res);
	n <<= 2;
	while(lim < n) lim <<= 1;
	n >>= 2;
	get_r(lim);
	fft(a, lim, 1);
	fft(b, lim, 1);
	for(int i = 0; i < lim; i++) {
		a[i] = a[i] * b[i];
	}
	fft(a, lim, -1);
	res = 0;
	for(int i = 1; i <= n; i++) {
		res = max(res, ll(a[i + n].r + 0.5));
	}
	printf("%lld\n", ans - 2 * res);
	return 0;
}

相關文章