传送门:hihocoder #1388 : Periodic Signal

先来几个大牛传送门:  (模板) NTT long long 版

解法一:因为我们知道FFT会精度不够,所以坚持用NTT,但是模数不够大,然后就一直GG,看来我们的搜索姿势也有问题,居然没有搜到上面大神的板子,真的是GG

http://www.cnblogs.com/WABoss/p/5903927.html

/**************************************************************
Problem:
User: youmi
Language: C++
Result: Accepted
Time:
Memory:
****************************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <cmath>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#define zeros(a) memset(a,0,sizeof(a))
#define ones(a) memset(a,-1,sizeof(a))
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scs(a) scanf("%s",a)
#define sclld(a) scanf("%I64d",&a)
#define pt(a) printf("%d\n",a)
#define ptlld(a) printf("%I64d\n",a)
#define rep(i,from,to) for(int i=from;i<=to;i++)
#define irep(i,to,from) for(int i=to;i>=from;i--)
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define lson (step<<1)
#define rson (lson+1)
#define eps 1e-6
#define oo 0x3fffffff
#define TEST cout<<"*************************"<<endl
const double pi=*atan(1.0); using namespace std;
typedef long long ll; int n;
const ll P = 50000000001507329LL; //190734863287 * 2 ^ 18 + 1
//const ll P = 1004535809LL; //479 * 2 ^ 21 + 1
//const ll P = 1004535809; // 119 * 2 ^ 23 + 1
const int N = << ;
const int G = ;
int len;
ll A[N],B[N];
long long a[N],b[N],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);
}
}
void work()///卷积,点乘,插值
{
NTT(a,len,);
NTT(b,len,);
for(int i=;i<len;i++)
a[i]=mul(a[i],b[i]);
NTT(a,len,-);
}
ll solve()
{
zeros(a);
zeros(b);
rep(i,,n-)
a[i]=A[i];
rep(i,,n-)
b[i]=B[i];
reverse(b,b+n);
work();
ll ans=;
rep(i,,n-)
a[i]+=a[i+n];
rep(i,,n-)
ans=max(ans,*a[i]);
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T_T;
scanf("%d",&T_T);
getwn();
for(int kase=;kase<=T_T;kase++)
{
sc(n);
len=;
while(len<=*n)
len<<=;
rep(i,,n-)
cin>>A[i];
rep(i,,n-)
cin>>B[i];
ll temp=;
rep(i,,n-)
temp+=A[i]*A[i];
rep(i,,n-)
temp+=B[i]*B[i];
ll ans=solve();
ans=temp-ans;
cout<<(ans)<<endl;
}
}

解法二:这个解法确实很漂亮,比赛的时候一直徘徊找一个更大的 模数,然后就GG了,http://www.cnblogs.com/smartweed/p/5903838.html

解法三:其实这种解法我们也尝试了,队友说NTT搞了那么久,说明暴力应该可以,不过最后只剩几分钟来不及找到合适的循环次数,http://www.cnblogs.com/cshg/p/5905398.html

hihocoder #1388 : Periodic Signal NTT&FFT的更多相关文章

  1. hihoCoder 1388 Periodic Signal(FFT)

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

  2. hihocoder #1388 : Periodic Signal fft

    题目链接: https://hihocoder.com/problemset/problem/1388 Periodic Signal 时间限制:5000ms内存限制:256MB 问题描述 Profe ...

  3. hihocode #1388 : Periodic Signal NTT

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

  4. hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)

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

  5. hihoCoder #1388 : Periodic Signal

    NTT (long long 版) #include <algorithm> #include <cstring> #include <string.h> #inc ...

  6. 【hihocoder#1388】Periodic Signal NTT

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

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

  8. hihocoder 1388 fft循环矩阵

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

  9. NTT&FFT(快速?变换)

    NTT&FFT 预先知识:无 我觉得我们可以从NTT/FFT讲起? 两个其实本质相同,都是求 多项式乘积 的算法 FFT \((x,y)\)指复数,我们可以不用管它 首先我们构造单位根\(\o ...

随机推荐

  1. 【position也可以很复杂】当弹出层遇上了鼠标定位(上)

    前言 周五时同事有一个关于弹出层的问题没有解决,但是面临下班问题,我有点不舒服,便叫回去周六过来解决,但是上周六病了,所以请了个假,于是故事发生啦.... 今天上班时候,组员们卡到了那个地方,然后结果 ...

  2. 轻松掌握:JavaScript享元模式

    享元模式 在JavaScript中,浏览器特别是移动端的浏览器分配的内存很有限,如何节省内存就成了一件非常有意义的事情.节省内存的一个有效方法是减少对象的数量. 享元模式(Flyweight),运行共 ...

  3. Atitti.java android反编译解决方案-----虚拟机方案

    Atitti.java android反编译解决方案-----虚拟机方案 哈哈,终极解决方案是虚拟机...c++也可以反编译为汇编代码,但无需担心,因为读懂汇编太麻烦..只要不能拿到c++源码就可.. ...

  4. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q25-Q28)

    Question 25 You are designing a SharePoint 2010 farm in your organization. You need to design the li ...

  5. Android - 模块添加与编译

    Android5.1 Ubuntu14.04 Android系统编译依靠.mk文件 添加google服务 我们自己的ROM里没有google服务 完整的google包里包含google框架和各种服务, ...

  6. IOS开发之功能模块--给任意的UIView添加点击事件

    前言:好久没写博客,今天来一波.我在实际项目开发中,会遇到这样功能需求:我已经搭好了iOS的UI界面,但是很多界面的子View用了UIView,然后这些UIView中用了UILabel和UIImage ...

  7. 安装php扩展

    下面我以soap安装为例子 cd /home/zhangy/php-5.2.6/ext/soap /usr/local/php/bin/phpize#确定php-config文件在不在,调用php-c ...

  8. composer快速入门

    composer.json 文件内容定义 ====================================================={ "require":{ &q ...

  9. MetaWeblog API调用

    http://rpc.cnblogs.com/metaweblog/webenh 在网上闲逛,突然对博客的接口感兴趣,经考察,多数博客都对metaWeblog Api 提供了支持,虽然windows ...

  10. spring的定时任务配置

    本文来源于:http://myspace1916.iteye.com/blog/1570707 也可参考:http://www.oschina.net/question/8676_9032 (个人只是 ...