hihocode #1388 : Periodic Signal NTT

#1388 : Periodic Signal

描述

Profess X is an expert in signal processing. He has a device which can send a particular 1 second signal repeatedly. The signal is A0 ... An-1 under n Hz sampling.

One day, the device fell on the ground accidentally. Profess X wanted to check whether the device can still work properly. So he ran another n Hz sampling to the fallen device and got B0 ... Bn-1.

To compare two periodic signals, Profess X define the DIFFERENCE of signal A and B as follow:

You may assume that two signals are the same if their DIFFERENCE is small enough. 
Profess X is too busy to calculate this value. So the calculation is on you.

输入

The first line contains a single integer T, indicating the number of test cases.

In each test case, the first line contains an integer n. The second line contains n integers, A0 ... An-1. The third line contains n integers, B0 ... Bn-1.

T≤40 including several small test cases and no more than 4 large test cases.

For small test cases, 0<n≤6⋅103.

For large test cases, 0<n≤6⋅104.

For all test cases, 0≤Ai,Bi<220.

输出

For each test case, print the answer in a single line.

样例输入
2
9
3 0 1 4 1 5 9 2 6
5 3 5 8 9 7 9 3 2
5
1 2 3 4 5
2 3 4 5 1
样例输出
80
0

EmacsNormalVim

 题解:

  化简式子

  也就是求

  

后面这个只需将B数组倒置,进行卷积,出来就是一段连续的位置是k = 0……n-1,所有情况

  代码来自huyifan

#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAXN 300000
typedef long long LL;
const long long P=50000000001507329LL;
const int G=3;

LL mul(LL x,LL y){
    return (x*y-(LL)(x/(long double)P*y+1e-3)*P+P)%P;
}
LL qpow(LL x,LL k,LL p){
    LL ret=1;
    while(k){
        if(k&1) ret=mul(ret,x);
        k>>=1;
        x=mul(x,x);
    }
    return ret;
}

LL wn[25];
void getwn(){
    for(int i=1; i<=18; ++i){
        int t=1<<i;
        wn[i]=qpow(G,(P-1)/t,P);
    }
}

int len;
void NTT(LL y[],int op){
    for(int i=1,j=len>>1,k; i<len-1; ++i){
        if(i<j) swap(y[i],y[j]);
        k=len>>1;
        while(j>=k){
            j-=k;
            k>>=1;
        }
        if(j<k) j+=k;
    }
    int id=0;
    for(int h=2; h<=len; h<<=1) {
        ++id;
        for(int i=0; i<len; i+=h){
            LL w=1;
            for(int j=i; j<i+(h>>1); ++j){
                LL u=y[j],t=mul(y[j+h/2],w);
                y[j]=u+t;
                if(y[j]>=P) y[j]-=P;
                y[j+h/2]=u-t+P;
                if(y[j+h/2]>=P) y[j+h/2]-=P;
                w=mul(w,wn[id]);
            }
        }
    }
    if(op==-1){
        for(int i=1; i<len/2; ++i) swap(y[i],y[len-i]);
        LL inv=qpow(len,P-2,P);
        for(int i=0; i<len; ++i) y[i]=mul(y[i],inv);
    }
}
void Convolution(LL A[],LL B[],int n){
    for(len=1; len<(n<<1); len<<=1);
    for(int i=n; i<len; ++i){
        A[i]=B[i]=0;
    }

    NTT(A,1); NTT(B,1);
    for(int i=0; i<len; ++i){
        A[i]=mul(A[i],B[i]);
    }
    NTT(A,-1);
}
int t,nn,m;
LL a[MAXN],b[MAXN];
LL ans,MAX;
int main()
{

    getwn();
    scanf("%d",&t);

    while(t--)
    {

        MAX=0;

        ans=0;

        scanf("%d",&nn);

        for(int i=0;i<nn;i++)
        {
            scanf("%lld",&a[i]);
            ans+=a[i]*a[i];
        }

        for(int i=0;i<nn;i++)
        {
            scanf("%lld",&b[nn - i - 1]);
            ans+=b[nn - i - 1]*b[nn - i - 1];
        }

        for(int i=0;i<nn;i++)
        {
            a[i+nn]=a[i];
            b[i+nn]=0;
        }

        Convolution(a,b,2*nn);

        for(int i=nn;i<2*nn;i++)
        {
            MAX=max(MAX,a[i]);
        }

        printf("%lld\n",ans-2*MAX);

    }

    return 0;
}
时间: 2024-12-29 07:19:32

hihocode #1388 : Periodic Signal NTT的相关文章

hihocoder #1388 : Periodic Signal NTT&amp;FFT

传送门:hihocoder #1388 : Periodic Signal 先来几个大牛传送门:  (模板) NTT long long 版 解法一:因为我们知道FFT会精度不够,所以坚持用NTT,但是模数不够大,然后就一直GG,看来我们的搜索姿势也有问题,居然没有搜到上面大神的板子,真的是GG http://www.cnblogs.com/WABoss/p/5903927.html /*******************************************************

hihocoder 1388 Periodic Signal

Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB Description Profess X is an expert in signal processing. He has a device which can send a particular 1 second signal repeatedly. The signal is A0 ... An-1 under n Hz sampling. One day, the device fel

hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)

时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device which can send a particular 1 second signal repeatedly. The signal is A0 ... An-1 under n Hz sampling. One day, the device fell on the ground accidenta

hihoCoder 1388 Periodic Signal(FFT)

[题目链接] http://hihocoder.com/problemset/problem/1388 [题目大意] 给出A数列和B数列,求下图式子: [题解] 我们将多项式拆开,我们可以得到固定项A2和B2,以及变动项-2AB,所以现在只要最大化AB即可.我们发现将A序列倒置,B序列倍置,所得到的卷积序列中的最大值就是AB的最大值,但是考虑到精度问题,我们在所得到的卷积序列中只判断极值产生的位置而不是直接获得极值,最后我们从极值产生的位置直接计算答案即可. [代码] #include <cst

hihoCoder1388 Periodic Signal(2016北京网赛F:NTT)

题目 Source http://hihocoder.com/problemset/problem/1388 Description Profess X is an expert in signal processing. He has a device which can send a particular 1 second signal repeatedly. The signal is A0 ... An-1 under n Hz sampling. One day, the device

hihocoder 1388 fft循环矩阵

#1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device which can send a particular 1 second signal repeatedly. The signal is A0 ... An-1 under n Hz sampling. One day, the device fell

Radio Basics for RFID

Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09/24 22:30:37) Radio Basics for RFID (2015/09

《DSP using MATLAB》示例Example5.1

终于看到第5章了,继续努力,加油!!! 代码: xn = [0, 1, 2, 3]; N =4; Xk = dfs(xn,N) 用到的dfs函数: function [Xk] = dfs(xn,N) %% Computes Discrete Fourier Series Coefficients %% --------------------------------------------- %% [Xk] = dfs(xn,N) %% Xk = DFS coeff. array over 0

Django 1.6 最佳实践: 如何正确使用 Signal(转)

原文:http://www.weiguda.com/blog/38/ 如何正确的使用signal: 简单回答是: 在其他方法无法使用的情况下, 才最后考虑使用signal. 因为新的django开发人员得知signal之后, 往往会很高兴去使用它. 他们在能使用signal的地方就使用signal, 并且这是他们觉得自己是django专家一样. 然而, 像这样编码一段时间后, django项目就会变得异常复杂, 许多内容都纠结在一起无法解开. 许多开发者也会将django signal和异步消息