【BZOJ2194】快速傅立叶之二

Description

请计算C[k]=sigma(a[i]*b[i-k]) 其中 k < = i < n ,并且有 n < = 10 ^ 5。 a,b中的元素均为小于等于100的非负整数。

Input

第一行一个整数N,接下来N行,第i+2..i+N-1行,每行两个数,依次表示a[i],b[i] (0 < = i < N)。

Output

输出N行,每行一个整数,第i行输出C[i-1]。

Sample Input

5
3 1
2 4
1 1
2 4
1 4

Sample Output

24
12
10
6
1

题解:如果我们将b数组反转,原式就变成了a[i]*b[n-i+k-1],发现满足卷积的形式,直接上FFT

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#define pi acos(-1.0)
using namespace std;
struct cp
{
double x,y;
cp (double a,double b){x=a,y=b;}
cp (){}
cp operator +(cp a) const {return cp(x+a.x,y+a.y);}
cp operator -(cp a) const {return cp(x-a.x,y-a.y);}
cp operator *(cp a) const {return cp(x*a.x-y*a.y,x*a.y+y*a.x);}
}n1[1<<20],n2[1<<20];
int ans[1<<20],n;
long long sum;
void init(cp *a,int len)
{
int i,j,t=0;
for(i=0;i<len;i++)
{
if(i>t) swap(a[i],a[t]);
for(j=(len>>1);(t^=j)<j;j>>=1);
}
}
void FFT(cp *a,int len,int f)
{
init(a,len);
int i,j,k,h;
cp u;
for(h=2;h<=len;h<<=1)
{
cp wn=cp(cos(f*2*pi/h),sin(f*2*pi/h));
for(j=0;j<len;j+=h)
{
cp w(1,0);
for(k=j;k<j+h/2;k++)
{
u=w*a[k+h/2],a[k+h/2]=a[k]-u,a[k]=a[k]+u,w=w*wn;
}
}
}
}
void work(cp *a,cp *b,int len)
{
FFT(a,len,1),FFT(b,len,1);
for(int i=0;i<len;i++) a[i]=a[i]*b[i];
FFT(a,len,-1);
for(int i=0;i<len;i++) ans[i]=int(a[i].x/len+0.5);
}
int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-')f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
int main()
{
n=rd();
int i,a,b,len=1;
while(len<2*n) len<<=1;
for(i=0;i<n;i++)
{
a=rd(),b=rd();
n1[i].x=a*1.0,n2[n-i-1].x=b*1.0;
}
work(n1,n2,len);
for(i=n-1;i<2*n-1;i++) printf("%d\n",ans[i]);
return 0;
}

【BZOJ2194】快速傅立叶之二的更多相关文章

  1. bzoj2194 快速傅立叶之二 ntt

    bzoj2194 快速傅立叶之二 链接 bzoj 思路 对我这种和式不强的人,直接转二维看. 发现对\(C_k\)贡献的数对(i,j),都是右斜对角线. 既然贡献是对角线,我们可以利用对角线的性质了. ...

  2. [bzoj2194]快速傅立叶之二_FFT

    快速傅立叶之二 bzoj-2194 题目大意:给定两个长度为$n$的序列$a$和$b$.求$c$序列,其中:$c_i=\sum\limits_{j=i}^{n-1} a_j\times b_{j-i} ...

  3. BZOJ2194:快速傅立叶之二(FFT)

    Description 请计算C[k]=sigma(a[i]*b[i-k]) 其中 k < = i < n ,并且有 n < = 10 ^ 5. a,b中的元素均为小于等于100的非 ...

  4. bzoj2194: 快速傅立叶之二

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  5. 2018.11.18 bzoj2194: 快速傅立叶之二(fft)

    传送门 模板题. 将bbb序列反过来然后上fftfftfft搞定. 代码: #include<bits/stdc++.h> #define ri register int using na ...

  6. bzoj千题计划256:bzoj2194: 快速傅立叶之二

    http://www.lydsy.com/JudgeOnline/problem.php?id=2194 相乘两项的下标 的 差相同 那么把某一个反过来就是卷积形式 fft优化 #include< ...

  7. BZOJ2194: 快速傅立叶之二(NTT,卷积)

    Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1776  Solved: 1055[Submit][Status][Discuss] Descript ...

  8. BZOJ2194 快速傅立叶之二 【fft】

    题目 请计算C[k]=sigma(a[i]*b[i-k]) 其中 k < = i < n ,并且有 n < = 10 ^ 5. a,b中的元素均为小于等于100的非负整数. 输入格式 ...

  9. BZOJ2194: 快速傅立叶之二 FFT_卷积

    Code: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring ...

随机推荐

  1. linux 下vi /vim 中文汉字乱码解决

    http://my.oschina.net/laserdance/blog/53474很多win下编译的配置文件(译码格式有utf8/gbk)上传到linux服务器上时打开汉字乱码 解决方法如下: 修 ...

  2. hihoCoder[Offer收割]编程练习赛1题目解析

    题目1 : 九宫 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi近期在教邻居家的小朋友小学奥数.而近期正好讲述到了三阶幻方这个部分,三阶幻方指的是将1~9不反 ...

  3. mysql数据库中不能插入0000-00-00 00:00:00日期数据(报错Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00')

    报错信息 SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' ...

  4. Laravel建站03--建立前台文章列表和文章详情

    经过了前面的开发环境搭建和数据库配置.git配置的准备工作以后,现在可以开始动作做点什么了. 先从简单的开始,那就先来个文章列表和详情页吧. 这期间可以学习到路由.建立model.controller ...

  5. C 语言 ioctl

    /* *@author cody *@date 2014-08-12 *@description */ /* #include <sys/ioctl.h> send control and ...

  6. Java并发编程(三):并发模拟(工具和Java代码介绍)

    并发模拟工具介绍 ① Postman : Http请求模拟工具 从图上我们可以看出,Postman模拟并发其实是分两步进行操作的.第一步:左边的窗口,在窗口中设置相关接口以及参数,点击运行进行第二步. ...

  7. atitit.Sealink2000国际海运信息管理系统

    atitit.Sealink2000国际海运信息管理系统 操作手册 目录 第一章 使用说明 第一节 系统登录 双击桌面的系统执行程序图标,进入选择数据库的对话框,如图1-1所示.选择相应的数据库后,点 ...

  8. Python学习笔记7:函数对象及函数对象作參数

    一.lambda函数 比如: fun1 = lambda x,y: x + y print fun1(3,4) 输出:7 lambda生成一个函数对象.该函数參数为x,y,返回值为x+y.函数对象赋给 ...

  9. cacheManager载入问题

    net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please pr ...

  10. Source Insight 项目简单使用说明

    SI(Source Insight) 是我一直写代码的好伙伴, 相信这强大的软件也是广大程序猿编写软件的利器. 正所谓" 工欲善其事, 必先利其器", 我们要学会利用这款软件. 先 ...