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. linux命令行翻页

    在linux上面执行命令,若命令太多屏幕显示不完,通过Shift+pageup/pageDown来查看. putty连接linux后执行就不存在这个问题.

  2. js封装的一行半显示省略号。(字数自由控制)

    $(function() { //控制一行半隐藏 (function ($) { $.fn.displayPart = function (opts) { $(this).each(function ...

  3. 初试百度地图API

    第一次使用百度地图API来定位并显示,参照了官网2.1.0版本demo里的DemoApplication和LocationOverlayDemo两个类来写,整了半天显示一片空白(图一),然后郁闷了半天 ...

  4. Python中的*args和**kwargs的理解与用法

    一.简述 1.*args和**kwargs 这两个是python中方法的可变参数. 2.*args表示任何多个无名参数,它是一个tuple: 3.**kwargs表示关键字参数,它是一个dict.并且 ...

  5. running android lint has encountered a

    近期写学习android编程的的时候,每次保存.java文件的时候,总会跳出例如以下错误 这个错误不是属于程序错误,把它关掉对于编程没有不论什么影响,但每次见到这个就是不爽,希望大神可以解决一下,谢谢 ...

  6. Python 实现指定目录下 删除指定大小的文件

    import os, sys from stat import * BIG_FILE_THRESHOLD = 6000L #1000000L dict1 = {} # dict2 = {} # def ...

  7. 关于Laravel5.2在php5.3.6X和在php7.1.10下的内存溢出

    php5.3.6X是编译安装,在debug模式下,频繁报出内存泄露警告 php7.1.10下则不会有此错误. 顺便提下:测试发现ThinkPHP也不会有该内存泄露警告! 希望知道如何解决该问题的童鞋能 ...

  8. Python魔法师

    第一章:数据结构和算法 1.1 查找最大或者最小的n个元素 heapq 模块的两个函数 nlargest()  nsmallest() import heapq nums = [1, 8, 2, 23 ...

  9. 编辑器未包含main类型

    明明写了main函数,在运行的时候,却得到这样的结果. 解决方案: 重新建立一个项目,建立项目的过程中

  10. 不得不看的Java代码性能优化总结

    原文:https://blog.csdn.net/mr_smile2014/article/details/50112723 前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么 ...