题目:https://www.acwing.com/problem/content/2243/
思路:
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
using namespace std;
#define LL long long
const int N = 4e5 + 10;
const LL mod = 998244353;
const double PI = acos(-1);
struct Complex
{
double x, y;
Complex operator +(const Complex& t) const
{
return { x + t.x,y + t.y };
}
Complex operator -(const Complex& t) const
{
return { x - t.x,y -t.y };
}
Complex operator *(const Complex& t) const
{
return { x*t.x - y*t.y,x*t.y+y*t.x};
}
}a[N], b[N];
int n, m;
LL ans1, ans2;
int tot, bit;
int rel[N];
LL gg(int c,int bb)
{
return (LL)n * c * c + 2 * c * bb;
}
void fft(Complex a[], int op)
{
for (int i = 0; i < tot; i++)
if (i < rel[i])swap(a[i], a[rel[i]]);
for (int m = 2; m <= tot; m *= 2)
{
Complex w1 ={ cos(2 * PI / m),op * sin(2 * PI / m) };
for (int i = 0; i < tot; i+=m)
{
Complex wk = { 1,0 };
for (int j = 0; j < m / 2; j++)
{
Complex x = a[i + j], y = a[i + j + m / 2]*wk;
a[i + j] = x + y;
a[i + j + m / 2] = x - y;
wk = w1 * wk;
}
}
}
}
int main() {
cin >> n >> m;
LL bb = 0;
for (int i = 0; i <n; i++)
{
cin >> a[n - 1 - i].x;
ans1 += (LL)a[n - 1 - i].x * a[n - 1 - i].x;
bb +=(LL)a[n - 1 - i].x;
}
for (int i = 0; i < n; i++)
{
cin >> b[i].x;
ans1 += b[i].x* b[i].x;
bb -= b[i].x;
}
for (int i = 0; i <= n - 1; i++)
a[i + n] = a[i];
int cc = -bb / n;
ans1 += min(gg(cc,bb), min(gg(cc+1,bb), gg(cc-1,bb)));
n--;
while ((1 << bit) < 3 * n + 1) bit++;
tot = 1 << bit;
for (int i = 0; i < tot; i++) rel[i] = rel[i / 2] / 2 + (i & 1 ? tot / 2 : 0);
fft(a, 1);
fft(b, 1);
for (int i = 0; i < tot; i++) a[i] = a[i] * b[i];
fft(a, -1);
for (int i = n - 1; i <= 2 * (n - 1); i++)
{
ans2 = max((int)ans2, int(a[i].x / tot + 0.5));
}
ans1 = ans1 - 2 * ans2;
cout << ans1 << endl;
return 0;
}