AHOI/HNOI 2017礼物

题目链接
首先,设增加量为 x x x,旋转以后数列 a , b a,b a,b那么费用为
∑ i = 1 n ( a i − b i + x ) 2 \sum_{i=1}^n(a_i-b_i+x)^2 i=1n(aibi+x)2
拆开得到
( a i − b i + x ) 2 = a i 2 + b i 2 + x 2 + 2 a i x − 2 a i b i − 2 b i x (a_i-b_i+x)^2=a_i^2+b_i^2+x^2+2a_ix-2a_ib_i-2b_ix (aibi+x)2=ai2+bi2+x2+2aix2aibi2bix
原式变成了
∑ i = 1 n a i 2 + ∑ i = 1 n b i 2 + n x 2 + 2 x ( ∑ i = 1 n a i − ∑ i = 1 n b i ) − 2 ∑ i = 1 n a i b i \sum_{i=1}^na_i^2+\sum_{i=1}^nb_i^2+nx^2+2x(\sum_{i=1}^na_i-\sum_{i=1}^nb_i)-2\sum_{i=1}^na_ib_i i=1nai2+i=1nbi2+nx2+2x(i=1naii=1nbi)2i=1naibi
我们令 ∑ i = 1 n a i 2 = a a ; ∑ i = 1 n b i 2 = b b ; ∑ i = 1 n a i = s u m a ; ∑ i = 1 n b i = s u m b \sum_{i=1}^na_i^2=aa;\sum_{i=1}^nb_i^2=bb;\sum_{i=1}^na_i=suma;\sum_{i=1}^nb_i=sumb i=1nai2=aa;i=1nbi2=bb;i=1nai=suma;i=1nbi=sumb为常数
所以可以先预处理每次转动后的 ∑ i = 1 n a i b i \sum_{i=1}^na_ib_i i=1naibi,再枚举x取最小值
至于每次转动后最小值可以用 f [ i ] f[i] f[i]表示转动 i i i次的 ∑ i = 1 n a i b i \sum_{i=1}^na_ib_i i=1naibi
所以 f [ i ] = ∑ j = 1 n a [ j ] ∗ b [ j + i ] f[i]=\sum_{j=1}^na[j]*b[j+i] f[i]=j=1na[j]b[j+i]发现并不是卷积的形式
稍作调整 X [ i ] = X [ i + N ] = a [ i ] , Y [ i ] = b [ N − i + 1 ] , F [ N + i ] = f [ i ] X[i]=X[i+N]=a[i],Y[i]=b[N-i+1],F[N+i]=f[i] X[i]=X[i+N]=a[i],Y[i]=b[Ni+1],F[N+i]=f[i]这个就可以卷积了。
代码

#include 
#include 
#include 
#include 
using namespace std;

#define LL long long

const int N = 4e5 + 5;
int n, m, suma, sumb, aa, bb;
int res = 0, a[N], b[N];
int lim = 1, l, R[N];
LL ans = 1e15;
const double Pi = acos(-1.0);
struct Complex {
    double x, y;
    Complex (double xx = 0, double yy = 0) {
        x = xx, y = yy;
    }
}A[N], B[N];

Complex operator * (Complex A, Complex B) {
    return Complex(A.x * B.x - A.y * B.y, A.x * B.y + A.y * B.x);
}

Complex operator + (Complex A, Complex B) {
    return Complex(A.x + B.x, A.y + B.y);
}

Complex operator - (Complex A, Complex  B) {
    return Complex(A.x - B.x, A.y - B.y);
}

void FFT(Complex *C, double flag) {
    for(int i = 0; i < lim; ++i)
        if(i < R[i]) swap(C[i], C[R[i]]);
    for(int j = 1; j < lim; j <<= 1) {
        Complex T(cos(Pi / j), flag * sin(Pi / j));
        for(int k = 0; k < lim; k += (j << 1)) {
            Complex t(1, 0);
            for(int l = 0; l < j; ++l, t = t * T) {
                Complex Nx = C[k + l], Ny = t * C[k + j + l];
                C[k + l] = Nx + Ny;
                C[k + j + l] = Nx - Ny;
            }
        }
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]), suma = suma + a[i] * a[i], aa += a[i];
    for(int i = 1; i <= n; ++i) scanf("%d", &b[i]), sumb = sumb + b[i] * b[i], bb += b[i];
    for(int i = 1; i <= n; ++i) {
        A[i].x = A[i + n].x = a[i];
        B[i].x = b[n - i + 1];
    }
    
    while(lim <= (n * 3)) lim <<= 1, l++;
    for(int i = 0; i < lim; ++i) R[i] = ((R[i >> 1] >> 1) | ((i & 1) << (l - 1)));
    FFT(A, 1); FFT(B, 1);
    for(int i = 0; i <= lim; ++i) A[i] = A[i] * B[i];
    FFT(A, -1);
    for(int i = 0; i <= lim; ++i) A[i].x = (LL)(A[i].x / lim + 0.5);

    for(int i = 1; i <= n; ++i)
        for(int j = -m; j <= m; ++j) {
            if(j * j * n + 2LL * j * (aa - bb) - 2LL * A[i + n].x < ans)
                ans = j * j * n + 2LL * j * (aa - bb) - 2LL * A[i + n].x;
        }
    printf("%lld\n", ans + suma + sumb);
    return 0;
}

总结

有些时候多项式乘积往往与FFT有关,哪怕形式和卷积不同,但仍然可以通过变形来处理
f [ i ] = ∑ j = 1 n a [ j ] ∗ b [ j + i ] f[i]=\sum_{j=1}^na[j]*b[j+i] f[i]=j=1na[j]b[j+i]
X [ i ] = X [ i + N ] = a [ i ] , Y [ i ] = b [ N − i + 1 ] , F [ N + i ] = f [ i ] X[i]=X[i+N]=a[i],Y[i]=b[N-i+1],F[N+i]=f[i] X[i]=X[i+N]=a[i],Y[i]=b[Ni+1],F[N+i]=f[i]

你可能感兴趣的:(多项式)