FFT模板(From MG)

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 using namespace std;
 5 struct cp{double x,y;};
 6 int n1,n2,n,m;
 7 double pi=acos(-1);
 8 cp a[500010],b[500010],cur[500010];
 9 cp operator *(cp x,cp y){return (cp){x.x*y.x-x.y*y.y,x.x*y.y+x.y*y.x};}
10 cp operator +(cp x,cp y){return (cp){x.x+y.x,x.y+y.y};}
11 cp operator -(cp x,cp y){return (cp){x.x-y.x,x.y-y.y};}
12 void fft(cp *a,int n,int fl)
13 {
14     for (int i=n>>1,j=1;j<n;j++)
15     {
16         if (i<j) swap(a[i],a[j]);
17         int k=n>>1;
18         for (;k&i;i^=k,k>>=1);
19         i^=k;
20     }
21     for (int m=2;m<=n;m<<=1)
22     {
23         cp w=(cp){cos(2*pi*fl/m),sin(2*pi*fl/m)};
24         cur[0]=(cp){1,0};
25         for (int i=1;i<m;i++) cur[i]=cur[i-1]*w;
26         for (int i=0;i<n;i+=m)
27             for (int j=i;j<i+(m>>1);j++)
28             {
29                 cp u=a[j],v=a[j+(m>>1)]*cur[j-i];
30                 a[j]=u+v;
31                 a[j+(m>>1)]=u-v;
32             }
33     }
34 }
35 int main()
36 {
37     scanf("%d%d",&n1,&n2);n1++;n2++;
38     for (int i=0;i<n1;i++) scanf("%lf",&a[i].x);
39     for (int i=0;i<n2;i++) scanf("%lf",&b[i].x);
40     n=max(n1,n2);
41     m=1;while (m<=n*2) m*=2;
42     fft(a,m,1);fft(b,m,1);
43     for (int i=0;i<=m;i++) a[i]=a[i]*b[i];
44     fft(a,m,-1);
45     for (int i=0;i<n1+n2-1;i++) printf("%d ",(int)(a[i].x/m+0.5));
46 }
时间: 2024-08-08 12:10:29

FFT模板(From MG)的相关文章

再写FFT模板

没什么好说的,今天有考了FFT(虽然不用FFT也能过)但是确实有忘了怎么写FFT了,于是乎只有重新写一遍FFT模板练一下手了.第一部分普通FFT,第二部分数论FFT,记一下模数2^23*7*17+1 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef double

fft模板 HDU 1402

1 // fft模板 HDU 1402 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <vector> 8 #include <math.h> 9 #include <memory.h> 10 #include <bits/stdc++.h> 11 using

hdu1402(大数a*b&amp;fft模板)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 给出两个长度1e5以内的大数a, b, 输出 a * b. 思路: fft模板 详情参见: http://blog.csdn.net/sdj222555/article/details/9786527  https://wenku.baidu.com/view/8bfb0bd476a20029bd642d85.html 可以将 a, b 看成两个多项式, 每个数位为一项, 每一位上的

FFT模板

  #include <cstdio> #include <cmath> #include <cstring> using namespace std; const double pi = acos(-1.0); const int maxn = 500005; struct complex { double r,i; complex(double r = 0.0,double i = 0.0) :r(r),i(i) {} inline complex operator

[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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

【BZOJ 2179】【FFT模板】 FFT快速傅立叶

2179: FFT快速傅立叶 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 1595 Solved: 792 [Submit][Status][Discuss] Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. Output 输出一行,即x*y的结果. Sample Input 1 3 4 Sample Output

【bzoj2179】FFT快速傅立叶 FFT模板

2016-06-01  09:34:54 很久很久很久以前写的了... 今天又比较了一下效率,貌似手写复数要快很多. 贴一下模板: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #includ

高精度乘法FFT 模板

渣模板,不知为何常数还挺大.. CODE: #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 200010 #define PI 3.1415926535897932384626 using namespace std; struct Complex{ double real,imag

FFT 模板

FFT(Fast Fourier Transformation/快速傅立叶变换),确切地说应该称之为FDFT(Fast Discrete Fourier Transformation/快速离散傅立叶变换),因为FFT是为解DFT问题而设计的一种快速算法.在深入讨论之前,有必要特别指出这一点. DFT问题: 给定一个复数域上的n-1次多项式A(x)的系数表示(cofficient representation)(a[0], a[1], ..., a[n-1]),求A(x)的某个点-值表示(poin