dft + fft(模板)

dft(多项式相乘模板)

/*
  Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>

using namespace std;

const double pi = acos(-1.0);

const int N = 3e6 + 10;

struct Complex {
    double r, i;

    Complex(double _r = 0, double _i = 0) : r(_r), i(_i) {}
}a[N], b[N];

Complex operator + (const Complex & a, const Complex & b) {
    return Complex(a.r + b.r, a.i + b.i);
}

Complex operator - (const Complex & a, const Complex & b) {
    return Complex(a.r - b.r, a.i - b.i);
}

Complex operator * (const Complex & a, const Complex & b) {
    return Complex(a.r * b.r - a.i * b.i, a.r * b.i + a.i * b.r);
}

int n, m;

void dft(Complex *f, int lim, int rev) {
    if(!lim) return ;
    Complex a1[lim], a2[lim];
    for(int i = 0; i < lim; i++) {
        a1[i] = f[i << 1], a2[i] = f[i << 1 | 1];
    }
    dft(a1, lim >> 1, rev);
    dft(a2, lim >> 1, rev);
    Complex wn = Complex(cos(pi / lim), rev * sin(pi / lim)), w = Complex(1, 0);
    for(int i = 0; i < lim; i++, w = w * wn) {
        f[i] = a1[i] + w * a2[i];
        f[i + lim] = a1[i] - w * a2[i];
    }
}

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    scanf("%d %d", &n, &m);
    for(int i = 0; i <= n; i++) {
        scanf("%lf", &a[i].r);
    }
    for(int i = 0; i <= m; i++) {
        scanf("%lf", &b[i].r);
    }
    int lim = 1;
    while(lim <= n + m) lim <<= 1;
    dft(a, lim >> 1, 1);//dft
    dft(b, lim >> 1, 1);//dft
    for(int i = 0; i < lim; i++) {
        a[i] = a[i] * b[i];
    }
    dft(a, lim >> 1, -1);//idft
    for(int i = 0; i <= n + m; i++) {
        printf("%d%c", int(a[i].r / lim + 0.5), i == n + m ? '\n' : ' ');
    }
	return 0;
}

fft(大数乘法模板)

/*
  Author : lifehappy
*/
#include <bits/stdc++.h>

using namespace std;

const double pi = acos(-1.0);

const int N = 5e6 + 10;

struct Complex {
    double r, i;

    Complex(double _r = 0, double _i = 0) : r(_r), i(_i) {}

}a[N], b[N];

Complex operator + (const Complex & a, const Complex & b) {
    return Complex(a.r + b.r, a.i + b.i);
}

Complex operator - (const Complex & a, const Complex & b) {
    return Complex(a.r - b.r, a.i - b.i);
}

Complex operator * (const Complex & a, const Complex & b) {
    return Complex(a.r * b.r - a.i * b.i, a.r * b.i + a.i * b.r);
}

char str1[N], str2[N];

int r[N], ans[N];

void fft(Complex * f, int lim, int rev) {
    for(int i = 0; i < lim; i++) {
        if(r[i] < i) {
            swap(f[i], f[r[i]]);
        }
    }
    for(int i = 1; i < lim; i <<= 1) {
        Complex wn = Complex(cos(pi / i), rev * sin(pi / i));
        for(int p = i << 1, j = 0; j < lim; j += p) {
            Complex w = Complex(1, 0);
            for(int k = 0; k < i; k++, w = w * wn) {
                Complex x = f[j + k], y = w * f[i + j + k];
                f[j + k] = x + y, f[i + j + k] = x - y;
            }
        }
    }
    if(rev == -1) {
        for(int i = 0; i < lim; i++) {
            f[i].r /= lim;
        }
    }
}

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n, m, lim;
    while(scanf("%s %s", str1, str2) != EOF) {
        n = strlen(str1), m = strlen(str2);
        for(int i = 0; i < n; i++) {
            a[i] = Complex(str1[n - i - 1] - '0', 0);
        }
        for(int i = 0; i < m; i++) {
            b[i] = Complex(str2[m - i - 1] - '0', 0);
        }
        lim = 1;
        while(lim  < (n << 1) || lim < (m << 1)) lim <<= 1;
        for(int i = 0; i < lim; ++i) {
            r[i] = (i & 1) * (lim >> 1) + (r[i >> 1] >> 1);
        }
        for(int i = n; i < lim; i++) {
            a[i] = Complex(0, 0);
        }
        for(int i = m; i < lim; i++) {
            b[i] = Complex(0, 0);
        }
        fft(a, lim, 1);
        fft(b, lim, 1);
        for(int i = 0; i < lim; i++) {
            a[i] = a[i] * b[i];
        }
        fft(a, lim, -1);
        for(int i = 0; i < lim; i++){
            ans[i] = int(a[i].r + 0.5);
        }
        for(int i = 0; i < lim; i++){
            ans[i + 1] += ans[i] / 10;
            ans[i] %= 10;
        }
        lim = n + m - 1;
        while(ans[lim] <= 0 && lim > 0) lim--;
        for(int i = lim; i >= 0; i--){
            printf("%d", ans[i]);
        }
        printf("\n");
    }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值