hihoCoder #1388 : Periodic Signal
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的更多相关文章
- 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 ( 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 ...
- hihocode #1388 : Periodic Signal NTT
#1388 : Periodic Signal 描述 Profess X is an expert in signal processing. He has a device which can ...
- 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 ...
- hihocoder 1388 fft循环矩阵
#1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal proce ...
- 【hihocoder#1388】Periodic Signal NTT
题目链接:http://hihocoder.com/problemset/problem/1388?sid=974337 题目大意:找出一个$k$,使得$\sum_{i=0}^{n-1}(A_{i}- ...
- hihoCoder1388 Periodic Signal(2016北京网赛F:NTT)
题目 Source http://hihocoder.com/problemset/problem/1388 Description Profess X is an expert in signal ...
随机推荐
- intel 硬盘加速技术
Intel Smart Response Technology 混合硬盘技术 Intel Rapid Storage Technology SERVER:
- 设计模式之过滤器模式(php实现)
/** * github地址:https://github.com/ZQCard/design_pattern * 过滤器模式(Filter Pattern)或标准模式(Criteria Patter ...
- docker部署 自动测试脚本记录
1 首先配置virtualenv, 并启动之,source py3env/bin/activate 2 安装各种必备包 ,如simplejson,chardet, configparser等 3 注意 ...
- flask的session解读及flask_login登录过程研究
#!/usr/bin/env python # -*- coding: utf-8 -*- from itsdangerous import URLSafeTimedSerializer from f ...
- Python中使用XMLRPC(入门)
一.简介 RPC是Remote Procedure Call的缩写,翻译成中文为:远程方法调用. 它是一种在本地机器上调用远端机器上的一个过程(方法)的技术,这个过程也被大家称为“分布式计算”,是为了 ...
- [转载]DLL劫持生成器 源码开放(纯WINDOWS SDK)+ 实例分析
本菜最近学习了什么DLL注入啊,hook啊(r3)的相关技术,觉得很好玩,于是深入发现还有DLL劫持这种东西觉得挺好玩的,加上最近看到各种木马分析报告中都还有发现有利用白加黑的现象.于是自己想找几个来 ...
- HDFS源码分析之UnderReplicatedBlocks(一)
http://blog.csdn.net/lipeng_bigdata/article/details/51160359 UnderReplicatedBlocks是HDFS中关于块复制的一个重要数据 ...
- [转]js模块化编程之彻底弄懂CommonJS和AMD/CMD!
原文: https://www.cnblogs.com/chenguangliang/p/5856701.html ------------------------------------------ ...
- 2017.9.15 postgres使用postgres_fdw实现跨库查询
postgres_fdw的使用参考来自:https://my.oschina.net/Kenyon/blog/214953 postgres跨库查询可以通过dblink或者postgres_fdw来完 ...
- 倍福TwinCAT(贝福Beckhoff)基础教程5.1 TwinCAT-2 运行可执行文件
个人认为这条命令做的参数比较混乱,PATHSTR是指可执行文件路径+最终文件名,DIRNAME是指可执行文件路径,最后COMNDLINE可有可无,是指带参数运行启动的文件 测试可以正常运行 ...