Gifts: polynomial

Description:

My roommate recently in love with a pretty little girl. Soon to her birthday, he decided to buy a couple bracelet, a left to their own, gave her. Each hand ring each n th decorations, decoration and each has a certain brightness.

But the day before her birthday, my roommate suddenly noticed that he seemed to take it in the wrong bracelet, and has no time to replace it!

He can use a special method, a bracelet in which the brightness of all the garnish increase a same integer X (may be negative).

And for this bracelet is a circle, it can be rotated at any angle, but since the direction of upper ornament is fixed, the bracelet can not be flipped. After transformation brightness required and rotated such that the difference between the minimum value of two bracelet.

In the two hands and the rotation of the ring after alignment garnish, from a starting position aligned counterclockwise garnish number 1,2, ..., n , where n is the number of each bracelet garnish . I first bracelet number position decoration luminance a_i $ $ , J bracelet second number position decoration luminance b_j $ $ . The difference between the two values bracelet: $ \ sum \ limits_ {i = 1} ^ {n} (a_i-b_i) ^ 2 $

Could you help him calculate, adjust (brightness transformation and rotation), so that the difference in value between the two bracelet minimum, this minimum value is how much?

n<=50000 m<=100

This question is skyh said water problem.

NC then there laughing, I thought it was skyh lying to me. So I want to beat him.

However, I would like to learn in chemistry will be admitted to ten minutes, so I'm not going to beat the skyh.

Sense strand is broken into a ring.

——By Paris

So put a bracelet off.

It has been aligned to join. Think about the last answer.

$\sum\limits_{i=1}^{n} (a_i - b_i - x)$

$=\sum\limits_{i=1}^{n} a_i^2 + b_i^2 + x^2 -2a_i x + 2 b_i x - 2 a_i b_i$

So you can find out about x part of solving a quadratic equation to solve. However, I do not, so get the best value x enumeration is also possible.

(I would have been in but do not know why WA90 autistic)

Therefore, the above equation is only one request $ \ sum \ limits_ {i = 1} ^ {n} a_i \ times b_i $

And here ab can dislocation, dislocation also requires and after. How to do?

If the alignment position is p, then the chain is broken into a ring $ \ sum \ limits_ {i = 1} ^ {n} a_ {p + i} \ times b_i $

This form is the "Quick changes of Fu Liye two." The b flip it over.

 1 #include<cstdio>
 2 #define mod 998244353
 3 #define int long long
 4 int max(int a,int b){return a>b?a:b;}
 5 int a[263333],b[263333],n,A,B,ans,len=1,rev[263333],mx=-1e16,m,mx2=-1e16;
 6 int pow(int b,int t,int a=1){for(;t;t>>=1,b=b*b%mod)if(t&1)a=a*b%mod;return a;}
 7 void NTT(int *a,int opt){
 8     for(int i=0;i<len;++i)if(rev[i]>i)a[i]^=a[rev[i]]^=a[i]^=a[rev[i]];
 9     for(int mid=1;mid<len;mid<<=1)
10         for(int i=0,t=pow(3,(mod-1)/mid/2*opt+mod-1);i<len;i+=mid<<1)
11             for(int j=0,w=1,x,y;j<mid;++j,w=w*t%mod)
12                 x=a[i+j],y=a[i+j+mid]*w%mod,a[i+j]=(x+y)%mod,a[i+j+mid]=(x-y+mod)%mod;
13     if(opt==-1)for(int i=0,iv=pow(len,mod-2);i<len;++i)a[i]=a[i]*iv%mod;
14 }
15 signed main(){
16     scanf("%lld%lld",&n,&m);
17     for(int i=0;i<n;++i)scanf("%lld",&a[i]),ans+=a[i]*a[i],a[i+n]=a[i],A+=a[i];
18     for(int i=0;i<n;++i)scanf("%lld",&b[i]),ans+=b[i]*b[i],B+=b[i];
19     while(len<=n*3)len<<=1;
20     for(int x=-m;x<=m;++x)mx2=max(mx2,-n*x*x+2*(A-B)*x);
21     ans-=mx2;
22     for(int i=0;i<n-1-i;++i)b[i]^=b[n-1-i]^=b[i]^=b[n-1-i];
23     for(int i=1;i<len;++i)rev[i]=rev[i>>1]>>1|(i&1?len>>1:0);
24     NTT(a,1);NTT(b,1);for(int i=0;i<len;++i)a[i]=a[i]*b[i]%mod;NTT(a,-1);
25     for(int i=0;i<n;++i)mx=max(mx,a[i+n]);
26     printf("%lld\n",ans-2*mx);
27 }
I still want to beat skyh how do ah?

Guess you like

Origin www.cnblogs.com/hzoi-DeepinC/p/12012631.html