FFT 模板
#include<bits/stdc++.h>
#define ll long long
#define N 600005
using namespace std;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
struct CD{
double x,y;
CD(double a=,double b=){x=a,y=b;}
friend CD operator + (CD n1,CD n2){return CD(n1.x+n2.x,n1.y+n2.y);}
friend CD operator - (CD n1,CD n2){return CD(n1.x-n2.x,n1.y-n2.y);}
friend CD operator * (CD n1,CD n2){return CD(n1.x*n2.x-n1.y*n2.y,n1.x*n2.y+n1.y*n2.x);}
};
const double Pi=acos(-1.0);
int bit,n,m,nn;
CD a[N],b[N];
void FFT(CD *a,int n,int type){
for(int i=,j=;i<n;i++) {
if(j>i)swap(a[i],a[j]);
int k=n;
while(j&(k >>= ))j&=~k;
j|=k;
}
for(int i=;i<=bit;i++){
CD w_n(cos(*type*Pi/(<<i)),sin(*type*Pi/(<<i)));
for(int j=;j<n;j+=(<<i)){
CD w(,);
for(int k=j;k<j+(<<(i-));k++){
CD tmp=a[k],tt=w*a[k+(<<(i-))];
a[k]=tmp+tt;
a[k+(<<(i-))]=tmp-tt;
w=w*w_n;
}
}
}
if(type<)for(int i=;i<n;i++)a[i].x/=n;
}
int main(){
n=read();m=read();n++;m++;
for(int i=;i<n;i++)scanf("%lf",&a[i].x),a[i].y=0.0;
for(int i=;i<m;i++)scanf("%lf",&b[i].x),b[i].y=0.0;
bit=;
while((<<bit)<(n+m-))bit++;
nn=<<bit;
for(int i=n;i<nn;i++)a[i]=CD(0.0,0.0);
for(int i=m;i<nn;i++)b[i]=CD(0.0,0.0); FFT(a,nn,);FFT(b,nn,);
for(int i=;i<nn;i++)a[i]=a[i]*b[i];
FFT(a,nn,-); for(int i=;i<(n+m-);i++)printf("%d ",(int)(a[i].x+0.5));
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& ...
- FFT模板
我终于下定决心学习FFT了. orzCHX,得出模板: #include<cstdio> #include<cctype> #include<queue> #inc ...
随机推荐
- python装饰器入门
按别人的教程弄的. 要清楚基于类和基于函数的实现的不同之处. #!/usr/bin/env python # -*- coding: utf-8 -*- ''' class entryExit(obj ...
- AngularJS——依赖注入
依赖注入 依赖注入(DI)是一个经典的设计模式, 主要是用来处理组件如何获得依赖的问题.关于DI,推荐阅读Martin Flower的文章(http://martinfowler.com/art ...
- [LeetCode] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 限定符【const】用法大全
1. 限定符声明变量只能被读 const int i=5; int j=0; ... i=j; //非法,导致编译错误 j=i; //合法 2. 必须初始化 const int i=5; //合法 c ...
- Java8中的default方法
default方法 Java 8中引入了一个新的概念,叫做default方法,也可以称为Defender方法,或者虚拟扩展方法(Virtual extension methods). Default方 ...
- 汇编学习(六)——代码转换程序
(一)逻辑运算指令 一.双操作数逻辑运算指令 1.指令格式: AND dst,src ; "与"运算, OR dst,src ; "或"运算 XOR dst,s ...
- 汇编学习(一)——win7 64位调出debug
一.安装方法: 1.下载一个dosbox和win7 32位debug.exe,安装dosbox,打开页面 2. 将debug.exe放入磁盘根目录,这里以D盘为例.在dosbox中输入mount c ...
- Java学习随笔5:Java多线程编程
1. 线程是程序中单独顺序的控制流,线程本身依靠程序进行运行,线程是程序中的顺序控制流,只能使用分配给程序的资源和环境. 2. 进程是执行中的程序,一个进程可以包含一个或多个线程,但至少要包含一个线程 ...
- Notification
一:普通Notification 1.内容标题setContentTitle(...) 2.大图标setLargeIcon(Bitmap) 3.内容setContentText(...) 4.内容附加 ...
- JQ学习(二)
jQuery 效果 jQuery hide() 和 show() 语法: $(selector).hide(speed,callback); $(selector).show(speed,callba ...