hihocode #1388 : Periodic Signal NTT
#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的更多相关文章
- hihocoder #1388 : Periodic Signal NTT&FFT
传送门:hihocoder #1388 : Periodic Signal 先来几个大牛传送门: (模板) NTT long long 版 解法一:因为我们知道FFT会精度不够,所以坚持用NTT,但 ...
- hihocoder #1388 : Periodic Signal fft
题目链接: https://hihocoder.com/problemset/problem/1388 Periodic Signal 时间限制:5000ms内存限制:256MB 问题描述 Profe ...
- 【hihocoder#1388】Periodic Signal NTT
题目链接:http://hihocoder.com/problemset/problem/1388?sid=974337 题目大意:找出一个$k$,使得$\sum_{i=0}^{n-1}(A_{i}- ...
- hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)
时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device w ...
- hihoCoder 1388 Periodic Signal(FFT)
[题目链接] http://hihocoder.com/problemset/problem/1388 [题目大意] 给出A数列和B数列,求下图式子: [题解] 我们将多项式拆开,我们可以得到固定项A ...
- hihoCoder #1388 : Periodic Signal
NTT (long long 版) #include <algorithm> #include <cstring> #include <string.h> #inc ...
- 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 ...
- hihoCoder1388 Periodic Signal(2016北京网赛F:NTT)
题目 Source http://hihocoder.com/problemset/problem/1388 Description Profess X is an expert in signal ...
- hihocoder1388 Periodic Signal
FFT 就可以了 比赛时候没时间做了 #include<bits/stdc++.h> using namespace std; typedef long long ll; const in ...
随机推荐
- 九度oj 题目1173:查找
题目描述: 输入数组长度 n 输入数组 a[1...n] 输入查找个数m 输入查找数字b[1...m] 输出 YES or NO 查找有则YES 否则NO . 输入: 输入有多组数据. ...
- 虚拟机vmnet0、vmnet1和vmnet8的区别 虚拟网卡概述
vmnet0,实际上就是一个虚拟的网桥 vmnet0,实际上就是一个虚拟的网桥,这个网桥有很若干个端口,一个端口用于连接你的Host,一个端口用于连接你的虚拟机,他们的位置是对等的,谁也不是谁的网关. ...
- EC++学习笔记(三) 资源管理
条款13:以对象管理资源 资源包括:动态分配内存.文件描述符.网络socket.互斥锁.数据库连接 把资源放进对象内,可以依赖c++的"析构函数自动调用机制"确保资源被释放智能指针 ...
- TYVJ3680 找妹子
时间: 1000ms / 空间: 1200KiB / Java类名: Main 背景 本题由 @fjzzq2002 提供,已奖励20金币. 描述 sps是zzq的好伙伴. sps一天叫来了许多个妹子. ...
- Perl语言入门--4--函数
1.chop函数:删除标量变量或数组中每个字符的最后一个字 举个栗子: #!/usr/bin/perl $v = 'Flowers'; $r = chop($v); print "$v (w ...
- Java学习:二 基础
2014 - 5 - 22 上午 Java对大小写敏感. 点号(.)用于调用方法,Java的能用语法是: object.method(parameters); Java中的常量声明使用fina ...
- vba功能语句
VBA语句集(第1辑) 定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明Option Private Module '标记模块为私有,仅对同一工程中其它模块有用,在宏 ...
- 生成PDF文档
byte[] buffer = context.Response.Clear(); context.Response.ClearHeaders(); context.Response.ClearCon ...
- Codeforces 487B Strip (ST表+线段树维护DP 或 单调队列优化DP)
题目链接 Strip 题意 把一个数列分成连续的$k$段,要求满足每一段内的元素最大值和最小值的差值不超过$s$, 同时每一段内的元素个数要大于等于$l$, 求$k$的最小值. 考虑$DP$ 设$ ...
- Python使用eval强制转换字符串为字典时报错:File "<string>", line 1, in <module> NameError: name 'nan' is not defined
文本中保存的内容为: { 'QQQ': [0.067, 0.167, 0.2, 0.033, 0.233, 0.267, 0.1, 0.133], 'TTT': [0.5, 0.375, 0.25, ...