时间限制: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.

题解

A[]的平方和 与 B[]的平方和可以直接求出。所以只要求出的最大值即可得到答案。

即求A[]与B[]的循环卷积。 FFT求解。

注意由于数据较大,FFT会出现精度问题。最后结果会有浮点精度误差,但是由结果得到的 k 是正确的,所以一个无赖的办法是根据FFT 的结果求 K,然后再自己算一遍得到最后答案。

注:题解的标准做法是找两个 10910^910​9​​ 左右模数 NTT 后 CRT 。

#include <algorithm>
#include <cstring>
#include <string.h>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cstdio>
#include <cmath> #define LL long long
#define N 60005
#define INF 0x3ffffff using namespace std; const long double PI = acos(-1.0); struct Complex // 复数
{
long double r,i;
Complex(long double _r = ,long double _i = )
{
r = _r; i = _i;
}
Complex operator +(const Complex &b)
{
return Complex(r+b.r,i+b.i);
}
Complex operator -(const Complex &b)
{
return Complex(r-b.r,i-b.i);
}
Complex operator *(const Complex &b)
{
return Complex(r*b.r-i*b.i,r*b.i+i*b.r);
}
}; void change(Complex y[],int len) // 二进制平摊反转置换 O(logn)
{
int i,j,k;
for(i = , j = len/;i < len-;i++)
{
if(i < j)swap(y[i],y[j]);
k = len/;
while( j >= k)
{
j -= k;
k /= ;
}
if(j < k)j += k;
}
}
void fft(Complex y[],int len,int on) //DFT和FFT
{
change(y,len);
for(int h = ;h <= len;h <<= )
{
Complex wn(cos(-on**PI/h),sin(-on**PI/h));
for(int j = ;j < len;j += h)
{
Complex w(,);
for(int k = j;k < j+h/;k++)
{
Complex u = y[k];
Complex t = w*y[k+h/];
y[k] = u+t;
y[k+h/] = u-t;
w = w*wn;
}
}
}
if(on == -)
for(int i = ;i < len;i++)
y[i].r /= len;
} const int MAXN = ; Complex x1[MAXN],x2[MAXN];
LL a[MAXN/],b[MAXN/]; //原数组
long long num[MAXN]; //FFT结果
void init(){
memset(num,,sizeof(num));
memset(x1,,sizeof(x1));
memset(x2,,sizeof(x2));
} int main()
{
int T;
scanf("%d",&T);
LL suma,sumb;
while(T--)
{
int n;
suma=;sumb=;
init();
scanf("%d",&n);
for(int i = ;i < n;i++) {scanf("%lld",&a[i]);suma+=a[i]*a[i];}
for(int i = ;i < n;i++) {scanf("%lld",&b[i]);sumb+=b[i]*b[i];}
int len = ;
while( len < *n ) len <<= ;
for(int i = ;i < n;i++){
x1[i] = Complex(a[i],);
}
for(int i = ;i < n;i++){
x2[i] = Complex(b[n-i-],);
}
// for(int i=n;i<len;i++) x1[i]=Complex(0,0);
fft(x1,len,);fft(x2,len,);
for(int i = ;i < len;i++){
x1[i] = x1[i]*x2[i];
}
fft(x1,len,-);
for(int i = ;i < len;i++){
num[i] = (LL)(x1[i].r+0.5);
}
// for(int i = 0;i < len;i++) cout<<num[i]<<endl;
LL ret=num[n-];
int flag=;
// cout<<ret<<endl;
for(int i=;i<n-;i++) {
// cout<<num[i]+num[i+n]<<endl;
if(ret<num[i]+num[i+n])
{ret=num[i]+num[i+n]; flag=n--i;}
//注意,此时得到的ret会有很小的浮点精度误差,
//flag表示k,这个是正确的
}
ret=;
for(int i=;i<n;i++){
ret+=a[i]*b[(i+flag)%n]; //重新算一遍得到最后答案
}
LL ans=suma+sumb-*ret;
cout<< ans<<endl;
}
return ;
}

hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)的更多相关文章

  1. 2016 acm香港网络赛 F题. Crazy Driver(水题)

    原题网址:https://open.kattis.com/problems/driver Crazy Driver In the Linear City, there are N gates arra ...

  2. 2016 acm香港网络赛 C题. Classrooms(贪心)

    原题网址:https://open.kattis.com/problems/classrooms Classrooms The new semester is about to begin, and ...

  3. 2016 acm香港网络赛 B题. Boxes

    原题网址:https://open.kattis.com/problems/boxes Boxes There are N boxes, indexed by a number from 1 to N ...

  4. 2016 acm香港网络赛 A题. A+B Problem (FFT)

    原题地址:https://open.kattis.com/problems/aplusb FFT代码参考kuangbin的博客:http://www.cnblogs.com/kuangbin/arch ...

  5. (中等) Hiho 1232 Couple Trees(15年北京网络赛F题),主席树+树链剖分。

    "Couple Trees" are two trees, a husband tree and a wife tree. They are named because they ...

  6. hihocoder #1388 : Periodic Signal NTT&FFT

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

  7. ACM-ICPC 2019南昌网络赛F题 Megumi With String

    ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...

  8. hihocoder #1388 : Periodic Signal fft

    题目链接: https://hihocoder.com/problemset/problem/1388 Periodic Signal 时间限制:5000ms内存限制:256MB 问题描述 Profe ...

  9. hdu 5881 Tea (2016 acm 青岛网络赛)

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5881 Tea Time Limit: 3000/1000 MS (Java/Others)    Me ...

随机推荐

  1. mysql root强密码的必要性max_allowed_packet被改成1024引起的风险

    前两天运维反馈说,有些机器的max_allowed_packet隔两天就会被改成1024,导致客户端调用时出错,网上有说内存不够的,也有人工修改的. 运维小姑娘一口咬定肯定没有改过的,而且my.cnf ...

  2. 【JavaEE】Hibernate继承映射,不用多态查询只查父表的方法

    几个月前,我在博问里面发了一个问题:http://q.cnblogs.com/q/64900/,但是一直没有找到好的答案,关闭问题以后才自己解决了,在这里分享一下. 首先我重复一下场景,博问里面举的动 ...

  3. Backbone学习笔记一Backbone中的MVC

    原文章地址http://bigdots.github.io/2015/12/01/Backbone学习笔记(一)/#more Backbone.js为复杂WEB应用程序提供模型(models).集合( ...

  4. HTML 5 <mark> 标签

    一,定义和用法 <mark> 标签定义带有记号的文本.请在需要突出显示文本时使用 <m> 标签. 二,实例 突出显示部分文本: <!DOCTYPE HTML> &l ...

  5. 模拟Select-Options对象实现多项数据输入功能

       模拟Select-Options对象实现多项数据输入功能 Select-Options对象可以同时输入多项值并将所输入数据存入内表以供程序使用,不过Select-Options的功能有一定的局限 ...

  6. 3.0之后在LinearLayout里增加分割线

    android:divider="@drawable/shape"<!--分割线图片--> android:showDividers="middle|begi ...

  7. Hibernate框架的基本搭建(一个小的java project的测试向数据库中插入和查询数据的功能)

    Hibernate介绍:Hibernate是一种“对象-关系型数据映射组件”,它使用映射文件将对象(object)与关系型数据(Relational)相关联,在Hibernate中映射文件通常以&qu ...

  8. 基于Retrofit+RxJava的Android分层网络请求框架

    目前已经有不少Android客户端在使用Retrofit+RxJava实现网络请求了,相比于xUtils,Volley等网络访问框架,其具有网络访问效率高(基于OkHttp).内存占用少.代码量小以及 ...

  9. Swift中的类和结构体的相同点与不同点

     相同点: 1.都是有内部变量和函数 2.都可以有内部下标方式去取属性 3.都可以有初始化函数 4.都可以用协议   不同点: 1.类有继承 2.类可以多重引用 3.类有析构  

  10. IOS组件绑定无效错误

    报错的原因:界面按钮事件没有绑定到源代码或者相关的代码被注释了.比如你的button组件以及绑定到IBOutlet,但是viewcontrol.m上没有相关的代码,就会出现异常.