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 ...
随机推荐
- u-boot-2015.01在tq2440上的初步移植
作者: 彭东林 邮箱: pengdonglin137@163.com QQ: 405728433 开发板: tq2440 工具: Win7 + VMware + Debian6 ...
- 设计模式之空对象模式(php实现)
github地址:https://github.com/ZQCard/design_pattern /** * 在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象 ...
- IntelliJ IDEA 配合 Maven 的一些技巧:Profiles
环境 IntelliJ IDEA 2017.1 Maven 3.3.9 Nexus 3.2.1 学习前提 了解 Maven 配置的基本用法 了解私有仓库,比如 nexus 的一些概念 强烈建议把 Ma ...
- 深入理解ES6里的promise
一.ES6 Promise是什么? 复杂的概念先不讲,我们先简单粗暴地把Promise用一下,有个直观感受.那么第一个问题来了,Promise是什么呢?是一个类?对象?数组?函数? 别猜了,直接打印出 ...
- Exception thrown in catch and finally clause
Based on reading your answer and seeing how you likely came up with it, I believe you think an " ...
- Hadoop之Flume详解
1.日志采集框架Flume 1.1 Flume介绍 Flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统. Flume可以采集文件,socket数据包等各种形式源数据,又可以将采集到 ...
- 13 Basic Cat Command Examples in Linux
FROM: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/ The cat (short for “concatenate ...
- 【高级功能】使用 Ajax
Ajax 是现代Web 应用程序开发的一项关键工具.它让你能向服务器异步发送和接收数据,然后用 Javascript 解析. Ajax 是 Asynchronous JavaScript and XM ...
- 两种“新型”的javaweb后门(jspx和Java Logger)
利用这个可以突破st2下 强制jsp跳转login.jsp 利用jspx解决jsp后缀被限制拿shell - Hack Blog | 黑客博客http://www.hackblog.cn/post ...
- 最美应用-从Android研发project师的角度之[最美时光]
最美应用-从Android研发project师的角度之最美时光 @author ASCE1885的 Github 简书 微博 CSDN 近期发现最美应用这样一个站点.它会定期推介一些非常有意思的app ...