FFT模板
我终于下定决心学习FFT了。
orzCHX,得出模板:
#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
const double PI=acos(-1.0);
struct FFT {
struct cox {
double r,i;
cox(double _r=0.0,double _i=0.0) {r=_r;i=_i;}
cox operator + (const cox& b) const {return cox(r+b.r,i+b.i);}
cox operator - (const cox& b) const {return cox(r-b.r,i-b.i);}
cox operator * (const cox& b) const {return cox(r*b.r-i*b.i,r*b.i+i*b.r);}
}f[maxn];
int len;
void init(int* A,int Len,int L) {
len=L;rep(i,,Len-) f[i]=cox(A[Len-i-],);
}
void cal(int tp) {
int j=len>>;
rep(i,,len-) {
if(i<j) swap(f[i],f[j]);int k=len>>;
while(j>=k) j-=k,k>>=;j+=k;
}
double lm=-*tp*PI;
for(int i=;i<=len;i<<=) {
cox wn(cos(lm/i),sin(lm/i));
for(int j=;j<len;j+=i) {
cox w(,);
for(int k=j;k<j+(i>>);k++) {
cox u=f[k],v=w*f[k+(i>>)];
f[k]=u+v;f[k+(i>>)]=u-v;w=w*wn;
}
}
}
if(tp<) rep(i,,len-) f[i].r/=len;
}
}a,b;
void mul(int* A,int* B,int L1,int L2,int& L,int* ans) {
L=;while(L<L1<<||L<L2<<) L<<=;
a.init(A,L1,L);b.init(B,L2,L);
a.cal();b.cal();
rep(i,,L-) a.f[i]=a.f[i]*b.f[i];
a.cal(-);rep(i,,L-) ans[i]=int(a.f[i].r+0.5);
}
int A[maxn],B[maxn],ans[maxn];
int main() {
int L1=read()+,L2=read()+,L;
rep(i,,L1-) A[i]=read();
rep(i,,L2-) B[i]=read();
mul(A,B,L1,L2,L,ans);
while(L>L1+L2-&&!ans[L-]) L--;
dwn(i,L-,) printf("%d ",ans[i]);
return ;
}
FFT模板的更多相关文章
- 再写FFT模板
没什么好说的,今天又考了FFT(虽然不用FFT也能过)但是确实有忘了怎么写FFT了,于是乎只有重新写一遍FFT模板练一下手了.第一部分普通FFT,第二部分数论FFT,记一下模数2^23*7*17+1 ...
- HDU 1402 A * B Problem Plus (FFT模板题)
FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...
- FFT模板(多项式乘法)
FFT模板(多项式乘法) 标签: FFT 扯淡 一晚上都用来捣鼓这个东西了...... 这里贴一位神犇的博客,我认为讲的比较清楚了.(刚好适合我这种复数都没学的) http://blog.csdn.n ...
- hdu1402(大数a*b&fft模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 给出两个长度1e5以内的大数a, b, 输出 a * b. 思路: fft模板 详情参 ...
- P1919 【模板】A*B Problem升级版 /// FFT模板
题目大意: 给定l,输入两个位数为l的数A B 输出两者的乘积 FFT讲解 这个讲解蛮好的 就是讲解里面贴的模板是错误的 struct cpx { double x,y; cpx(double _x= ...
- fft模板 HDU 1402
// fft模板 HDU 1402 #include <iostream> #include <cstdio> #include <cstdlib> #includ ...
- [hdu1402]大数乘法(FFT模板)
题意:大数乘法 思路:FFT模板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- UOJ#34 FFT模板题
写完上一道题才意识到自己没有在博客里丢过FFT的模板-- 这道题就是裸的多项式乘法,可以FFT,可以NTT,也可以用Karasuba(好像有人这么写没有T),也可以各种其他分治乘法乱搞-- 所以我就直 ...
- 【bzoj2179】FFT快速傅立叶 FFT模板
2016-06-01 09:34:54 很久很久很久以前写的了... 今天又比较了一下效率,貌似手写复数要快很多. 贴一下模板: #include<iostream> #include& ...
随机推荐
- 每天一个脚本解析day1==》《service xxxxx status》之service脚本解析
vim /sbin/service #!/bin/sh . /etc/init.d/functions #读取环境变量. VERSION="$(basename $0) ver. 0. ...
- Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- iOS 中的第三方库管理工具
xcode没有android studio中的gradle进行第三方库管理,但是有第三方的库管理工具CocoaPods,https://github.com/CocoaPods/CocoaPods/w ...
- apache AllowEncodedSlashes 允许URL中对路径分隔符进行编码
2013年11月29日 10:35:32 情景: 你想通过在当前的URL中记录来源页面的URL,以便处理完请求后再跳转回来源页: http://www.example1.com/refer/http: ...
- Windows下使用命令行设置ip地址的DNS服务器
使用命令行或者编写bat批处理设置网络连接的IP地址以及DNS服务器地址有时候会比手动更加方便,IP地址和DNS的设置一般是配合的,常用到的几个状态是: 1.IP地址动态获取,DNS也动态 2.IP地 ...
- Java for LeetCode 073 Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 解题思路: ...
- 19.python笔记之Rabbitmq
RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列(MQ)是一种应用程序 ...
- dbVisualizer连接mysql
- EZ的间谍网络(codevs 4093)
由于外国间谍的大量渗入,学校安全正处于高度的危机之中.YJY决定挺身而作出反抗.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他们就愿意交出手 ...
- 天使之城(codevs 2821)
2821 天使之城 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 天使城有一个火车站,每辆火车 ...