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& ...
随机推荐
- 【Django】如何自定义manage.py命令? 达到启动后台进程的目的?
代码: #-*- coding:utf- -*- """ The handle active user mail send """ from ...
- 【Spring】Spring系列2之bean的配置
2.bean的配置 2.1.IOC概述 2.2.bean的获取 2.3.依赖注入方式 2.4.属性注入细节 内部bean,不需要ID,ID无效,外部不能引用: 2.5.集合属性注入 2.6.使用p命名 ...
- codeforces 459C Pashmak and Buses 解题报告
题目链接:http://codeforces.com/problemset/problem/459/C 题目意思:有 n 个 students,k 辆 buses.问是否能对 n 个students安 ...
- GLSL的qualifier
uniform:从应用程序到vertex shader 到fragment shader都能使用,但是值一直不变: varying:从vertex shader到fragment shader,在fr ...
- free(): invalid next size (fast/normal)问题
本文转自 http://blog.sina.com.cn/s/blog_77f1e27f01019qq9.html ,在此感谢! c++编译常会出现free(): invalid next size ...
- java 基本数据类型
long 是长整型,在怎么长本身也是整型,12.10的整形部分是12,当然结果是12, byte: 八位整数 -128——127,可用来节省内存的使用.short: 16位整数 -32768——32, ...
- caffe 无GPU 环境搭建
root@k-Lenovo:/home/k# sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-d ...
- mysql TIMESTAMP详解
navicat中设置timestamp字段的时间,默认这里填写CURRENT_TIMESTAMP,就是在插入数据的时候按照当前时间插入: 勾选根据当前时间戳更新,表示在UPDATE的时候,会根据当前时 ...
- Java Hour 49 保存和查询历史的Weather
吾一直坚信,是需求的不断变化推动了架构的不断演变. 新的需求 能够查看指定日期的Weather,因为客户想要比较昨天和今天的天气情况,所以需要能够查询历史数据的功能. 1 能保存当前的天气到数据库 2 ...
- C++程序设计课程学习的网址
很详细 C++程序设计课程主页 http://blog.csdn.net/sxhelijian/article/details/7910565 孙鑫C++视频教程 rmvb格式 全20CD完整版 ...