FFT 板子

来源:互联网 发布:windows 程序设计 编辑:程序博客网 时间:2024/05/15 14:11
#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_unlockedinline 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;}//楚塞三湘接,荆门九派通。//江流天地外,山色有无中。//郡邑浮前浦,波澜动远空。//襄阳好风日,留醉与山翁。//--王维《汉江临泛》
原创粉丝点击