快速傅里叶变换(FFT)板子

19 篇文章 0 订阅

递归版

//UOJ 34
//Luogu 3803
//By Hany01
#include<bits/stdc++.h>
#define For(i ,j , k) for (int i = (j) ; i <= (k) ; ++ i)
#ifdef hany01
    #define debug(...) fprintf(stderr , __VA_ARGS__)
#else
    #define debug(...)
#endif
typedef long long LL;
using namespace std;
typedef complex<double>cpx;

inline void file()
{
    freopen("fft.in" , "r" , stdin);
    freopen("fft.out" , "w" , stdout);
}

template <typename T> inline bool chkmax(T &a , const T &b) { return a < b ? a = b , 1 : 0; }
template <typename T> inline bool chkmin(T &a , const T &b) { return a > b ? a = b , 1 : 0; }

char c_;
int _ , __;
inline int read()
{
    for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = (1 << 22) + 10;
const double Pi = acos(-1.0);
int n , m;
cpx a[maxn] , b[maxn];

inline void Init()
{
    n = read();
    m = read();
    For(i , 0 , n)
        a[i] = read();
    For(i , 0 , m)
        b[i] = read();
    m += n;
    for (n = 1 ; n <= m ; n <<= 1) ;
}

inline void FFT(cpx *x , int n , int type)
{
    if (n == 1)
        return ;
    cpx l[n >> 1] , r[n >> 1];
    for (int i = 0 ; i < n ; i += 2)
        l[i >> 1] = x[i] ,
        r[i >> 1] = x[i + 1];
    FFT(l , n >> 1 , type);
    FFT(r , n >> 1 , type);
    cpx wn(cos(2 * Pi / n) , sin(type * 2 * Pi / n)) , w(1 , 0) , t;
    for (int i = 0 ; i < n >> 1 ; ++ i , w *= wn)
    {
        t = w * r[i];
        x[i] = l[i] + t;
        x[i + (n >> 1)] = l[i] - t;
    }
}

int main()
{
#ifdef hany01
    file();
#endif
    Init();
    FFT(a , n , 1);
    FFT(b , n , 1);
    For(i , 0 , n)
        debug("%lf\n" , a[i].real());
    For(i , 0 , n)
        a[i] *= b[i];
    FFT(a , n , -1);
    For(i , 0 , m)
        printf("%d " , (int)round(a[i].real() / n));
    return 0;
}
//千里黄云白日曛,北风吹雁雪纷纷。
//    -- 高适《别董大二首》

迭代版(faster)

#include<bits/stdc++.h>

using namespace std;

#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define getchar getchar_unlocked
#define putchar putchar_unlocked

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const double pi = acos(-1.0);
const int maxn = 1<<21;

struct Complex
{
    double real, imag;
}a[maxn], b[maxn];
inline Complex operator + (const Complex &A, const Complex &B) { return (Complex){A.real + B.real, A.imag + B.imag}; }
inline Complex operator - (const Complex &A, const Complex &B) { return (Complex){A.real - B.real, A.imag - B.imag}; }
inline Complex operator * (const Complex &A, const Complex &B) { return (Complex){A.real * B.real - A.imag * B.imag, A.real * B.imag + A.imag * B.real}; }

int n, r[maxn];

inline void fft(Complex *a, int type)
{
    rep(i, n) if (i < r[i]) swap(a[i], a[r[i]]);
    for (register int i = 2; i <= n; i <<= 1)
    {
        register Complex wn = Complex{cos(2 * pi / i), sin(2 * pi / i * type)};
        for (register int j = 0; j < n; j += i)
        {
            register Complex w = Complex{1, 0};
            rep(k, i >> 1) {
                register Complex x = a[j + k], y = a[j + k + (i >> 1)] * w;
                a[j + k] = x + y; a[j + k + (i >> 1)] = x - y;
                w = w * wn;
            }
        }
    }
}

int main()
{
    int n1, n2, m, cnt;
    n1 = read() + 1; n2 = read() + 1; m = n1 + n2 - 1;
    rep(i, n1) a[i].real = read();
    rep(i, n2) b[i].real = read();
    for (n = 1; n < m; n <<= 1) ++ cnt;
    rep(i, n) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (cnt - 1));
    fft(a, 1);
    fft(b, 1);
    rep(i, n) a[i] = a[i] * b[i];
    fft(a, -1);
    rep(i, m) printf("%d", (int)(a[i].real / n + 0.5)), putchar(' ');
    return 0;
}
//楚塞三湘接,荆门九派通。
//江流天地外,山色有无中。
//郡邑浮前浦,波澜动远空。
//襄阳好风日,留醉与山翁。
//--王维《汉江临泛》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值