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& ...
随机推荐
- Linux Tomcat 简介
如今,基于Web的应用越来越多,传统的Html已经满足不了如今的需求.我们需要一个交互式的Web,于是便诞生了各种Web语言.如Asp,Jsp,Php等.当然,这些语言与传统的语言有着密切的联系,如P ...
- hping3命令
hping3命令 网络测试 hping是用于生成和解析TCPIP协议数据包的开源工具.创作者是Salvatore Sanfilippo.目前最新版是hping3,支持使用tcl脚本自动化地调用其API ...
- IOC原理解释
spring ioc它其实是一种降低对象耦合关系的设计思想,通常来说,我们在一个类调用另一个类的方法的时候,需要不断的new新的对象来调用该方法,类与类之间耦合度比较高,有了ioc容器以后,ico容器 ...
- 在64位的linux上运行32位的程序
1.症状 (1)执行bin文件时提示:No such file or directory (2)ldd bin文件 的输出为: not a dynamic executable (3)file bi ...
- Ubuntu的一些常用快捷键
Ubuntu操作基本快捷键 * 打开主菜单 = Alt + F1 * 运行 = Alt + F2 * 显示桌面 = Ctrl + Alt + d * 最小化当前窗口 = Alt + F9 * 最大化当 ...
- atan函数与atan2函数
atan函数:传送门. atan2函数:传送门. atan 和 atan2 都是求反正切函数,如:有两个点 point(x1,y1), 和 point(x2,y2); 那么这两个点形成的斜率的角度计算 ...
- VelocityTracker简介
android.view.VelocityTracker主要用跟踪触摸屏事件(flinging事件和其他gestures手势事件)的速率.用addMovement(MotionEvent)函数将Mot ...
- Doodle Poll 投票文档
使用Doodle Poll网页文件可以让大家投票看什么时间大家都合适.
- 利用SQLiteOpenHelper来管理SQLite数据库 (转)
转载自 利用SQLiteOpenHelper来管理SQLite数据库 http://blog.csdn.net/conowen/article/details/7306545 Android学习笔记( ...
- 【框架】RefreshListView下拉刷新
布局: <com.example.administrator.d30_myrefreshlistview.RefreshListView android:id="@+id/refres ...