luogu3723 [AH2017/HNOI2017] Gift [NTT]

topic

My roommate recently fell in love with a cute little girl. It was her birthday soon, he decided to buy a pair of lovers bracelets, one for himself and one
for her. There are n decorations on each bracelet, and each decoration has a certain brightness. But the day before her birthday, my roommate suddenly
found out that he seemed to be holding the wrong bracelet and ran out of time to replace it!
He can only use a special method to increase the brightness of all decorations in one of the bracelets by the same natural number c (ie, a non-negative integer). And since this bracelet is a circle, it can be rotated at any angle,
but since the direction of the decoration above is fixed, the bracelet cannot be turned over. It is necessary to minimize the difference between the two bracelets after brightness transformation and rotation
. After the two bracelets are rotated and the decorations are aligned, number the decorations 1, 2,...,n counterclockwise from the aligned position,
where n is the number of decorations per bracelet, and the first The brightness of the decoration at position i of one bracelet is xi, the brightness of the decoration at position i of the second bracelet
is yi, and the difference between the two bracelets is (see input and output samples and sample explanations) : \sum_{i=1}^{n}(x_i-y_i)^2 Please help him
calculate and adjust (brightness transformation and rotation) to make the difference between the two bracelets the smallest, this minimum value How much is it?

input format

The first line of the input data has two numbers n and m, which represent the number of decorations of each bracelet is n, and the initial brightness of each decoration is less than or equal to m.
The next two lines, each with n numbers, represent the brightness of the decorations in the counterclockwise direction from a certain position on the first bracelet and the second bracelet respectively.
1≤n≤50000, 1≤m≤100, 1≤ai≤m

output format

Output a number that represents the minimum difference between the two bracelets.
Note that after modifying the bracelet, the brightness of the decoration can be greater than m.

input sample

5 6

1 2 3 4 5

6 3 3 4 5

Sample output

1

hint

【Example explanation】

The brightness of the first bracelet needs to be increased by 1, and the brightness of the first bracelet becomes: 2 3 4 5 6 Rotate the second bracelet. For this example, the

The brightness of the second bracelet 6 3 3 4 5 moves to the left circularly 2017-04-15 Page 6, a total of 6 pages, so that the final brightness of the second bracelet is

: 3 3 4 5 6. At this time, the brightness difference between the two bracelets is 1.

answer

We set \(d_i = x_i + y_i\)
then the difference value is:
\[ \begin{aligned} &=\sum\limits_{i = 1}^{n} (d_i + r)^2 \\ &=\ sum\limits_{i = 1}^{n}(d_i^2 + r^2 + 2d_ir) \\ &=\sum\limits_{i = 1}^{n} d_i^2 + nr^2 + 2r\ The sum\limits_{i = 1}^{n} d_i \\ \end{aligned} \]
can be directly determined, it is the minimum point of the quadratic function,
we only need to minimize
\[\sum\limits_{i = 1} ^{n} d_i^2\]
If we double the length of \(y\) , for a dislocation relationship, the difference between \(x_i and y_i\) is a fixed value.
We actually find
\[min\{ \sum\limits_ {j - i = k} x_i * y_j\} \qquad k \in [0,n - 1]\]
We make \(t_i = x_{n - i + 1}\)
then find
\[min\{ \sum\limits_{j + i= n + 1 + k} t_i * y_j\} \qquad k \in [0,n - 1]\]To
put it bluntly, it is to flip \(x\)

Then NTT can

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 400005,maxm = 100005,INF = 1000000000;
inline int read(){
    int out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    return out * flag;
}
const int G = 3,P = 998244353;
LL A[maxn],B[maxn],L,R[maxn],n,m;
LL qpow(LL a,LL b){
    LL ans = 1;
    for (; b; b >>= 1,a = a * a % P)
        if (b & 1) ans = ans * a % P;
    return ans;
}
void NTT(LL* a,int f){
    for (int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
    for (int i = 1; i < n; i <<= 1){
        LL gn = qpow(G,(P - 1) / (i << 1));
        for (int j = 0; j < n; j += (i << 1)){
            LL g = 1,x,y;
            for (int k = 0; k < i; k++,g = g * gn % P){
                x = a[j + k]; y = g * a[j + k + i] % P;
                a[j + k] = (x + y) % P; a[j + k + i] = (x - y + P) % P;
            }
        }
    }
    if (f == 1) return;
    int nv = qpow(n,P - 2); reverse(a + 1,a + n);
    for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
}
LL N,a[maxn],b[maxn],sum;
LL ans = 0;
int main(){
    N = read(); read();
    REP(i,N) sum += (a[i] = read());
    REP(i,N) sum -= (b[i] = read());
    LL r = -sum / N;
    ans = INF;
    ans = min(ans,N * (r - 1) * (r - 1) + 2 * sum * (r - 1));
    ans = min(ans,N * r * r + 2 * sum * r);
    ans = min(ans,N * (r + 1) * (r + 1) + 2 * sum * (r + 1));
    REP(i,N) ans += a[i] * a[i] + b[i] * b[i];
    REP(i,N) A[i] = a[N - i + 1],B[i] = B[N + i] = b[i];
    m = 3 * N; L = 0;
    for (n = 1; n <= m; n <<= 1) L++;
    for (int i = 0; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
    NTT(A,1); NTT(B,1);
    for (int i = 0; i < n; i++) A[i] = A[i] * B[i] % P;
    NTT(A,-1);
    LL tmp = 0;
    for (int i = N + 1; i <= N + N; i++) tmp = max(tmp,A[i]);
    ans -= 2 * tmp;
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325210131&siteId=291194637