题目链接:

https://hihocoder.com/problemset/problem/1388

Periodic Signal

时间限制: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 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:
>![](http://images2015.cnblogs.com/blog/809202/201609/809202-20160925112540556-540075792.png)
>
> 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
> For large test cases, 0
> For all test cases, 0≤Ai,Bi 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

题意

给你两个大小为n的数组a,b,求:

题解

构造下a,b数组就可以转换成多项式乘法问题,然后用fft计算,注意最后计算结果会有浮点误差,但是大小关系还是可以用的,所以求出最大的位置之后,把答案再算一遍。

构造:

另n等于3:

a0a1a2 --> a2a1a0

b0b1b2 --> b0b1b2b0b1b2

这样x2到x4的系数就是答案。

代码

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define scf scanf
#define prf printf const int maxn=24e4+10;
const double PI=acos(-1.0);
const double eps=1e-8;
typedef long long LL; struct Complex {
double real, image;
Complex(double real, double image):real(real),image(image) {}
Complex() {}
friend Complex operator + (const Complex &c1, const Complex &c2) {
return Complex(c1.real + c2.real, c1.image + c2.image);
}
friend Complex operator - (const Complex &c1, const Complex &c2) {
return Complex(c1.real - c2.real, c1.image - c2.image);
}
friend Complex operator * (const Complex &c1, const Complex &c2) {
return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real);
}
}A[maxn],B[maxn]; struct IterativeFFT {
Complex A[maxn]; int rev(int id, int len) {
int ret = 0;
for(int i = 0; (1 << i) < len; i++) {
ret <<= 1;
if(id & (1 << i)) ret |= 1;
}
return ret;
} //当DFT= 1时是DFT, DFT = -1则是逆DFT
//对长度为len(2的幂)的数组进行DFT变换
void FFT(Complex *a,int len, int DFT) {
for(int i = 0; i < len; i++)
A[rev(i, len)] = a[i];
for(int s = 1; (1 << s) <= len; s++) {
int m = (1 << s);
Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
//这一层结点的包含数组元素个数都是(1 << s)
for(int k = 0; k < len; k += m) {
Complex w = Complex(1, 0);
//折半引理, 根据两个子节点计算父亲节点
for(int j = 0; j < (m >> 1); j++) {
Complex t = w*A[k + j + (m >> 1)];
Complex u = A[k + j];
A[k + j] = u + t;
A[k + j + (m >> 1)] = u - t;
w = w*wm;
}
}
}
if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
for(int i=0; i<len; i++) a[i]=A[i];
} int solve(Complex* a,Complex* b,int len,int n){
FFT(a,len,1),FFT(b,len,1);
for(int i=0;i<len;i++) a[i]=a[i]*b[i];
FFT(a,len,-1); int pos=0;
double ans=-1;
for(int i=0;i<n;i++){
if(ans+eps<a[i+n-1].real){
ans=a[i+n-1].real;
pos=i;
}
}
return pos;
} } myfft; LL a[maxn],b[maxn];
int n; int main(){
int tc;
scf("%d",&tc);
while(tc--){
scf("%d",&n);
int len=1; while(len<n*2) len<<=1;
for(int i=0;i<n;i++) scf("%lld",&a[i]);
for(int i=0;i<n;i++) scf("%lld",&b[i]); for(int i=0;i<len;i++){
A[i]=Complex(0,0),B[i]=Complex(0,0);
} for(int i=0;i<n;i++){
A[i]=Complex(a[n-1-i],0),B[i]=Complex(b[i],0);
A[i+n]=Complex(0,0), B[i+n]=Complex(b[i],0);
} int k=myfft.solve(A,B,len,n); LL ans=0;
for(int i=0;i<n;i++){
int j=(i+k)%n;
ans+=(a[i]-b[j])*(a[i]-b[j]);
} prf("%lld\n",ans);
}
}

