hihocoder #1388 : Periodic Signal fft
题目链接:
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:
>
>
> 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的更多相关文章
- hihocoder #1388 : Periodic Signal NTT&FFT
传送门:hihocoder #1388 : Periodic Signal 先来几个大牛传送门: (模板) NTT long long 版 解法一:因为我们知道FFT会精度不够,所以坚持用NTT,但 ...
- hihoCoder 1388 Periodic Signal(FFT)
[题目链接] http://hihocoder.com/problemset/problem/1388 [题目大意] 给出A数列和B数列,求下图式子: [题解] 我们将多项式拆开,我们可以得到固定项A ...
- hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)
时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device w ...
- hihoCoder #1388 : Periodic Signal
NTT (long long 版) #include <algorithm> #include <cstring> #include <string.h> #inc ...
- hihocode #1388 : Periodic Signal NTT
#1388 : Periodic Signal 描述 Profess X is an expert in signal processing. He has a device which can ...
- 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 ...
- hihocoder 1388 fft循环矩阵
#1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal proce ...
- 【hihocoder#1388】Periodic Signal NTT
题目链接:http://hihocoder.com/problemset/problem/1388?sid=974337 题目大意:找出一个$k$,使得$\sum_{i=0}^{n-1}(A_{i}- ...
- hihoCoder1388 Periodic Signal(2016北京网赛F:NTT)
题目 Source http://hihocoder.com/problemset/problem/1388 Description Profess X is an expert in signal ...
随机推荐
- C++学习第一天(helloword)
C++编译过程 #include <iostream> //iostream 提供了一个叫命名空间的东西,标准的命名空间是std 包含了有关输入输出语句的函数 // input&^ ...
- 【Len's DMG】macOS Mojave 10.14.1 正式版 18B75 With Clover 4726原版镜像
亮点:本次10.14.1正式版镜像更新config配置文件SMbios机型信息,让识别更趋于完善,自带去除10.14.1 USB端口限制补丁和最新USBInjectAll.kext,移除大量可能造成卡 ...
- day 85 Vue学习之vue-cli脚手架下载安装及配置
1. 先下载node.js,下载地址:https://nodejs.org/en/download/ 找个目录保存,解压下载的文件,然后配置环境变量,将下面的路径配置到环境变量中. 由于 Node ...
- python 3.x 循环读取文件用户登录
import os # 导入python自带库的模块 import sys # 导入python自带库的模块 import getpass # 导入python自带库的模块 lock_file = ' ...
- C语言学习记录_2019.02.05
switch只能判断整数,而分段函数的判别是一个范围,我们无法用整数来表示范围 跟踪语句的方法: (1)debug调试 (2)printf( )语句跟踪 小套路:当循环次数很大时,可以先模拟较小次数的 ...
- Go语言中的常量
1 概述 常量,一经定义不可更改的量.功能角度看,当出现不需要被更改的数据时,应该使用常量进行存储,例如圆周率.从语法的角度看,使用常量可以保证数据,在整个运行期间内,不会被更改.例如当前处理器的架构 ...
- Verilog中使用'include实现参数化设计
前段时间在FPGA上用Verilog写了一个多端口以太网的数据分发模块,因为每个网口需要独立的MAC地址和IP地址,为了便于后期修改,在设计中使用parameter来定义这些地址和数据总线的位宽等常量 ...
- 【转】mxGraph教程-开发入门指南
原文:https://blog.csdn.net/sunhuaqiang1/article/details/51289580 mxGraph教程-开发入门指南 概述 mxGraph是一个JS绘图组件适 ...
- # 2017-2018-1 20155224 《信息安全系系统设计基础》第四周MyOD
2017-2018-1 20155224 <信息安全系系统设计基础>第四周MyOD 在这里跟老师先道歉,当时我的git没有安装好,后面也一直没有装上,所以程序没有git. 要求 参考教材第 ...
- P4284 [SHOI2014]概率充电器
P4284 [SHOI2014]概率充电器 今天上课讲到的题orz,第一次做这种上下搞两次dp的题. g[i]表示i的子树(包括i)不给i充电的概率. f[i]表示i的父亲不给i充电的概率. g[]可 ...
