NTT (long long 版)

#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 INF 0x3ffffff using namespace std; typedef long long LL;
const int N = ;
const LL P = 180143985094819841LL; //190734863287 * 2 ^ 18 + 1
const int G = ;
LL wn[]; 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;
}
void getwn() {
for(int i = ; i <= ; ++i) {
int t = << i;
wn[i] = qpow(G, (P - ) / t, P);
}
}
void change(LL *y, int len) {
for(int i = , j = len / ; i < len - ; ++i) {
if(i < j) swap(y[i], y[j]);
int k = len / ;
while(j >= k) {
j -= k;
k /= ;
}
j += k;
}
}
void NTT(LL *y, int len, int on) {
change(y, len);
int id = ;
for(int h = ; h <= len; h <<= ) {
++id;
for(int j = ; j < len; j += h) {
LL w = ;
for(int k = j; k < j + h / ; ++k) {
LL u = y[k];
LL t = mul(y[k+h/], w);
y[k] = u + t;
if(y[k] >= P) y[k] -= P;
y[k+h/] = u - t + P;
if(y[k+h/] >= P) y[k+h/] -= P;
w = mul(w, wn[id]);
}
}
}
if(on == -) {
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);
}
}
LL a[], b[];
LL x[N], y[N], num[N];
void mul(LL a[], LL b[], LL c[], int len)
{
NTT(a, len, );
NTT(b, len, );
for (int i = ; i < len; i++)
{
c[i] = mul(a[i], b[i]);
}
NTT(c, len, -);
}
void init(){
memset(num,,sizeof(num));
memset(x,,sizeof(x));
memset(y,,sizeof(y));
}
int main()
{
int T;
scanf("%d",&T);
getwn(); //!!!
LL suma,sumb;
while(T--)
{
int n;
suma=;sumb=; //suma为A[]平方和
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++){
x[i] = a[i];
}
for(int i = ;i < n;i++){
y[i] = b[n-i-];
}
mul(x, y, num, len); //NTT
LL ret=num[n-];
for(int i=;i<n-;i++) {
ret=max(ret,num[i]+num[i+n]);
}
LL ans=suma+sumb-*ret;
cout<< ans<<endl;
}
return ;
}

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

  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 ( 2016 acm 北京网络赛 F题)

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

  4. hihoCoder 1388 Periodic Signal(FFT)

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

  5. hihocode #1388 : Periodic Signal NTT

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

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

  7. hihocoder 1388 fft循环矩阵

    #1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal proce ...

  8. 【hihocoder#1388】Periodic Signal NTT

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

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

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

随机推荐

  1. SQL Server判断某个字段是否包含中文/英文字符/数字

    原文:SQL Server判断某个字段是否包含中文/英文字符/数字 因最近在清理系统中的脏数据,需要查询某个字段是否包含中文/英文字符/数字的数据, 比较简单,仅以此篇博客做一个简单总结,方便以后查阅 ...

  2. python xml与字典的相互转换

    def trans_xml_to_dict(xml): """ 将微信支付交互返回的 XML 格式数据转化为 Python Dict 对象 :param xml: 原始 ...

  3. C++ 模板应用 实现一个Queue 队列

    #include<iostream> using namespace std; template <typename T> class Queue { public: Queu ...

  4. Manthan, Codefest 16 D. Fibonacci-ish(暴力)

    题目链接:点击打开链接 题意:给你n个数, 问最长的题目中定义的斐波那契数列.  思路:枚举開始的两个数, 由于最多找90次, 所以能够直接暴力, 用map去重.  注意, 该题卡的时间有点厉害啊. ...

  5. ElastcSearch的Mapping映射建立

    根据oracle的字段来建立ElasticSearch的Mapping public class Start { private static Logger log = LoggerFactory.g ...

  6. Win7梦幻桌面字体有问题怎么办

      1:首先,下载我提供的压缩文件,解压"Win7中文界面梦幻桌面补丁",得到若干文件,这时可能需要管理员权限,详细的压缩包里有说明.      2: 然后在桌面上单击右键,可以发 ...

  7. List装form

    List<MemberPrivilegeForm> formlist = new ArrayList<MemberPrivilegeForm>(); int status = ...

  8. VS中c++文件调用c 函数 ,fatal error C1853 预编译头文件来自编译器的早期版本号,或者预编译头为 C++ 而在 C 中使用它(或相反)

    出现错误:error C1853: "Debug\ConsoleApplication1.pch"预编译头文件来自编译器的早期版本号.或者预编译头为 C++ 而在 C 中使用它(或 ...

  9. 渐进式 JPEG (Progressive JPEG)来提升用户体验

    1.概述 jpg格式分为:Baseline JPEG(标准型)和Progressive JPEG(渐进式).两种格式有相同尺寸以及图像数据,扩展名也是相同的,唯一的区别是二者显示的方式不同. Base ...

  10. json对象和json字符串之间的转换-JavaScript实现

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...