题目链接:http://hihocoder.com/problemset/problem/1388?sid=974337

题目大意:找出一个$k$,使得$\sum_{i=0}^{n-1}(A_{i}-B_{i+k \quad mod \quad n})^{2}$最小

把那个式子拆开..得到:

$\sum _{i=0}^{n-1} A_{i}^{2}+\sum_{i=0}^{n-1}B_{i}^{2}-\sum_{i=0}^{n-1}2*A_{i}*B_{i+k}$

前面的两个和式是定值,所以最小化后面的值,后面这个是个卷积的形式,所以把$B$翻转硬上就好了...

然后问题就来了..FFT了之后貌似精度爆炸了..然后就写NTT..然后又炸了..发现模数规模不够..于是查表换了个巨大的模数,然后又炸了..

然后去看题解,意识到一个严重的问题..相乘爆longlong...然后又学习了TA爷的$O(1)$快速乘..

附上一张神表:折跃

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
#define LL long long
inline LL read()
{
LL x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define P (LL)((29LL<<57)+1)
#define G 3
#define MAXN 800100
int T,N,len;
LL Wn[30],A[MAXN],B[MAXN],C[MAXN],wn[MAXN],a[MAXN],b[MAXN];
inline LL Mul(LL x,LL y) {return (x*y-(LL)(x/(long double)P*y+1e-3)*P+P)%P;}
inline LL Pow(LL x,LL y)
{
LL re=1;
for (LL i=y; i; i>>=1,x=Mul(x,x))
if (i&1) re=Mul(re,x);
return re;
}
inline LL Inv(LL x) {return Pow(x,P-2);}
inline void Prework()
{
len=1;
while (len<(N<<1)) len<<=1;
for (int i=0; i<N; i++) A[i]=a[i];
for (int i=0; i<N; i++) B[i]=b[N-1-i];
for (int i=N; i<len; i++) A[i]=0;
for (int i=N; i<len; i++) B[i]=0;
// for (int i=0; i<len; i++) printf("%I64d ",A[i]); puts("");
// for (int i=0; i<len; i++) printf("%I64d ",B[i]); puts("");
for (int i=0; i<=28; i++) wn[i]=Pow(G,(P-1)/(1<<i));
}
inline void Rader(LL *x)
{
for (int i=1,j=len>>1,k; i<len-1; i++)
{
if (i<j) swap(x[i],x[j]);
k=len>>1;
while (j>=k) j-=k,k>>=1;
if (j<k) j+=k;
}
}
inline void DFT(LL *x,int opt)
{
Rader(x);
for (int h=2,id=0; h<=len; h<<=1)
{
LL Wn=wn[++id];
for (int i=0; i<len; i+=h)
{
LL W=1;
for (int j=i; j<i+h/2; j++)
{
LL u=x[j]%P,t=Mul(W,x[j+h/2]);
x[j]=(u+t)%P; x[j+h/2]=(u-t+P)%P;
W=Mul(W,Wn);
}
}
}
if (opt==-1)
{
for (int i=1; i<len/2; i++) swap(x[i],x[len-i]);
for (int i=0; i<len; i++)
x[i]=Mul(x[i],Inv(len));
}
}
inline void NTT(LL *A,LL *B)
{
DFT(A,1); DFT(B,1);
for (int i=0; i<len; i++) C[i]=Mul(A[i],B[i]);
DFT(C,-1);
}
int main()
{
T=read();
while (T--)
{
N=read();
LL ans=0;
for (int i=0; i<=N-1; i++) a[i]=read();
for (int i=0; i<=N-1; i++) b[i]=read();
for (int i=0; i<=N-1; i++) ans+=(LL)a[i]*a[i];
for (int i=0; i<=N-1; i++) ans+=(LL)b[i]*b[i];
Prework();
NTT(A,B);
LL mx=C[N-1];
for (int i=0; i<N-1; i++) mx=max(mx,C[i]+C[i+N]);
// for (int i=0; i<len; i++) printf("%I64d ",C[i]); puts("");
printf("%lld\n",ans-2*mx);
}
return 0;
}

  

【hihocoder#1388】Periodic Signal NTT的更多相关文章

  1. hihocoder #1388 : Periodic Signal NTT&FFT

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

  2. hihocode #1388 : Periodic Signal NTT

    #1388 : Periodic Signal   描述 Profess X is an expert in signal processing. He has a device which can ...

  3. 【hihocoder 1298】 数论五·欧拉函数

    [题目链接]:http://hihocoder.com/problemset/problem/1298 [题意] [题解] 用欧拉筛法; 能够同时求出1..MAX当中的所有质数和所有数的欧拉函数的值; ...

  4. 【hihocoder 1297】数论四·扩展欧几里德

    [题目链接]:http://hihocoder.com/problemset/problem/1297 [题意] [题解] 问题可以转化为数学问题 即(s1+v1*t)%m == (s2+v2*t)% ...

  5. 【hihocoder 1296】数论三·约瑟夫问题

    [题目链接]:http://hihocoder.com/problemset/problem/1296 [题意] [题解] [Number Of WA] 0 [完整代码] #include <b ...

  6. 【hihocoder 1295】Eular质数筛法

    [题目链接]:http://hihocoder.com/problemset/problem/1295 [题意] [题解] 可以在O(N)的复杂度内求出1..N里面的所有素数; 当然受空间限制,N可能 ...

  7. 【hihocoder 1287】 数论一·Miller-Rabin质数测试

    [题目链接]:http://hihocoder.com/problemset/problem/1287 [题意] [题解] 取的底数必须是小于等于n-1的; 那12个数字能通过2^64以内的所有数字; ...

  8. 【hihocoder 1333】平衡树·Splay2

    [题目链接]:http://hihocoder.com/problemset/problem/1333 [题意] [题解] 伸展树; 要求提供操作: 1.插入一个元素,两个权值,id作为查找的比较权值 ...

  9. 【hihocoder 1329】平衡树·Splay(Splay做法)

    [题目链接]:http://hihocoder.com/problemset/problem/1329 [题意] [题解] 插入操作:-,记住每次插入之后都要把它放到根节点去就好; 询问操作:对于询问 ...

随机推荐

  1. [SQL注入1]From SQL injection to Shell

    第一次写,希望大神们多指点. 对于刚接触WEB渗透测试这块的朋友们,很希望能有个平台可以练习.网络上有不少,十大渗透测试演练系统,我这里推荐一个在10以外,适合初学者一步一步进步的平台PENTESTE ...

  2. MySql 学习笔记 (派生表)

    派生表也是一种子查询那么它出现在 select * from ( select * from b <--这个就是派生表啦 )派生表其实不是个好东西,在生产的时候他是可以通过索引来过滤的,但是一但 ...

  3. Qt使用MinGW编译,如何忽略警告

    Qt编译时经常出现以下警告: warning: unused parameter 'arg1' [-Wunused-parameter] warning: unused variable 'i' [- ...

  4. 以Crypto++实现RSA加解密二进制数据

    网上一大片讲怎么加解密字符串的,找了大半天也没找到讲加解密二进制数据的,于是自己研究了下,分享给大家. 加解密函数: #include <rsa.h> #include <randp ...

  5. poj1881:素因子分解+素数测试

    很好的入门题 先测试是否为素数,若不是则进行素因子分解,算法详见总结贴 miller robin 和pollard rho算法 AC代码 #include <iostream> #incl ...

  6. hdu1573:数论,线性同余方程组

    题目大意: 给定一个N ,m 找到小于N的  对于i=1....m,满足  x mod ai=bi  的 x 的数量. 分析 先求出 同余方程组 的最小解x0,然后 每增加lcm(a1...,am)都 ...

  7. xm学习笔记

    1关于静态网页的制作 html主要负责页面的结构+css页面的美观+js与用户的交互. 2html 有标签体的标签: <p></p>  <span></spa ...

  8. uiautomatorviewer 可以查看到android中的web 元素信息

    以知乎社区账号登录使用微博账号为例,使用uiautomatorviewer 可以定位到登录框.密码框,需要结合appium的inspector 1.genymotion 模拟器开启,模拟器安卓系统为4 ...

  9. Performance Tuning guide 翻译 || 前言

    CSDN 对格式支持比較弱,能够到http://user.qzone.qq.com/88285879/blog/1399382878 看一致的内容. 前言Preface 包含例如以下几个小节 l Au ...

  10. [RxJS] Sharing Streams with Share

    A stream will run with each new subscription added to it. This lesson shows the benefits of using sh ...