hihocoder 1388 fft循环矩阵
#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
/*
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循环矩阵的更多相关文章
- hihocoder #1388 : Periodic Signal NTT&FFT
传送门:hihocoder #1388 : Periodic Signal 先来几个大牛传送门: (模板) NTT long long 版 解法一:因为我们知道FFT会精度不够,所以坚持用NTT,但 ...
- bzoj 2510: 弱题 概率期望dp+循环矩阵
题目: Description 有M个球,一开始每个球均有一个初始标号,标号范围为1-N且为整数,标号为i的球有ai个,并保证Σai = M. 每次操作等概率取出一个球(即取出每个球的概率均为1/M) ...
- POJ 3150 循环矩阵的应用
思路: 首先 先普及一个性质: 循环矩阵*循环矩阵=循环矩阵 由于此题是距离小于d的都加上一个数. 那么 构造矩阵的时候 我们发现 诶呦 这是个循环矩阵 看看数据范围 n^2log(k)可以过. 那就 ...
- LA 3704细胞自动机——循环矩阵&&矩阵快速幂
题目 一个细胞自动机包含 $n$ 个格子,每个格子的取值为 $0 \sim m-1$.给定距离 $d$,则每次操作是将每个格子的值变为到它的距离不超过 $d$ 的所有格子的在操作之前的值的和除以 $m ...
- BZOJ 4204 && BZOJ 2510 循环矩阵
n^3logn非常显然.所以要用一种因为这个矩阵是一个循环矩阵,所以只要知道第一行就可以知道所有行了. C[i][j]=C[i-1][j-1]; #include <iostream> # ...
- LA 3704 (矩阵快速幂 循环矩阵) Cellular Automaton
将这n个格子看做一个向量,每次操作都是一次线性组合,即vn+1 = Avn,所求答案为Akv0 A是一个n*n的矩阵,比如当n=5,d=1的时候: 不难发现,A是个循环矩阵,也就是将某一行所有元素统一 ...
- bzoj 2510: 弱题 循环矩阵
2510: 弱题 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 124 Solved: 61[Submit][Status][Discuss] De ...
- UVA 1386 - Cellular Automaton(循环矩阵)
UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...
- UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)
题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离 ...
随机推荐
- 201621123060《JAVA程序设计》第二周学习总结
1.本周学习总结 本周学习了JAVA中的引用类.包装类(学习了一种语法:自动装箱)和数组(遍历数组的新方法foreach循环). 2. 书面作业 1.String-使用Eclipse关联jdk源代码 ...
- IOS webview iframe 宽度超出屏幕解决方案
IOS 真机webview中,iframe 却不能很好地适应屏幕大小,总是超出屏幕尺寸,需要左右滚动才能看到完整页面. <div style="overflow: auto;-webk ...
- Spring Boot 配置文件详解
Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...
- sql 用临时表时报错 "Chinese_PRC_90_CI_AI" 和 "Chinese_PRC_CI_AS" 之间的排序规则冲突
在用临时表关联数据库中的表做关联查询时,如果报这种情况的话,就要把临时表和关联的表的排序规则统一掉. LEFT JOIN #tsub ON #tsub.joinjarno collate Chines ...
- JAVA_SE基础——66.StringBuffer类 ③
如果需要频繁修改字符串 的内容,建议使用字符串缓冲 类(StringBuffer). StringBuffer 其实就是一个存储字符 的容器. 容器的具备 的行为 常用方法 String 增加 ap ...
- Python内置函数(41)——id
英文文档: id(object) Return the "identity" of an object. This is an integer which is guarantee ...
- Spring知识点回顾(06)Profile 和 条件注解 @Conditional
1.设定环境中的active profiles 如:DispatcherServerlet的init-param spring.profiles.active=production spring.pr ...
- 洛谷P1209-最大公约数与最小公倍数问题
一个萌新的成长之路 Discription 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件: 1.P, ...
- python—-模块与包1
模块与包 1 什么是模块? 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀 2 为何要使用模块? 如果你对出python解释器然后重新进入,那么你之前定义的函数 ...
- Hive:表1inner join表2结果group by优化
问题背景 最近遇到一个比较棘手的事情:hive sql优化: lib表(id,h,soj,noj,sp,np) --一个字典表 mitem表(md,mt,soj,noj,sp,np)- ...