码迷,mamicode.com
首页 > 其他好文 > 详细

[模板]快速傅里叶变换 FFT

时间:2018-12-16 18:44:53      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:git   string   get   inline   ring   tchar   iostream   amp   +=   

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define reg register
inline int read() {
    int res = 0;char ch = getchar();bool fu = 0;
    while(!isdigit(ch)) fu |= (ch == -), ch = getchar();
    while(isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
    return fu ? - res : res;
}

namespace BriMon
{
#define N 1100005
const double Pi = 3.14159265358979323846264338327950;
int n, m, rev[N<<2];
struct cmplx {
    double x, y;
    cmplx(double a = 0, double b = 0) {x = a, y = b;}
    friend cmplx operator + (cmplx a, cmplx b) { return cmplx(a.x + b.x, a.y + b.y); }
    friend cmplx operator - (cmplx a, cmplx b) { return cmplx(a.x - b.x, a.y - b.y); }
    friend cmplx operator * (cmplx a, cmplx b) { return cmplx(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);}
} b[N<<2], c[N<<2];

void fft(cmplx *f, int op) 
{
    for (reg int i = 0 ; i < n ; i ++) if (i < rev[i]) swap(f[i], f[rev[i]]);
    for (reg int p = 2 ; p <= n ; p <<= 1) 
    {
        int len = p >> 1;
        cmplx tmp = cmplx(cos(Pi / len), op * sin(Pi / len));
        for (reg int k = 0 ; k < n ; k += p) 
        {
            cmplx w = cmplx(1, 0);
            for (reg int l = k ; l < k + len ; l ++) 
            {
                cmplx __tmp = w * f[l + len];
                f[l + len] = f[l] - __tmp;
                f[l] = f[l] + __tmp;
                w = w * tmp;
            }
        }
    }
}

int main()
{
    n = read(), m = read();
    for (reg int i = 0 ; i <= n ; i ++) scanf("%lf", &b[i].x);
    for (reg int i = 0 ; i <= m ; i ++) scanf("%lf", &c[i].x);
    for (m += n, n = 1 ; n <= m ; n <<= 1);
    for (reg int i = 0 ; i < n ; i ++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0);
    fft(b, 1), fft(c, 1);
    for (reg int i = 0 ; i < n ; i ++) b[i] = b[i] * c[i];
    fft(b, -1);
    for (reg int i = 0 ; i <= m ; i ++) printf("%.0lf ", fabs(b[i].x) / n);
    return 0;
}
}

int zZh = BriMon :: main();
int main() {return 0;}

 

[模板]快速傅里叶变换 FFT

标签:git   string   get   inline   ring   tchar   iostream   amp   +=   

原文地址:https://www.cnblogs.com/BriMon/p/10127441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!