hihocoder #1388 : Periodic Signal fft的更多相关文章

  1. hihocoder #1388 : Periodic Signal NTT&FFT

    传送门:hihocoder #1388 : Periodic Signal 先来几个大牛传送门:  (模板) NTT long long 版 解法一:因为我们知道FFT会精度不够,所以坚持用NTT,但 ...

  2. hihoCoder 1388 Periodic Signal(FFT)

    [题目链接] http://hihocoder.com/problemset/problem/1388 [题目大意] 给出A数列和B数列,求下图式子: [题解] 我们将多项式拆开,我们可以得到固定项A ...

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

    时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device w ...

  4. hihoCoder #1388 : Periodic Signal

    NTT (long long 版) #include <algorithm> #include <cstring> #include <string.h> #inc ...

  5. hihocode #1388 : Periodic Signal NTT

    #1388 : Periodic Signal   描述 Profess X is an expert in signal processing. He has a device which can ...

  6. hihocoder 1388 &&2016 ACM/ICPC Asia Regional Beijing Online Periodic Signal

    #1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal proce ...

  7. hihocoder 1388 fft循环矩阵

    #1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal proce ...

  8. 【hihocoder#1388】Periodic Signal NTT

    题目链接:http://hihocoder.com/problemset/problem/1388?sid=974337 题目大意:找出一个$k$,使得$\sum_{i=0}^{n-1}(A_{i}- ...

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

    题目 Source http://hihocoder.com/problemset/problem/1388 Description Profess X is an expert in signal ...

随机推荐

  1. VS2010调试和头文件路径设置

    1:VS2010 release 调试C/C++ -> 常规 -> 调试信息格式, 修改为程序数据库(/Zi)C/C++ ->优化 -> 优化,修改为已禁用(/Od)链接器 - ...

  2. react fake double , bind click and dblclick on the same element

    smartClick:function(id,name,waiter,e){ var desk = $$(e.currentTarget).data('raw'); if(this.lastClick ...

  3. GNS3 jungle newsfeed 隐藏

    windows 7 windows 8.1 1.开始---运行 输入(没有引号):“%appdata%” 2.修改---GNS3/gns3_gui.ini 的两行参数 "default_lo ...

  4. sqlite3 数据库命令操作

    SQLite 数据库,是一个非常轻量级自包含(lightweight and self-contained)的DBMS,它可移植性好,很容易使用,很小,高效而且可靠. SQLite嵌入到使用它的应用程 ...

  5. 一步步实现一个基本的缓存模块·续, 添加Memcached调用实现

    jusfr 原创,转载请注明来自博客园. 在之前的实现中,我们初步实现了一个缓存模块:包含一个基于Http请求的缓存实现,一个基于HttpRuntime.Cache进程级的缓存实现,但观察代码,会发现 ...

  6. android 图片二维码识别和保存(二)

    续上一篇,开发图片二维码识别功能后,我们对功能进行性能分析内存占用显著提高了,不使用该功能内存占用大约是147M,使用这个功能多次以后,高达203M. 因此对功能进行研究,发现每次生成的图片没有即时的 ...

  7. Win7搭建FTP服务器

    “控制面板” -> “程序和功能” -> “打开或关闭Windows 功能”: 1.展开“Internet 信息服务” 2.勾选“Internet Information Services ...

  8. 【转】sshpass-Linux命令之非交互SSH密码验证

      sshpass-Linux命令之非交互SSH密码验证 ssh登陆不能在命令行中指定密码.sshpass的出现,解决了这一问题.sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次 ...

  9. alibaba/fescar 阿里巴巴 开源 分布式事务中间件

    Fescar 是 阿里巴巴 开源的 分布式事务中间件,以 高效 并且对业务 0 侵入 的方式,解决 微服务 场景下面临的分布式事务问题. 示例:https://github.com/windwant/ ...

  10. 我的第一个上线小程序,案例实战篇二——LayaAir游戏开始界面开发

    不知不觉我的第一个小程序已经上线一周了,uv也稳定的上升着. 很多人说我的小程序没啥用,我默默一笑,心里说:“它一直敦促我学习,敦促我进步”.我的以一个小程序初衷是经验分享,目前先把经验分享到博客园, ...