P3723 [AH2017/HNOI2017]礼物

给两个序列$x,y$。求可以移动$y$,即向右移动,$i\rightarrow i+1,n\rightarrow 1$。

求最小值$\sum(x_i+c-y_i)^2$

无脑展开

一个定值,一个二次函数,一个卷积。

$\sum x_{i+k} y_i=h_{n+k+1}(x)=\sum x_{i+k} fy_{n-i+1}$,$fy$为$y$翻转。

由于项数比较小,$FFT$即可

代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;

const double pi = acos(-1.0);

int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}

struct Complex
{
double x, y;
Complex(double _x = 0.0, double _y = 0.0)
{
x = _x;
y = _y;
}

Complex operator-(const Complex &b) const
{
return Complex(x - b.x, y - b.y);
}

Complex operator+(const Complex &b) const
{
return Complex(x + b.x, y + b.y);
}

Complex operator*(const Complex &b) const
{
return Complex(x * b.x - y * b.y, x * b.y + y * b.x);
}
};
int rev[N];
void FFT(Complex *A, int n, int inv)
{
for (int i = 0; i < n; i++)
if (i < rev[i])
swap(A[i], A[rev[i]]);
for (int l = 1; l < n; l <<= 1)
{
Complex temp(cos(pi / l), inv * sin(pi / l));
for (int i = 0; i < n; i += (l << 1))
{
Complex omega(1, 0);
for (int j = 0; j < l; j++, omega = omega * temp)
{
Complex x = A[i + j], y = omega * A[i + j + l];
A[i + j] = x + y;
A[i + j + l] = x - y;
}
}
}

if (inv == -1)
for (int i = 0; i < n; i++)
A[i].x /= n;
}

void FFTX(Complex *a, int n, Complex *b, int m, Complex *ans)
{
int ML = 1, bit = 0;
while (ML < n + m)
ML <<= 1, bit++;
for (int i = 0; i < ML; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1));
FFT(a, ML, 1);
FFT(b, ML, 1);
for (int i = 0; i < ML; i++)
ans[i] = a[i] * b[i];
FFT(ans, ML, -1);
}
Complex a[N], b[N], c[N];
ll ans;
ll suma, sumb;
int main()
{
int n = read(), m = read();
for (int i = 1; i <= n; i++)
a[i].x = read(), suma += a[i].x, ans += a[i].x * a[i].x;
for (int i = 1; i <= n; i++)
b[i].x = read(), sumb += b[i].x, ans += b[i].x * b[i].x;
for (int i = 1; i <= n; i++)
b[i + n] = b[i];
// if (-(suma - sumb) < 0)
// ans += 0;
// else
// {
// ll cc = -(suma - sumb) / n;

// ans += min(n * cc * cc + 2 * cc * (suma - sumb), n * (cc + 1) * (cc + 1) + 2 * (cc + 1) * (suma - sumb));
// }
int t = sumb - suma;
int c1 = floor(t * 1.0 / n), c2 = ceil(t * 1.0 / n);
ans += min(n * c1 * c1 - 2 * c1 * t, n * c2 * c2 - 2 * c2 * t);
reverse(a + 1, a + 1 + n);
// for (int i = 1; i <= 2 * n; i++)
// cout << a[i].x << " ";
// cout << endl;
// for (int i = 1; i <= 2 * n; i++)
// cout << b[i].x << " ";
// cout << endl;

FFTX(a, 2 * n, b, 2 * n, c);

int mx = 0;
for (int i = 0; i <= n; i++)
mx = max(int(c[n + i + 1].x + 0.5), mx);
ans -= 2 * mx;
printf("%lld\n", ans);
}