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 ...
随机推荐
- XCTest(一)
target: + 来添加target,可以选择工程中不同的target目标来添加 class: +来添加class, 可以按照不同的测试功能来分类,比如NormalFunctionTest, UI ...
- DOM系统学习-基础
DOM介绍 DOM介绍: D 网页文档 O 对象,可以调用属性和方法 M 网页文档的树型结构 节点: DOM将树型结构理解为由节点组成. 节点种类: 元素节点.文本节点.属性节点等 查找元 ...
- Enum枚举类使用集合
1.使用扩展方法使用枚举值对于的Description属性值 public static class EnumExtenstion { public static string GetDescript ...
- http://blog.csdn.net/i_bruce/article/details/39555417
http://blog.csdn.net/i_bruce/article/details/39555417
- zookeeper启动错误 ---- Unable to load database on disk
zk启动报错 解决办法,进入zkdata目录删除version-2下面的所有文件 参考: https://issues.apache.org/jira/browse/ZOOKEEPER-1546 [h ...
- 修改MySQL数据库存储位置datadir
2017-04-14 1.找到mysql安装目录,默认是C:/ProgramData/MySQL/MySQL Server 5.1,找到my.ini.如果这个文件没有,自己建一个my.ini. 2.假 ...
- How to get the edited text from itext in fabricjs
https://stackoverflow.com/questions/39286826/how-to-get-the-edited-text-from-itext-in-fabricjs http: ...
- 怪异恼人的java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream问题的解决
测试以前做的一个邮件发送类,出现以下问题: Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/m ...
- Visual Studio 外请版本号管理插件 - AnkhSVN
Visual Studio 外请版本号管理插件 - AnkhSVN 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致 ...
- MonoBehaviour.FixedUpdate 固定更新
function FixedUpdate () : void Description描述 This function is called every fixed framerate frame, if ...