#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 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
/*
hihocoder 1388 fft循环矩阵 problem:
给你两个数组,求差值平方和的最小值. (a[i] - b[(i+k)%n])^ solve:
可以转换成 (a[i]*a[i]-2*a[i]*b[j]+b[j]*b[j]). 也就成了求a[i]*b[j]的最大值. 然后就没什么想法了- -
参考:http://blog.csdn.net/VictorZC8/article/details/52655099
说的是可以转换成两个数组相乘. 这样的话fft就能解决了. double型的话会有精度问题
结果发现别人是先求fft求完,比较出k的价值. 然后计算 ( 好机制Orz ) hhh-2016-09-25 16:53:25
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <math.h>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define key_val ch[ch[root][1]][0]
using namespace std;
const int maxn = 1e6 + 1000;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const double eps = 1e-7;
template<class T> void read(T&num)
{
char CH;
bool F=false;
for(CH=getchar(); CH<'0'||CH>'9'; F= CH=='-',CH=getchar());
for(num=0; CH>='0'&&CH<='9'; num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p)
{
if(!p)
{
puts("0");
return;
}
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const double PI = acos(-1.0); struct Complex
{
double x,y;
Complex(double _x = 0.0,double _y = 0.0)
{
x = _x;
y = _y;
}
Complex operator-(const Complex &b)const
{
return Complex(x-b.x,y-b.y);
}
Complex operator+(const Complex &b)const
{
return Complex(x+b.x,y+b.y);
}
Complex operator*(const Complex &b)const
{
return Complex(x*b.x-y*b.y,x*b.y+y*b.x);
}
}; void change(Complex y[],int len)
{
int i,j,k;
for(i = 1,j = len/2; i < len-1; i++)
{
if(i < j) swap(y[i],y[j]);
k = len/2;
while(j >= k)
{
j-=k;
k/=2;
}
if(j < k) j+=k;
}
} void fft(Complex y[],int len,int on)
{
change(y,len);
for(int h = 2; h <= len; h <<= 1)
{
Complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
for(int j = 0; j < len; j+=h)
{
Complex w(1,0);
for(int k = j; k < j+h/2; k++)
{
Complex u = y[k];
Complex t = w*y[k+h/2];
y[k] = u+ t;
y[k+h/2] = u-t;
w = w*wn;
}
}
}
if(on == -1)
{
for(int i = 0; i < len; i++)
y[i].x /= len;
}
} Complex a[maxn];
Complex b[maxn];
ll x[maxn],y[maxn];
int n; void File()
{
freopen("in.txt","r",stdin);
} int main()
{
int T;
// File();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int len = 1;
while(len <= (n*2 -1)) len <<= 1;
for(int i = 0;i < n;i++)
{
scanf("%lld",&x[i]);
a[i] = x[i];
}
for(int i = n;i < len;i++)
a[i] = 0;
for(int i = 0;i < n;i++)
{
scanf("%lld",&y[i]);
}
for(int i = 0;i < n;i++) b[i] = y[n-1-i];
for(int i = 0;i < n-1;i++) b[i+n] = y[n-1-i];
for(int i = 2*n-1;i < len;i ++)
b[i] = 0;
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);
ll Max = -1;
int pos = 0;
for(int i = n-1,j = 0;j <= n;j++ )
{
if( (ll)(a[i+j].x + 0.5) > Max)
{
pos = j;
Max = (ll)(a[i+j].x + 0.5);
}
}
ll ans = 0;
pos = (n-pos) % n;
// cout << pos<<endl; for(int i = 0;i < n;i++)
{
ans += (x[i] - y[(i + pos) % n])*(x[i] - y[(i + pos) % n]);
}
print(ans);
}
return 0;
}

  

hihocoder 1388 fft循环矩阵的更多相关文章

  1. hihocoder #1388 : Periodic Signal NTT&FFT

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

  2. bzoj 2510: 弱题 概率期望dp+循环矩阵

    题目: Description 有M个球,一开始每个球均有一个初始标号,标号范围为1-N且为整数,标号为i的球有ai个,并保证Σai = M. 每次操作等概率取出一个球(即取出每个球的概率均为1/M) ...

  3. POJ 3150 循环矩阵的应用

    思路: 首先 先普及一个性质: 循环矩阵*循环矩阵=循环矩阵 由于此题是距离小于d的都加上一个数. 那么 构造矩阵的时候 我们发现 诶呦 这是个循环矩阵 看看数据范围 n^2log(k)可以过. 那就 ...

  4. LA 3704细胞自动机——循环矩阵&&矩阵快速幂

    题目 一个细胞自动机包含 $n$ 个格子,每个格子的取值为 $0 \sim m-1$.给定距离 $d$,则每次操作是将每个格子的值变为到它的距离不超过 $d$ 的所有格子的在操作之前的值的和除以 $m ...

  5. BZOJ 4204 && BZOJ 2510 循环矩阵

    n^3logn非常显然.所以要用一种因为这个矩阵是一个循环矩阵,所以只要知道第一行就可以知道所有行了. C[i][j]=C[i-1][j-1]; #include <iostream> # ...

  6. LA 3704 (矩阵快速幂 循环矩阵) Cellular Automaton

    将这n个格子看做一个向量,每次操作都是一次线性组合,即vn+1 = Avn,所求答案为Akv0 A是一个n*n的矩阵,比如当n=5,d=1的时候: 不难发现,A是个循环矩阵,也就是将某一行所有元素统一 ...

  7. bzoj 2510: 弱题 循环矩阵

    2510: 弱题 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 124  Solved: 61[Submit][Status][Discuss] De ...

  8. UVA 1386 - Cellular Automaton(循环矩阵)

    UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...

  9. UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)

    题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离 ...

随机推荐

  1. Alpha冲刺Day5

    Alpha冲刺Day5 一:站立式会议 今日安排: 首先由于经过黄腾飞短暂的测试,发现导入导出仍然有一些问题,今天需要进行完善 由黄腾飞负责企业自查风险管理子模块,要求为单元进行风险点的管理 由张梨贤 ...

  2. 记录Yii2代码调试中出现的两个问题(截图展示)

    1.代码会中断执行,不提示错误信息,是由于substr函数第一个参数为数组造成的 2. 谷歌浏览器调试异步调用接口时出现的错误,需在接口返回处进行断点调试 这两个错误比较隐蔽,调试代码时必须认真仔细

  3. kafka安装使用和遇到的坑

    下载安装 参考:https://segmentfault.com/a/1190000012730949 ​ https://kafka.apache.org/quickstart 关闭服务 关闭zoo ...

  4. python/SQLAchemy

    python/SQLAchemy SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数 ...

  5. 云计算 IaaS,SaaS,PaaS的区别?一个通俗易懂的吃货文章

    来自一篇吃货文章了: ———————————————————— &lt;img src="https://pic2.zhimg.com/a55676f8e1b084a398f8cd5 ...

  6. POJ-3641 Pseudoprime numbers---快速幂

    题目链接: https://vjudge.net/problem/POJ-3641 题目大意: 问p是不是伪素数.伪素数条件:①p不是素数.② ap = a (mod p). 思路: 直接快速幂模板+ ...

  7. POJ-3295 Tautology---栈+表达式求值

    题目链接: https://vjudge.net/problem/POJ-3295 题目大意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式WFF      其中      ...

  8. 使用MFC创建C++程序

    编译环境:VS2017 MFC简介: MFC(MicrosoftFoundationClasses)是微软基础类库的简称,是微软公司实现的一个c++类库,主要封装了大部分的windows API函数. ...

  9. scrapy爬取全部知乎用户信息

    # -*- coding: utf-8 -*- # scrapy爬取全部知乎用户信息 # 1:是否遵守robbots_txt协议改为False # 2: 加入爬取所需的headers: user-ag ...

  10. 定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容。提示(可以了解python的urllib模块)

    定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容.提示(可以了解python的urllib模块) import urllib.request def get_ ...