hihocoder1388 Periodic Signal

FFT 就可以了 比赛时候没时间做了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 6e4+5;

int A[MAXN<<2], B[MAXN<<2], C[MAXN<<2];
struct FFTSOLVE {
    int pos[MAXN<<2];
    struct comp {
        double r , i ;
        comp ( double _r = 0 , double _i = 0 ) : r ( _r ) , i ( _i ) {}
        comp operator + ( const comp& x ) {
            return comp ( r + x.r , i + x.i ) ;
        }
        comp operator - ( const comp& x ) {
            return comp ( r - x.r , i - x.i ) ;
        }
        comp operator * ( const comp& x ) {
            return comp ( r * x.r - i * x.i , i * x.r + r * x.i ) ;
        }
        comp conj () {
            return comp ( r , -i ) ;
        }
    } A[MAXN<<2] , B[MAXN<<2] ;
    const double pi = acos ( -1.0 ) ;
    void FFT ( comp a[] , int n , int t ) {
        for ( int i = 1 ; i < n ; ++ i ) if ( pos[i] > i ) swap ( a[i] , a[pos[i]] ) ;
        for ( int d = 0 ; ( 1 << d ) < n ; ++ d ) {
            int m = 1 << d , m2 = m << 1 ;
            double o = pi * 2 / m2 * t ;
            comp _w ( cos ( o ) , sin ( o ) ) ;
            for ( int i = 0 ; i < n ; i += m2 ) {
                comp w ( 1 , 0 ) ;
                for ( int j = 0 ; j < m ; ++ j ) {
                    comp& A = a[i + j + m] , &B = a[i + j] , t = w * A ;
                    A = B - t ;
                    B = B + t ;
                    w = w * _w ;
                }
            }
        }
        if ( t == -1 ) for ( int i = 0 ; i < n ; ++ i ) a[i].r /= n ;
    }
    void mul ( int *a , int *b , int *c ,int k) {
        int i , j ;
        for ( i = 0 ; i < k ; ++ i ) A[i] = comp ( a[i] , b[i] ) ;
        j = __builtin_ctz ( k ) - 1 ;
        for ( int i = 0 ; i < k ; ++ i ) {
            pos[i] = pos[i >> 1] >> 1 | ( ( i & 1 ) << j ) ;
        }
        FFT ( A , k , 1 ) ;
        for ( int i = 0 ; i < k ; ++ i ) {
            j = ( k - i ) & ( k - 1 ) ;
            B[i] = ( A[i] * A[i] - ( A[j] * A[j] ).conj () ) * comp ( 0 , -0.25 ) ;
        }
        FFT ( B , k , -1 ) ;
        for ( int i = 0 ; i < k ; ++ i ) {
            c[i] = ( long long ) ( B[i].r + 0.5 );
        }
    }
}boy;

int N;
int s1[MAXN], s2[MAXN];
int main(){
    int T; scanf("%d",&T);
    while(T--) {
        ll ans = 0;
        memset(A,0,sizeof(A));
        memset(B,0,sizeof(B));
        scanf("%d",&N);
        for(int i = 0; i < N; ++i) {
            scanf("%d",&s1[i]); 
        } 
        for(int i = 0; i < N; ++i) {
            scanf("%d",&s2[i]); 
        }

        int len = 1; while(len < N*2) len <<= 1;
        for(int i = 0; i < N; ++i) {
            A[i] = s1[N-i-1]; B[i] = s2[i];
        }
        for(int i = N; i < 2*N; ++i) {
            A[i] = 0; B[i] = s2[i-N];
        }
        boy.mul(A,B,C,len);

        double an = -1; double eps = 1e-8; int pos;
        for(int i = 0; i < N; ++i) {
            if(an+eps < boy.B[i+N-1].r) {
                an = boy.B[i+N-1].r; pos = i;
            }
        }

        for(int i = 0; i < N; ++i) {
            ans += 1ll*(s1[i]-s2[(i+pos)%N]) * (s1[i]-s2[(i+pos)%N]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
posted @ 2016-09-25 15:16  basasuya  阅读(119)  评论(0编辑  收藏  举报