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& ...
随机推荐
- datetime中strftime和strptime用法
from datetime import * format = "%Y-%m-%d %H:%M:%S" a=datetime.now() day=a.day b=a.replace ...
- Scanner 和 String 类的常用方法
Scanner类是在jdk1.5 之后有了这个: 常用格式是: Scanner sc = new Scanner(System.in); 从以下版本开始: 1.5 构造方法摘要 Scanner(Fil ...
- Cocos2d 中的Sprite大小调整问题
以前用UIImageView,比如 UIImageView *view = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"b ...
- IOS多线程(NSThread)
1.创建方法 使用NSThread创建线程主要有两个个方法,分别如下 NSThread* myThread = [[NSThread alloc] initWithTarget:self sele ...
- Window环境下Python和Django的安装
转载地址:http://blog.csdn.net/haoni123321/article/details/7593821 1.下载python,本文使用python-2.7.2.msi 2.下载dj ...
- vs c++ 将string转换为double
可以用atof()这个函数,但是这个函数的参数是char*类型的,因此需将string类型强制转换,方法为在函数的参数中写成 const_cast<const char *>(str.c_ ...
- eclipse 反编译插件安装
1.下载jad.exe和jadclipse_3.3.0.jar 2.将jad.exe拷贝到%JAVA_HOME%bin目录下,将jadclipse_3.3.0.jar拷贝到eclipse的plug ...
- 爱情之路(codevs 2070)
题目描述 Description yh非常想念他的女朋友小y,于是他决定前往小y所在的那块大陆. 小y所在的大陆共有n个城市,m条双向路,每条路连接一个或两个城市.经过一条路ei需要耗费时间ti.此外 ...
- nginx 匹配.zip .apk 结尾的文件 直接下载
server { listen 80; server_name ok.xidd.com; index index.html index.htm index.php; root /alidata/www ...
- Java Hour 46 SLF4J
SLF4J(Simple Logging Facade for Jave). 上个章节在配置启动项目后,会报一个奇怪的错误,一开始以为是Maven 依赖有冲突,其实是和slf4j 密切相关的. 本文原 ...