#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
 题解:
  化简式子
  也就是求  
  

后面这个只需将B数组倒置,进行卷积,出来就是一段连续的位置是k = 0……n-1,所有情况

  代码来自huyifan

#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAXN 300000
typedef long long LL;
const long long P=50000000001507329LL;
const int G=; LL mul(LL x,LL y){
return (x*y-(LL)(x/(long double)P*y+1e-)*P+P)%P;
}
LL qpow(LL x,LL k,LL p){
LL ret=;
while(k){
if(k&) ret=mul(ret,x);
k>>=;
x=mul(x,x);
}
return ret;
} LL wn[];
void getwn(){
for(int i=; i<=; ++i){
int t=<<i;
wn[i]=qpow(G,(P-)/t,P);
}
} int len;
void NTT(LL y[],int op){
for(int i=,j=len>>,k; i<len-; ++i){
if(i<j) swap(y[i],y[j]);
k=len>>;
while(j>=k){
j-=k;
k>>=;
}
if(j<k) j+=k;
}
int id=;
for(int h=; h<=len; h<<=) {
++id;
for(int i=; i<len; i+=h){
LL w=;
for(int j=i; j<i+(h>>); ++j){
LL u=y[j],t=mul(y[j+h/],w);
y[j]=u+t;
if(y[j]>=P) y[j]-=P;
y[j+h/]=u-t+P;
if(y[j+h/]>=P) y[j+h/]-=P;
w=mul(w,wn[id]);
}
}
}
if(op==-){
for(int i=; i<len/; ++i) swap(y[i],y[len-i]);
LL inv=qpow(len,P-,P);
for(int i=; i<len; ++i) y[i]=mul(y[i],inv);
}
}
void Convolution(LL A[],LL B[],int n){
for(len=; len<(n<<); len<<=);
for(int i=n; i<len; ++i){
A[i]=B[i]=;
} NTT(A,); NTT(B,);
for(int i=; i<len; ++i){
A[i]=mul(A[i],B[i]);
}
NTT(A,-);
}
int t,nn,m;
LL a[MAXN],b[MAXN];
LL ans,MAX;
int main()
{ getwn();
scanf("%d",&t); while(t--)
{ MAX=; ans=; scanf("%d",&nn); for(int i=;i<nn;i++)
{
scanf("%lld",&a[i]);
ans+=a[i]*a[i];
} for(int i=;i<nn;i++)
{
scanf("%lld",&b[nn - i - ]);
ans+=b[nn - i - ]*b[nn - i - ];
} for(int i=;i<nn;i++)
{
a[i+nn]=a[i];
b[i+nn]=;
} Convolution(a,b,*nn); for(int i=nn;i<*nn;i++)
{
MAX=max(MAX,a[i]);
} printf("%lld\n",ans-*MAX); } return ;
}

hihocode #1388 : Periodic Signal NTT的更多相关文章

  1. hihocoder #1388 : Periodic Signal NTT&FFT

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

  2. hihocoder #1388 : Periodic Signal fft

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

  3. 【hihocoder#1388】Periodic Signal NTT

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

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

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

  5. hihoCoder 1388 Periodic Signal(FFT)

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

  6. hihoCoder #1388 : Periodic Signal

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

  7. 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 ...

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

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

  9. hihocoder1388 Periodic Signal

    FFT 就可以了 比赛时候没时间做了 #include<bits/stdc++.h> using namespace std; typedef long long ll; const in ...

随机推荐

  1. 九度oj 题目1020:最小长方形

    题目描述:     给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在内.长方形框的边分别平行于x和y坐标轴,点落在边上也算是被框在内. 输入: 测试输入 ...

  2. PHP中file_put_contents追加和换行的实现方法

    在PHP的一些应用中需要写日志或者记录一些信息,这样的话.可以使用fopen(),fwrite()以及 fclose()这些进行操作.也可以简单的使用file_get_contents()和file_ ...

  3. 七牛云杜江华:让云 + AI 成为企业服务的标配

    12 月 5-6 日,2018 创业邦 100 未来领袖峰会暨创业邦年会(以下简称「创业邦 100 未来领袖峰会」)在北京国家会议中心举行.12 月 5 日下午,七牛云执行副总裁杜江华在企业服务论坛上 ...

  4. 【DFS序+单点修改区间求和】POJ 3321 Apple Tree

    poj.org/problem?id=3321 [题意] 给一棵n个节点的树,每个节点开始有一个苹果,m次操作 1.将某个结点的苹果数异或 1 2.查询一棵子树内的苹果数 #include<io ...

  5. Cstring中GetBuffer()方法的主要作用

    摘自:http://bbs.csdn.net/topics/310247836 GetBuffer()主要作用是将字符串的缓冲区长度锁定   CString::GetBuffer有两个重载版本: (1 ...

  6. SHOWMODALDIALOG表单提交时禁止打开新窗口

    前提条件:showmodaldialog中有表单form.当action="#"的时候,提交表单,不会打开新窗口,但这种#自提有时不能用,#是本页面完整的带参数的url,如果表单中 ...

  7. Count on a tree(bzoj 2588)

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  8. hdu 1717

    小数化分数2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  9. 模拟用户登录-SpringMVC+Spring+Mybatis整合小案例

    1. 导入相关jar包 ant-1.9.6.jarant-launcher-1.9.6.jaraopalliance.jarasm-5.1.jarasm-5.2.jaraspectj-weaver.j ...

  10. FireDac心得

    usesFireDAC.Phys.MySQL, FireDAC.Stan.Def, FireDAC.DApt, FireDAC.Comp.Client, FireDAC.Comp.UI, FireDA ...