#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. Codeforces Round #305 (Div. 2) D. Mike and Feet

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. oracle表空间,分区表,以及索引的总结

    表空间: Oracle的UNDOTBS01.DBF文件太大的解决办法 1..禁止undo tablespace自动增长 alter   database   datafile   'full_path ...

  3. 02-offsetLeft和offsetTop

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. BZOJ [HNOI2015]亚瑟王 ——期望DP

    发现每张卡牌最后起到作用只和是否打出去了有关. 而且每张牌打出去的概率和之前的牌打出去的情况有关. 所以我们按照牌的顺序进行DP. 然后记录$i$张牌中打出$j$张的概率,然后顺便统计答案. 直接对系 ...

  5. [luoguP4035] [JSOI2008]球形空间产生器(高斯消元)

    传送门 设球心的坐标为未知量 用最后一个点来表示球面到球心的距离,那么它和前n个式子相等 移项乱搞 最后高斯消元 #include <cmath> #include <cstdio& ...

  6. NOI2015 荷马史诗 【k-哈夫曼树】

    题目 追逐影子的人,自己就是影子 --荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛> ...

  7. 【leetcode】lower_bound

    int binary_search(const vector<int>& stones,int val){ int sz=stones.size(); ,r=sz-; while( ...

  8. 【数位DP】bnuoj 52813 J. Deciphering Oracles

    http://acm.bnu.edu.cn/v3/contest_show.php?cid=9208#problem/J [AC] #include<bits/stdc++.h> usin ...

  9. bzoj3609 [Heoi2014]人人尽说江南好 博弈

    [Heoi2014]人人尽说江南好 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 581  Solved: 420[Submit][Status][D ...

  10. Bzoj2007 [Noi2010]海拔(平面图最短路)

    2007: [Noi2010]海拔 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2742  Solved: 1318[Submit][Status] ...